Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: Recomendations For Learning perl?

by adamZ88 (Beadle)
on Dec 07, 2016 at 21:00 UTC ( [id://1177426]=note: print w/replies, xml ) Need Help??


in reply to Re: Recomendations For Learning perl?
in thread Recomendations For Learning perl?

Looks very helpful. Thanks. I do find it that programming for things that interest me makes things easier and more fun to learn. I will give this I tr. Hmmm I wonder how many words contain my name.....

  • Comment on Re^2: Recomendations For Learning perl?

Replies are listed 'Best First'.
Re^3: Recomendations For Learning perl?
by 1nickt (Canon) on Dec 07, 2016 at 21:31 UTC

      I don't suppose you would want to share the code for this program hu Nick? It is pretty cool.

        Sure, here you go:

        use strict; use warnings; use Algorithm::Diff qw/ LCS_length /; ( my $name = $ARGV[0] ) && length $ARGV[0] > 2 or die "Usage: `$0 <nam +e>` (min. 3 letters)"; open my $fh, '<', '/usr/share/dict/words' or die $!; my $found = 0; while ( <$fh> ) { next unless LCS_length( [ split //, $name ], [ split // ] ) == len +gth $name; $found++; print "$found $_"; } __END__
        Things to maybe note:
        • How to load a clever module and import its LCS_length function to do all the work :-)
        • How to capture the user input from the command line in the array @ARGV
        • How to open a file for reading
        • How to create a counter and increment it in a loop
        • How to read from an open filehandle line-by-line (which avoids reading it all into memory), using:
          while ( <$fh> ) { ... }
        • The most obfuscated thing in this script: How to be lazy and not have to tell a function what to operate on by using the default argument, which is the current value of the magic variable $_. So in a loop I can say:
          while ( <$fh> ) { # each line is loaded into $_ in turn my @characters = split //; }
          , omitting the second argument to split, and Perl knows to Do The Right Thing. More self-documenting usage would be:
          while ( my $line = <$fh> ) { # each line now in $line my @characters = split //, $line; }
        Note that you can get documentation on many of Perl's built-in functions with perldoc -f <function_name>, e.g. perldoc -f split.

        Hope this helps! have fun ....

        edit: fixed missing parens


        The way forward always starts with a minimal test.
        I am pretty sure 1nickt will share it with you, since you kindly ask. Sharing code is certainly part of the Perl culture and also part of this site's raison d'être.

        And you might very well be astonished by the small numbre of code lines needed to doing that.

        I learned Linux and pearl
        Oh, and BTW, the proper name of the language is Perl.

        Command line one-liner:

        perl -wMstrict -E 'print for grep /adam/, <>' /usr/share/dict/words

        Script form (overly verbose so it's easier to understand):

        use warnings; use strict; my $word_file = '/usr/share/dict/words'; open my $fh, '<', $word_file or die "can't open the $word_file file!: $!"; while (my $line = <$fh>){ if ($line =~ /adam/){ print $line; } }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (11)
As of 2024-04-19 16:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found