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:
warning message but code works anyway?

 



Wildcard
Novice

Oct 16, 2017, 6:39 AM

Post #1 of 5 (572 views)
warning message but code works anyway? Can't Post

Hello there! :)
My compiler spits out a warning message but somehow the codes works fine anyway. The warning is:
Use of uninitialized value in concatenation (.) or string [..] at line 34

the code is the following:

Code
use strict;  
use warnings;

my $scal = "Hello";
my @array = ( 1, 2, 3);
my %hash = ("1" => "one", "2" => "two", "3" => "three", );


dumpit($scal);
dumpit(\@array);
dumpit(\%hash);

sub dumpit

{
my $ref = shift;
if (ref($ref) eq 'HASH')
{
print "HASH:\n";
foreach (sort (keys %{$ref}))
{
print "\t $_ => $ref->{$_} \n";
}
}

elsif (ref($ref) eq 'ARRAY')
{
print "ARRAY:\n";
for my $i (0 .. @{$ref})
{
print "\t $i: $ref->[$i] \n";
}
}

else
{
print "SKALAR:\n";
print "\t ${ref}\n";
}
}


It gives out the following:

Quote
SKALAR:
Hello
ARRAY:
0: 1
1: 2
2: 3
Use of uninitialized value in concatenation (.) or string at C:\Strawberry\perl\allerlei.pl line 34.
3:
HASH:
1 => one
2 => two
3 => three

I'm not sure but perhaps I neglected to define a boundary for the array iteration or something like that. Weird, the code works, but somehow I managed to mess up anyway. COuld someone explain what it does mean?


(This post was edited by Wildcard on Oct 16, 2017, 6:41 AM)


BillKSmith
Veteran

Oct 16, 2017, 6:55 AM

Post #2 of 5 (566 views)
Re: [Wildcard] warning message but code works anyway? [In reply to] Can't Post

Your problem is with ARRAY. You are trying to print one element past the end of the array. (Note the '3:' in your output.)

Code
C:\Users\Bill\forums\guru>type wildcard3.pl 
use strict;
use warnings;

my $scal = "Hello";
my @array = ( 1, 2, 3);
my %hash = ("1" => "one", "2" => "two", "3" => "three", );


dumpit($scal);
dumpit(\@array);
dumpit(\%hash);

sub dumpit

{
my $ref = shift;
if (ref($ref) eq 'HASH')
{
print "HASH:\n";
foreach (sort (keys %{$ref}))
{
print "\t $_ => $ref->{$_} \n";
}
}

elsif (ref($ref) eq 'ARRAY')
{
print "ARRAY:\n";
# for my $i (0 .. @{$ref})
for my $i (0 .. @{$ref}-1)
{
print "\t $i: $ref->[$i] \n";
}
}

else
{
print "SKALAR:\n";
print "\t ${ref}\n";
}
}

C:\Users\Bill\forums\guru>perl wildcard3.pl
SKALAR:
Hello
ARRAY:
0: 1
1: 2
2: 3
HASH:
1 => one
2 => two
3 => three

Good Luck,
Bill


Wildcard
Novice

Oct 16, 2017, 7:02 AM

Post #3 of 5 (565 views)
Re: [BillKSmith] warning message but code works anyway? [In reply to] Can't Post

thanks! Soooo... the missing -1 was the problem here. Just why though? Cannot really grasp it Unsure


BillKSmith
Veteran

Oct 16, 2017, 8:11 AM

Post #4 of 5 (561 views)
Re: [Wildcard] warning message but code works anyway? [In reply to] Can't Post

Your symbol @{$ref} returns the size of the array (3 elements) They have indexes 0, 1, and 2. Your for-loop processes indexes 0..@{$ref) which means 0..3 or (0, 1, 2, and 3). Index 3 is undefined, hence your error. With the -1, you correctly process elements 0..2.

You may prefer to use:

Code
         for my $i(0..$#{$ref})


This mean use 0 through the highest index (2).
Good Luck,
Bill


Wildcard
Novice

Oct 16, 2017, 8:30 AM

Post #5 of 5 (559 views)
Re: [BillKSmith] warning message but code works anyway? [In reply to] Can't Post

oh, of course, that's s clear now! Thanks

 
 


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

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