Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Creating report

by catfish1116 (Beadle)
on Sep 08, 2020 at 19:07 UTC ( [id://11121490]=perlquestion: print w/replies, xml ) Need Help??

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

I am taking a file and trying to create report Data is Name Street Zip and I want each to appear on it's own line: Name: Fred Street: Green Zip 55505 Below is the code and error message.

#! /usr/bin/perl use v5.16.3; use strict; use warnings; use diagnostics; ######################################################### ## # ## 9/4/20 # ## Program takes white space separated values and # ## produces a report for each line # ## # ## # ######################################################### chomp( my $header = <> ); my @field_names = split /\s+/, $header; while( <>) { chomp; my @fields = split /\s+/; foreach my $index (0 .. $#fields ) { print "$field_names[$index]: $fields[$index]\n" } } Use of uninitialized value within @field_names in concatenation (.) or + string at ./Exercise_9_6.pl line 26, <> line 3 (#1)

TIA The Catfish </post>

Replies are listed 'Best First'.
Re: Creating report
by Fletch (Bishop) on Sep 08, 2020 at 19:29 UTC

    You have more fields in a line (at least one) than you have columns in the header line. Just a WAG since you've not given any useful sample data but my guess is that you've got spaces in (say) what you expect to be coming as a single column (e.g. you have a street name with embedded spaces and your line is something like Fred New Sesame 12345). When you split that line you're getting four values in @fields and the undef error because you're accessing $field_names[3] which doesn't exist if your header line is Name Street Zip.

    Problem is that your data format isn't the greatest so there's not really an obvious suggestion how to fix it. Theoretically you might presume the first and last fields are Name and Zip and treat however many appear in the middle as Street, but if your data has Names with embedded spaces that's going to fail as well. Better would be to step back and get your input data into a more amenable format (e.g. CSV wouldn't be bad (EDIT: and then use Text::CSV_XS or the like to parse it)).

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Creating report
by davido (Cardinal) on Sep 08, 2020 at 20:12 UTC

    Try the following code:

    chomp( my $header = <> ); my @field_names = split /\s+/, $header; my $expected_field_count = scalar(@field_names); while( <>) { chomp; my @fields = split /\s+/; my $field_count = scalar(@fields); if ($field_count > $expected_field_count)) { warn "Input error: <<<$_>>> has $field_count fields, but $expe +cted_field_count were expected.\n", 'Fields obtained were: (', join(')(', @fields), ')'; } foreach my $index (0 .. $#fields ) { print "$field_names[$index]: $fields[$index]\n" } }

    You will find at least one of your lines of input produces a rather verbose warning. It is likely that this is the result of not handling some special case as you unpack your input. Often the best way of dealing with these sorts of issues is to use an input parser that is known to handle edge cases predictably. Text::CSV or its brother Text::CSV_XS would be a good choice. However, you're working on "Exercise_9_6", so it could be your instructor prefers that you not use an off-the-shelf CSV parser. In that case, you'll have to explore the fun of escaping field delimiters through metacharacters such as backslash, or through quote constructs. That's not much fun, which is why we like to use off-the-shelf parsers.

    While you're here, you should write tests for your parser, and this edge case should be included in your tests.


    Dave

Re: Creating report
by tobyink (Canon) on Sep 08, 2020 at 19:29 UTC

    You have more fields in one of your lines of data (line 3 in particular) than you did on your headers line.

    Without seeing the data, my guess is that this is because you're splitting on white space, and some of the values contain spaces. Person names and street names often do contain spaces.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (1)
As of 2024-04-25 02:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found