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


In reply to uninitialized value in phonebook program by smgfc

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.