CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
uninitialized value - help please.

 



netmonky
Novice

Feb 18, 2011, 3:37 AM

Post #1 of 7 (2417 views)
uninitialized value - help please. Can't Post

Hi all,



I have some data that looks like

name,address,phone,,friend,1,2,3,4

name,address,phone,8,friend,1,2,3,4

I am splitting the values and assigning them to variables in a while loop.

while (<INFILE>) {
my @filelist = split(/,/,$_);
my $name = $filelist[0];
my $add = $filelist[1];
my $phone = $filelist[2];
my $num = $filelist[3]; <<--Use of uninitialized value $filelist[3]
my $friend = $filelist[8];



I keep seeing Use of uninitialized value $filelist[3] when nothing is in field 3 of the array. I can turn warning off and all is good :-) -- but I would like to know the correct way of doing this please.



Thanks in advance!

Mark


(This post was edited by netmonky on Feb 18, 2011, 3:41 AM)


netmonky
Novice

Feb 18, 2011, 5:13 AM

Post #2 of 7 (2410 views)
Re: [netmonky] uninitialized value - help please. [In reply to] Can't Post

sorted this by

my @filelist = split(/,/,$_,10); <<-- added length.

Unsure why though, could anyone explain?


BillKSmith
Veteran

Feb 18, 2011, 5:53 AM

Post #3 of 7 (2408 views)
Re: [netmonky] uninitialized value - help please. [In reply to] Can't Post

The best fix I know is to use an array slice for the assignments. The empty fields are set to a null string. You can set another default with the ||= operator.


Code
use strict; 
use warnings;
while (<DATA>) {
my( $name, $add, $phone, $num, $friend ) = (split /,/)[0,1,2,3,8];
$num ||= 0;
print $name, $add, $phone, $num, $friend, "\n";
}
__END__
name,address,phone,,friend,1,2,3,4
name,address,phone,8,friend,1,2,3,4

Good Luck,
Bill


netmonky
Novice

Feb 18, 2011, 6:07 AM

Post #4 of 7 (2406 views)
Re: [BillKSmith] uninitialized value - help please. [In reply to] Can't Post

Thanks Bill,If I use your method of assigning variables but with a string of say 18 fields, could I specify multiple undef's in one statement, instead of doing:use strict;
use warnings;
while (<DATA>) {
my( $name, $undef, $undef, $add ) = (split /,/);
$num ||= 0;
print $name, $add, "\n";
}
__END__
maybe something like undef(x2) ?


shawnhcorey
Enthusiast


Feb 18, 2011, 6:15 AM

Post #5 of 7 (2405 views)
Re: [netmonky] uninitialized value - help please. [In reply to] Can't Post

The correct code is:

Code
my( $name, undef, undef, $add ) = (split /,/);


I don't think (undef)x2 will work.

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


BillKSmith
Veteran

Feb 18, 2011, 7:25 AM

Post #6 of 7 (2402 views)
Re: [netmonky] uninitialized value - help please. [In reply to] Can't Post

You and Shawn are discussing an alternate to the array slice that I used. The choice between them is clearly a matter of preference. I prefer the array slice because it emphasizes the fields that you want rather than the ones that you do not. The specification of the array slice itself can often be shortened with the use of the range operator.

My original assignemt can be shortened to:


Code
    my( $name, $add, $phone, $num, $friend ) = (split /,/)[0..3,8];

Good Luck,
Bill


netmonky
Novice

Feb 20, 2011, 11:15 AM

Post #7 of 7 (2391 views)
Re: [BillKSmith] uninitialized value - help please. [In reply to] Can't Post

Thanks all :-D. Learning.....

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives