Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

parsing data in a file

by pinnacle (Acolyte)
on Nov 02, 2010 at 14:08 UTC ( #868994=perlquestion: print w/replies, xml ) Need Help??

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

Script
1 #!/usr/bin/perl -w 2 3 use strict; 4 16 while (my $input = <DATA>){ 17 chomp($input); 18 my ($name,$num,$addr,$date,$n) = split(":", $input); 19 my ($fname,$lname) = split(" ",$name); 20 print "$lname\n" if $lname =~ /^ker/; 21 } 22 print "\n"; 23 24 25 __DATA__ 26 Tommy Savage: 40872 40140: 12 2 2 Oxbow Court, Sunnyvale, CA 940 +87: 5/19/66: 34200 27 Lesle Kerstin: 4084561234: 4 Harvard Square, Boston, MA 02133: 4 +/22/62: 52600 28 JonDeLoach: 4082533 122: 12 3 Park St. , San Jose, CA 94086: 7/2 +5/53: 85100 29 Ephram Hardy: 2932595395: 2 3 5 Carlton Lane, Joliet, IL 73858: +8/12/20: 56700 30 Betty Boop: 24583683 57: 63 5 Cutesy Lane, Hollywood, CA 91464: +6/23/23: 14500 31 William Kopf: 84683 62837: 693 7 Ware Road, Milton, PA 93756: 9/ +21/46: 43500 32 Norma Corder: 3978572735: 74 Pine Street, Dearborn, MI 23874: 3/ +28/45: 245700 33 James Ikeda: 8349388376: 2 3 445 Aster Ave. , Allentown, NJ 8374 +5: 12/1/38: 45000 34 Lori Gortz: 3278325728: 3465 Mirlo Street, Peabody, MA 34756: 10 +/2/65: 35200 35 Barbara Kerz: 38557 3 8326: 83 2 Ponce Drive, Gary, IN 83756: 12 +/15/46: 268500
Result
:~/pbe/chap8$ ./exer1.pl Use of uninitialized value $lname in pattern match (m//) at ./exer1.pl + line 20, <DATA> line 3. Use of uninitialized value $name in split at ./exer1.pl line 19, <DATA +> line 11. Use of uninitialized value $lname in pattern match (m//) at ./exer1.pl + line 20, <DATA> line 11. Use of uninitialized value $name in split at ./exer1.pl line 19, <DATA +> line 12. Use of uninitialized value $lname in pattern match (m//) at ./exer1.pl + line 20, <DATA> line 12. Use of uninitialized value $name in split at ./exer1.pl line 19, <DATA +> line 13. Use of uninitialized value $lname in pattern match (m//) at ./exer1.pl + line 20, <DATA> line 13. Use of uninitialized value $name in split at ./exer1.pl line 19, <DATA +> line 14. Use of uninitialized value $lname in pattern match (m//) at ./exer1.pl + line 20, <DATA> line 14. Use of uninitialized value $name in split at ./exer1.pl line 19, <DATA +> line 15. Use of uninitialized value $lname in pattern match (m//) at ./exer1.pl + line 20, <DATA> line 15.
Question

I am very confused with uninitialized errors, can anyone explain how to fix it. thanks!!

Replies are listed 'Best First'.
Re: parsing data in a file
by tirwhan (Abbot) on Nov 02, 2010 at 14:12 UTC

    Take a close look at the error messages and the lines in your data the error messages correspond to. You'll see that on line 3 of your data the name is JonDeLoach, so there is no whitespace for your second split to split on, and hence no data in $lname. As for lines 11 and subsequent of your data...well, I'm sure you can easily see what's wrong with them.


    All dogma is stupid.
Re: parsing data in a file
by johngg (Canon) on Nov 02, 2010 at 15:06 UTC

    Note also that you are trying to print a last name if it matches the regex /^ker/ but regexen are case-sensitive. Unless you either supply the i flag (/^ker/i) to ignore case or make your pattern more accurate (/^Ker/) you will never get a match as your last names start with an upper-case letter.

    Cheers,

    JohnGG

Re: parsing data in a file
by talexb (Chancellor) on Nov 02, 2010 at 16:46 UTC

    You should be programming defensively here:

    my ($fname,$lname) = split(" ",$name);

    Instead of that, I'd recommend

    # If there are two names separated by a space .. my ($fname,$lname); if ( $name =~ /\w+\s\w+/ ) { # Separate them into first and last names. ($fname,$lname) = split(" ",$name); } else { # Otherwise, decide what to do with just a single name. # Use the name as a last name? Die? Ignore? }
    Don't assume that your input's always going to be clean.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re: parsing data in a file
by kcott (Archbishop) on Nov 02, 2010 at 14:54 UTC

    Take a look at split - the first argument is actually a regular expression.

    Looking at your data, split(":", $input) should probably be split(/: /, $input) (i.e. a colon followed by a space).

    -- Ken

      And as sometimes TAB characters hide themselves as whitespace, I usually write such regexen as /:\s+/

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        Good point, CountZero. And a requirement in more complex REs that you've spaced out for readability: / : \s+ /x (a bit over-the-top for that example but it shows the intent :-)

        -- Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://868994]
Approved by kennethk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2022-09-30 12:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (126 votes). Check out past polls.

    Notices?