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;

In reply to typo? variable misteriously becoming undef by scain

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.