Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

typo? variable misteriously becoming undef

by scain (Curate)
on Nov 14, 2002 at 19:54 UTC ( #212990=perlquestion: print w/ replies, xml ) Need Help??
scain has asked for the wisdom of the Perl Monks concerning the following question:

Hello Fellow Monks,

I return to coding after several months off and it seems like I have forgotten everything I ever knew. As a warmup project, I wanted to make a simple parser that would take free-form address data and generate V-card formatted data, with the help of user input. This was supposed to be an easy, one off script, that I have now been working on for a few hours.

Here is how it is suppposed to work: Read through a file one line at a time and present its contents at STDOUT. The user then selects an action, either to split the line on commas, thus breaking up a city and state for later categorization, or the user selects what category the item belongs in. After the user selects a category, the array that holds the data (possibly from a previous split) is shifted, and then it goes through the loop again. Seems straight forward enough. (As a side note: yes I really do want to do it this way, because it really is free-form: some addresses are typical, three or four lines, some are all mushed together in one line, comma delimited.)

But, when a file has this as its contents Medina, OH, and Medina is properly categorized as a city, and the shift is done, initially, $la[0] holds "OH", but when it gets to the bottom of the loop, it is mysteriously undef. I don't see how or why, but I suspect that it is my rusty skills causing me to miss something. Please take a look at the code below and let me know what I am missing.

Thanks,
Scott

#!/usr/bin/perl -w use strict; my @la = (); my %card; open FILE, $ARGV[0] or die "couldn't open $ARGV[0]: $!\n"; while (1) { my $response; if ( scalar @la == 0 ) { last if eof FILE; chomp( $la[0] = <FILE> ); } if ( $la[0] =~ /^\s*(\S.*\S*)\s*$/ ) { $la[0] = $1; print <<STDOUT; 1 - split line on commas 2 - Last name 3 - First name 4 - Street address 5 - City 6 - State 7 - Home phone 8 - Work phone 9 - cell phone 0 - other (comment) $la[0] STDOUT chomp( $response = <STDIN> ); if ( $response == 2 ) { $card{'ln'} = $la[0]; shift @la; } if ( $response == 3 ) { $card{'fn'} = $la[0]; shift @la; } if ( $response == 4 ) { $card{'sa'} = $la[0]; shift @la; } if ( $response == 5 ) { $card{'ci'} = $la[0]; shift @la; print "in response == 5: $la[0]\n"; #"OH" like you would expect } if ( $response == 6 ) { $card{'st'} = $la[0]; shift @la; } if ( $response == 7 ) { $card{'hp'} = $la[0]; shift @la; } if ( $response == 8 ) { $card{'wp'} = $la[0]; shift @la; } if ( $response == 9 ) { $card{'cp'} = $la[0]; shift @la; } if ( $response == 1 ) { @la = split /\,\s*/, $la[0]; } else { $card{'co'} = $la[0]; shift @la; } if ($la[0] eq undef) { print "\$la[0] is undef\n"; #now it's undef! } } else { #nothing matched on this line, probably empty warn "WARN: $la[0]\n"; @la = (); } } close FILE;

Comment on typo? variable misteriously becoming undef
Select or Download Code
Re: typo? variable misteriously becoming undef
by John M. Dlugosz (Monsignor) on Nov 14, 2002 at 20:04 UTC
    1) don't do eq undef. That's nonesence. You cannot compare something directly against undef (so I've read somewhere around here) but should only test it directly.

    2) Your shift @la gets rid of the OH and moves everything else down one. I don't see a $la[1] anywhere, so guess what? I don't see why it's not already gone at the first print.

      John,

      Thanks:

      1. It felt funny doing it, but it seemed to work, so I left it.

      2. Just to clarify: The first time through the loop, $la[0] contains "Medina, OH", then I do a split /\,\s*/, $la[0] and assign it back into @la, so now $la[0] should contain "Medina" and $la[1] should contain "OH". Second time through the loop, I select "5", $la[0] gets assigned into the hash, and I shift, which should bump "Medina" out, and put "OH" at $la[0].

      Scott

Re: typo? variable misteriously becoming undef
by Enlil (Parson) on Nov 14, 2002 at 20:06 UTC
    I think your problem lies in the shift @la.in your code as $la[0] is taken from the from being set and $la[1] is now $la[0].. You seem to be adding things to the array and then removing them ala shift. So when you get to the end you have nothing in the array.

    update: I changed it so it made more sense, as when I reread it the third time, I seemed to have been babbling.

    -enlil

Re: typo? variable misteriously becoming undef
by dws (Chancellor) on Nov 14, 2002 at 20:08 UTC
    You're falling into the
    else { $card{'co'} = $la[0]; shift @la; }
    case, which clears @la by shifting out the single element that's it's holding (i.e., "OH"). To verify this, dump %card.

    I suspect you mean to be using "elsif" instead of a squence of single "if" statements followed by an "if/else". As you have things set up, you'll hit that else case for any $response other than 1.

      Holy {explitive deleted}!

      I knew it was something stupid. I can't believe I did that, and now I've shown everyone how far you can regress when you don't code for a few months (and you use cut and paste too much).

      Thanks,
      Scott

Re: typo? variable misteriously becoming undef
by elusion (Curate) on Nov 14, 2002 at 20:10 UTC
    You've forgotten your elsif!

    You are using many different if statements, so you can go through more than one. This is important because if your else statement.

    $response is tested to be equal to be 2,3,4, and 5, at which point, you have action, and your value's printed. Then it continues to be tested for 6,7,8,9, and 1. After the one, you have an else. Since response equals 5 and not 1, the else is executed, shifting the array again.

    Change all the ifs but the first in the series to elsifs, and it should work. ;-)

    elusion : http://matt.diephouse.com

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://212990]
Approved by dws
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2014-12-20 03:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (95 votes), past polls