Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-20 01:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found