Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Set Array Columns to 0

by Anonymous Monk
on Jul 05, 2012 at 16:03 UTC ( [id://980102]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks, I am attempting to set the value of blank elements in an array to zero. How can I do this? The input looks like this:

Thurs,July,5,2012,123,456,789 Thurs,July,5,2012,123,456,789,Fri,July,6,2012

Here is my code

my $line; my ($dow,$mon,$day,$year,$num1,$num2,$num3,$dow2,$mon2,$day2,$year2) = + ""; foreach $line (@array){ ($dow,$mon,$day,$year,$num1,$num2,$num3,$dow2,$mon2,$day2,$year2) += split /[,]/, $line; print "$dow2\n";

I would like to be able to print $dow2 through $year2 but I am getting the error "Use of uninitialized value $dow2 in print..." What am I doing wrong here?

Replies are listed 'Best First'.
Re: Set Array Columns to 0
by jethro (Monsignor) on Jul 05, 2012 at 16:41 UTC

    Your second line will only initialize the first variable $dow with "", all the other variables will be set to undef

    The simplest code to make it work is to extend the list on the right hand side of line 4 so that it will always be enough to fill all variables even if split would return the empty list:

    ($dow,$mon,$day,$year,$num1,$num2,$num3,$dow2,$mon2,$day2,$year2)= ( s +plit(/[,]/, $line),("") x 11 );

    Note that 'x' is an operator that in this case produces a list of 11 empty strings. Naturally you could also use 0 instead of "".

      Is there any way to fill in the missing area (the top line where the date is) with comma-separated 0's? I tried your example and it does not print with zeros. Thank you.

        Just replace the quotes in ("") x 11 with your placeholder of choice.
Re: Set Array Columns to 0
by toolic (Bishop) on Jul 05, 2012 at 16:33 UTC
    Your 1st input line only has 7 columns, but $dow2 corresponds to the 8th column. You get the warning message because split sets the 8th-11th columns to the undefined value. Perhaps you want:
    print "$dow2\n" if defined $dow2;
Re: Set Array Columns to 0
by brx (Pilgrim) on Jul 05, 2012 at 16:41 UTC

    When you split the line, the result can be a list which contains less elements than you are expecting.

    Thurs,July,5,2012,123,456,789 : here you'll obtain 7 elements, no more.

    So, do not trust the input and don't forget to chomp your lines.

    Here, I'm using an additional list of 11 empty string ("" is not undef) to be sure that all (11) variables are defined.
    #!perl use strict; use warnings; my @array=<DATA>; chomp @array; my $line; my ($dow,$mon,$day,$year,$num1,$num2,$num3,$dow2,$mon2,$day2,$year2); foreach $line (@array){ ($dow,$mon,$day,$year,$num1,$num2,$num3,$dow2,$mon2,$day2,$year2) += ((split /[,]/, $line),("")x11); print "$dow2\n"; } __DATA__ Thurs,July,5,2012,123,456,789 Thurs,July,5,2012,123,456,789,Fri,July,6,2012

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-19 10:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found