Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^5: Recomendations For Learning perl?

by 1nickt (Canon)
on Dec 07, 2016 at 22:49 UTC ( [id://1177448]=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^6: Recomendations For Learning perl?
by adamZ88 (Beadle) on Dec 08, 2016 at 04:26 UTC

    You are too kind! I totally do see myself thinking of a project like this one and making it a reality when I am more proficient in Perl. Surprisingly I follow almost all of your code! The two things that cause some head scratching is loading the module and the second argument that needs to be passed. Is this just the length of the name? I know I have loaded modules previously, I just forgot how, I will do my research.

      the second argument that needs to be passed. Is this just the length of the name?

      No, there's only one argument expected. That line of code is the program's error checking. It does two things, but acts on the same variable both times.

      ( my $name = $ARGV[0] ) && length $ARGV[0] > 2 or die "Usage: `$0 <nam +e>` (min. 3 letters)";
      What's happening here is we are evaluating the truth of an expression without using an explicit if statement.
      my $name = $ARGV[0]
      will evaluate to false if the first argument passed in is empty (i.e. non-existent) because then the value of $name will be false (so `0` would fail also)[1]. If that happens, the or die ... kicks in. But if it evaluates to true, then
      && length $ARGV[0] > 2
      is evaluated for truth, so at this point we know we have some kind of value in the arg passed in, and now we check that it's at least three characters long. If the second part of the evaluation fails, the whole thing fails (because of the &&), and again we go to or die ....

      Finally, in our error message emitted upon dying, we use $0, the global variable that holds the name of the currently executing script, and explain what the problem was.

      Amazing how much useful stuff you can do with one line of Perl, isn't it?

      [1] So in Perl you can evaluate truth very concisely:

      while ( 1 ) { # runs forever ... } if ( 0 ) { # won't run ... } sub foo { return 'something'; # or maybe not } if ( foo() ) { # only runs if foo() returns something ... }

      Hope this helps.

      edit: added missing parens


      The way forward always starts with a minimal test.

        Wow! I totally missed that. Yes, it is amazing how one line can achieve so much. Even as a beginner, I feel that I pressure myself to keep my code as concise as possible, thus, I like to achieve a lot with little code, though it does get confusing at times. Your breakdown of the program is very detailed and helpful! I was eager to try the program myself, so I installed the recommended module, copied the code and ran it. I did not have words installed, so I took care of that as well. The output is not as expected. I tried Steve's one-liner below and that works correctly for what it is supposed to do. But the output for your program is below:

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1177448]
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: (5)
As of 2024-04-23 21:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found