scain has asked for the wisdom of the Perl Monks concerning the following question:
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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: typo? variable misteriously becoming undef
by dws (Chancellor) on Nov 14, 2002 at 20:08 UTC | |
by scain (Curate) on Nov 14, 2002 at 20:14 UTC | |
|
Re: typo? variable misteriously becoming undef
by elusion (Curate) on Nov 14, 2002 at 20:10 UTC | |
|
Re: typo? variable misteriously becoming undef
by John M. Dlugosz (Monsignor) on Nov 14, 2002 at 20:04 UTC | |
by scain (Curate) on Nov 14, 2002 at 20:11 UTC | |
|
Re: typo? variable misteriously becoming undef
by Enlil (Parson) on Nov 14, 2002 at 20:06 UTC |