Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Befuddled by The Llama

by bluethundr (Pilgrim)
on May 30, 2004 at 21:38 UTC ( [id://357679]=perlquestion: print w/replies, xml ) Need Help??

bluethundr has asked for the wisdom of the Perl Monks concerning the following question:

Hey guys, It's been a little while since I've been back to the gates, because I have decided to have a "fresh start" with the Llama. This after a pusillanimous start in my path to true perl wisdom.

Now, I know that some folks out there aren't happy that I haven't yet learned the use of 'strict'. To whit I say "PATIENCE my brethren!" ;P. For I realize that I've only ridden tricycles my whole life. Now I've taken it upon myself to learn how to drive a fricken' Batmobile and I'm only up to 3rd gear...out of 27 or so! It'll be some time before I get to ejector seats n' oilslicks! As soon as doc merlyn 'splain to me 'bout strict in da book, I promise to you I will start using it! :D

At any rate, I've been cruisin' right along through the examples up to this point! But I have inadvertantly rammed the Bat'bile into a wall at the moment. For the past two hours I've been frying my brain with variations on Chapter 3 example 2.
#!/usr/bin/perl -w @names = qw/fred barney wilma betty/; @in; $count = 0; print "\n\n\n"; # just some dumb formatting while ($in[$count] !~ /q/i) { print "\nPlease enter a number: "; chomp($in[$count]); count++; } foreach $x (@in) { print $names[$x]; print "\n"; } print "\n\n\ndone.\n\n\n"; # just more formatting and so I know it's + "done"


The error I get (consistantly) is

Use of unitialized value in pattern match (m//) at ./chp3-ex2.pl in line 11, <STDIN> line 2.

I swear to you that I've been tinkering with this for HOURS!!!! I suppose, no I know I've learned some stuff from the tinkering! That's what I've been doing with EVERY example from the book. Tinkering, experimenting, playing. I've pretty much been ignoring the "time suggestions". Usually I'm way under even those suggestions! But for now, I sit here...befuddled. I think I am going to have a little break and go take in some bad sci-fi!

By the way, for those of you who gave me SAGE advice on my last go round through Perl 21 (which I abandoned halfway through)...your words are *NOT* lost on me! EVERY POST has been read, re-read and will be re-read AGAIN after I have a better grasp of the llama!

Replies are listed 'Best First'.
Re: Befuddled by The Llama
by davidj (Priest) on May 30, 2004 at 22:08 UTC
    You have some typos:

    You have a missing ")" on the following line:

    while ($in[$count] !~ /q/i {
    It should be
    while ($in[$count] !~ /q/i) {
    Second:
    chomp($in[$count]";
    should be:
    chomp($in[$count]);
    Third:
    print "\n\n\n"done.\n\n\n";
    should be:
    print "\n\n\ndone.\n\n\n";
    And finally:
    count++;
    should be:
    $count++;
    Furthermore, you will loop endlessly on:
    while ($in[$count] !~ /q/i) {
    because nothing in @in has been set.

    update: You would have saved a lot of time by "use warnings;". It would have pointed out all the errors :)

    davidj
      You would have saved a lot of time by "use warnings;". It would have pointed out all the errors :)

      Interestingly, he did, as he had thrown the '-w' switch to the perl executable. It's curious that he didn't get more warnings... unless the OP didn't give us all of them.

Re: Befuddled by The Llama
by parv (Parson) on May 30, 2004 at 22:34 UTC

    In the name of everybody's sanity, please post the code that one can actually execute which generates the errors/problems posted.

    Your code as posted has missing/misplaced [)"$]. Even after correcting above errors, the error message does/will not match the code as it is missing data fetching from STDIN which results in an infinite loop.

    Let me try my rough mind reading abilities ... the error you (would) get is (would be) due to an undef value, $in[$count], being compared when the while loop is entered.

    UPDATE May 31 2004: Restructured.

Re: Befuddled by The Llama
by reTard (Sexton) on May 30, 2004 at 22:31 UTC
    I'm no expert but these kinds of errors are really easy to spot when you have an editor (such as vim) that uses colours to show what your code is doing. I.e. red = quoted strings, yellow = statements like print, dark blue = comments and light blue = variables.

      Textpad is great for Windows users, but I liked KDE's KATE the most. Not to mention Jext, jEdit, Editpad, Syn, etc.

      How can you feel when you're made of steel? I am made of steel. I am the Robot Tourist. Robot Tourist, by Ten Benson

Re: Befuddled by The Llama
by TomDLux (Vicar) on May 30, 2004 at 22:53 UTC

    Your array @in gets read in various places, but nowhere do you assign anything to it. The program might work better if you read user input somewhere.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

Re: Befuddled by The Llama
by Anonymous Monk on May 30, 2004 at 23:09 UTC
    As others stated ... it seems you want to let the user type something in, but nowhere do you provide the place to do it! Since you seem to be using this approach (getting user input, even when it's not really necessary for the problem), and since it seems to be the same design each time (with error introduced, albeit), I'd suggest adhering to the 'Lazy' virtue of a perl programmer and go ahead and write some functions for all this input junk and just reuse the stuff time in and time out. Immediately, you might try (although this doesn't provide the massive cleanup necessary to make it look decent),
    $in[$count] = <STDIN>; while ($in[$count] !~ /q/i { print "\nPlease enter a number: "; chomp($in[$count]"; count++; $in[$count] = <STDIN>; }

      I would suggest getting input BEFORE doing the chomp .... and since you want this to happen at least once, it is the ideal situation for using do{} while();. Note that this requires moving the increment.

      do { print "\nPlease enter a number: "; $in[$count] = <STDIN>; chomp($in[$count]"; } while ($in[$count++] !~ /q/i

      --
      TTTATCGGTCGTTATATAGATGTTTGCA

      The given situation is perfect for a do-while loop to avoid the need to read from stdin in more than one place...

      { my $count = 0; do { print "\nPlease enter a number: "; $in[$count++] = <STDIN>; } while ( $in[-1] !~ m/q/i ); pop @in; # Remove 'quit signal' } chomp @in;

      ...but i would personally prefer something like...

      while ( 'reading...' ) { print "\nPlease enter a number: "; my $in = <STDIN>; do { push @in , $in; next; } unless $in =~ m/q/i; last; } chomp @in;
Re: Befuddled by The Llama
by Seumas (Curate) on Jun 01, 2004 at 00:43 UTC
    Now, I know that some folks out there aren't happy that I haven't yet learned the use of 'strict'. To whit I say "PATIENCE my brethren!"

    I swear to you that I've been tinkering with this for HOURS!!!!

    Honestly, if you learn my and use strict;, you would have fewer problems, learn to avoid bad habits and troubleshoot issues faster. Strict will tell you when and what is wrong and the only real prerequisite for it is understanding my. If you would spend ten minutes reading up on both of these in the PerlMonk's library, you'd save yourself those HOURS!!!! of tinkering.
      ++Seumas. use strict and use warnings aren't things you tack on after you know what you're doing. They're the first lines you write in every program you write, starting with Hello World.

      I don't understand why bluethundr would rather spend HOURS staring at non-working code and then post to Perlmonks and wait for a response, rather than simply avoiding problems by using what he knows is the correct style from the start. (This is the second node I've seen by him that fits this pattern; there may be others.)

      But I do know it's not worth my time to debug his code and offer my suggestions until he takes those steps for himself. For one thing, since he's ignoring the simple and commonsense advice he's being universally offered, I suspect he would ignore whatever I recommended as well.

Re: Befuddled by The Llama
by robot_tourist (Hermit) on Jun 01, 2004 at 07:28 UTC

    #!/usr/bin/perl -w use strict; use warnings; use diagnostics; # code follows here...

    :)

    How can you feel when you're made of steel? I am made of steel. I am the Robot Tourist. Robot Tourist, by Ten Benson

    Update: s/codes/code/

      #!/usr/bin/perl -w use warnings;

      This is a bit redundant, isn't it? One or the other would be sufficient.

        Not for Windows users, you insensitve clod!

        I always wanted to say that! Well since I read the comments to a /. poll last week anyway.

        How can you feel when you're made of steel? I am made of steel. I am the Robot Tourist.
        Robot Tourist, by Ten Benson

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://357679]
Approved by sgifford
Front-paged by arden
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (7)
As of 2024-04-23 13:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found