http://qs321.pair.com?node_id=147256

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

So I am about switch phones, but panasonic cant get the 200+ phone numbers I have saved on my phone to the new one, so I wrote this program to create a text file of all the names in the phone book and their numbers, that way i can create a program to parse the output and (granted manually) enter them into the new one.The problem, however, is some numbers in my phone book only have a first name, not a first and a last, (i.e. "Doctor" or "school") and so I assign a 0 to that field. When I go to write the data I get a uninitialized value error at line 45 if ($2 =~ /^0$/) {, which doesnt make any sense to me since $2 is defined as 0. Anyway here is the code, if anyone has a clue what is going on, thanks alot! thans alot anyway
#!/usr/bin/perl -w use strict; my (@def, %char, $cont, $field, @info, %book, $file, $name, $i); @def = qw\ first_name second_name work_number home_number cell_number +\; #fields %char = ( #regex for each field 'first_name' => '^[a-z]+$', 'second_name' => '^[a-z]*$', 'work_number' => '^([0-9]{3}-|)[0-9]{3}-[0-9]{4}$', 'home_number' => '^([0-9]{3}-|)[0-9]{3}-[0-9]{4}$', 'cell_number' => '^([0-9]{3}-|)[0-9]{3}-[0-9]{4}$' ); $cont = 1; while ($cont == 1) { @info = (); foreach $field (@def) { print "$field: "; $_ = <>; chomp; unless ($_) { #if no input then field = 0 push @info, "0"; next; } while (!/$char{$field}/i) { #check format print "error: $_: invalid format: $char{$field}\n"; print "$field: "; $_ = <>; chomp; unless ($_) { push @info, "0"; next; } } push @info, lc($_); } for (2 .. $#info) { #create hash of array of data, key = first nam +e & last name push @{ $book{$info[0] . "&" . $info[1]} }, $info[$_]; } print "another entery? (*/n)"; $_ = <>; chomp; $cont = 0 if /n/i; } print "Save where? "; $file = <>; chomp $file; open (BOOK, ">$file") or (die "Can't open $file: $!"); foreach $name (keys %book) { #write the data $name =~ /(.+)&(.+)/; if ($2 =~ /^0$/) { print BOOK "$1,::"; } else { print BOOK"$1,$2::"; } for ( 0 .. $#{ $book{$name} } ) { print BOOK $def[$_+2] . ":" . $book{$name}[$_] . "::" unless +$book{$name}[$_] =~ /^0$/; #print field name then data unless the fie +ld is empty } print BOOK "\n"; } close(BOOK);

Edit: chipmunk 2002-02-24