Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: Radoteur

by QuillMeantTen (Friar)
on Oct 19, 2015 at 06:51 UTC ( [id://1145296]=note: print w/replies, xml ) Need Help??


in reply to Re: Radoteur
in thread Radoteur

Great many thanks for your input, I have started rewriting my code before even reading all your post, so here is my take on your advice:

#!/usr/bin/perl #this is a perl implementation of the radoteur from the book "Théorie +du Bordel #Ambiant" authored by the french researcher Roland Moreno #It will take a word list as input and output giberrish-y words from i +t, after #a while it will cycle, this program has been specially written to ide +ntify #the relationship between the wordlist and the size of the cycles #next thing would be to do some stat works on the word size, their num +bers #inside the wordlist to find the equation that models the word list - +cycle #size the most accurately use strict; use warnings; use autodie; my($evt,$niter); my $cycled = 0; $niter= 0; #this is a double hash, the first level keys are line numbers, the sec +ond #its values are another hash which keys are letter coordinates inside +the #line's word, if we have the same letter in the same word on the same +line #appear twice then we have cycled my %letter_lines; if(!defined $ARGV[0]){ die "to use me, give me the word list file as argument\n"; } while($cycled == 0){ open my $fh, '<',$ARGV[0]; FILE_ITER: while(<$fh>){ #here I take in the new word and split it into its letters #I also count the iteration number my $word = $_; $niter++; my @letters = split("", $word); #if evt is undefined, that's because we were looking for a + newline #in the previous event or its the first iteration, in any +case #the new event will be the first letter of the current wor +d if(!defined($evt)){ if(defined $letter_lines{$.}{0}){ warn "we have cycled: doing line $. letter + 0 again\n"; $cycled = 1; last FILE_ITER; } else{ $letter_lines{$.}{0} = 1; warn "adding to hash line $. letter 0\n"; } $evt = shift @letters; print $evt; next FILE_ITER; } else{ WORD_ITER:foreach my $i (0 .. $#letters){ if($evt eq $letters[$i]){ my $j = $i+1; $evt = $i < $#letters?$letters[$j]:undef; if(defined $evt){ if(defined $letter_lines{$.}{$j}){ warn "we have cycled : line $. letter $j \n"; $cycled = 1; last FILE_ITER; } else{ warn "adding to hash line $. letter $j\n"; $letter_lines{$.}{$j} = 1; print $evt; } } last WORD_ITER; } } } } close($fh); warn "going back to the beginning\n"; } warn "cycled in $niter iterations\n";

Replies are listed 'Best First'.
Re^3: Radoteur
by stevieb (Canon) on Oct 19, 2015 at 14:46 UTC

    Your loop labels are not required here (and rarely ever are). You can remove them in both the loops, and the last and next calls. For example, replace:

    WORD_ITER:foreach(...)

    with:

    foreach(...)

    and:

    next WORD_ITER; last WORD_ITER;

    with just:

    next; last;

    You can do this in both your while and for statements/blocks.

      I know they are not needed, yet I read in Perl Best Practices that whenever last or next are used adding labels helps will the readability and future maintanability of the code

      It seemed to make sense so I adopted the practice. update: typo

        I read in Perl Best Practices that whenever last or next are used adding labels helps will the readability

        If you are prepared to pay a 15% performance penalty for that asinine piece of advice; continue:

        cmpthese -1,{ a=>q[ LOOP:for(1..1000){ next LOOP; } ], b=>q[ for(1..1000){ next; } ] };; Rate a b a 7862/s -- -14% b 9195/s 17% --

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1145296]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-18 16:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found