uninitialized value

J

jan09876

Hi,
I try to learn perl by myself without any programming background. I
try to transform my DATA to
#title -fruit-#
orange => orange, carot,
red => apple, cherry, strawberry
#title -vegetables-#
green => cucumber
red => tomatoes

I keep on receiving this message Use of uninitialized value in join
or string at ./print.pl line 18, <DATA> line 8. and don't know what to
do to fix the problem.

#!/usr/bin/perl
use warnings;
use strict;
my $title = "#.*#";
my %hash;

while(<DATA>) {
chomp;
{
my($key,$val) = split(/\s*=>\s*/);
push(@{$hash{$key}},$val);
}

for my $key (sort keys %hash) {
print "$key => @{$hash{$key}}\n";
print $titles;
}
}

__DATA__
#title -fruit-#
orange => orange
orange => carot
red => apple
red => cherry
red => strawberry
#title -vegetables-#
green => cucumber
red => tomatoes
 
T

Tad McClellan

I try to learn perl by myself without any programming background.

I keep on receiving this message


No you don't.

You receive this message:

Global symbol "$titles" requires explicit package name at ./print.pl line 16.
Execution of ./print.pl aborted due to compilation errors.

Please do not re-type Perl code, use copy/paste or your editor's
"import" function rather than attempting to type in your code. If you
make a typo you will get followups about your typos instead of
about the question you are trying to get answered.

You inserted a typo that cannot be in your real program.

We need to see *exactly* what code you're running if we are to
help you debug it.

Use of uninitialized value


That means you are using the special "undef" value as if
it had been given a real value.

in join
or string at ./print.pl line 18,


Is that really the line number?

Because I cannot duplicate that...

(or did your re-typing change line numbers too?)

<DATA> line 8. and don't know what to
do to fix the problem.


First you need to *find* the problem. Then you fix it.

Every message that perl might issue is documented in

perldoc perldiag

so you should get in the habit of looking up your messages in there.

#!/usr/bin/perl
use warnings;
use strict;


This is most excellent!

You are already ahead of most newbie Perl programmers.

my $title = "#.*#";


You never change the value of that variable in your code, it is
a variable that does not vary!

Is that what you meant to do?

my %hash;

while(<DATA>) {
chomp;
{
my($key,$val) = split(/\s*=>\s*/);


What will end up in $val for your "title" lines?

This is the cause of the warnings you are getting.

You need to treat the title lines differently than the data lines.

push(@{$hash{$key}},$val);
}

for my $key (sort keys %hash) {
print "$key => @{$hash{$key}}\n";
print $titles;
^
^

The program you posted will not even run, so it cannot be issuing
uninitialized warnings because those happen at runtime.
 
T

tuxdna

Hi,
I try to learn perl by myself without any programming background. I
try to transform my DATA to
#title -fruit-#
orange => orange, carot,
red => apple, cherry, strawberry
#title -vegetables-#
green => cucumber
red => tomatoes

I keep on receiving this message Use of uninitialized value in join
or string at ./print.pl line 18, <DATA> line 8. and don't know what to
do to fix the problem.

#!/usr/bin/perl
use warnings;
use strict;
my $title = "#.*#";
my %hash;

while(<DATA>) {
chomp;
{
my($key,$val) = split(/\s*=>\s*/);
push(@{$hash{$key}},$val);
}

for my $key (sort keys %hash) {
print "$key => @{$hash{$key}}\n";
print $titles;

}
}

__DATA__
#title -fruit-#
orange => orange
orange => carot
red => apple
red => cherry
red => strawberry
#title -vegetables-#
green => cucumber
red => tomatoes


Check this one. This Works.

#!/usr/bin/
perl
use
warnings;
use
strict;

my %hash=();

while(<DATA>)
{
if ( /^
\#/ )
{
for my $key (sort keys
%hash)

{
print "$key => ", join(", " ,
@{$hash{$key}}),"\n";
}
print
$_;

%hash=();

next;
}

chomp;

my($key,$val) = split(/\s*=>
\s*/);
push( @{$hash{$key}} ,
$val );
}

for my $key (sort keys
%hash)
{
print "$key => ", join(", " ,
@{$hash{$key}}),"\n";
}

__DATA__
#title -fruit-
#
orange =>
orange
orange => carot
red =>
apple
red =>
cherry
red =>
strawberry
#title -vegetables-
#
green =>
cucumber
red => tomatoes
 
J

Joe Smith

tuxdna said:
Check this one. This Works.

No, it doesn't.
#!/usr/bin/
perl

linux% perl temp.pl
Can't exec /usr/bin/ at temp.pl line 1.

After fixing the first line:

linux% perl temp
Use of uninitialized value in join or string at temp line 41, <DATA> line 16.
Use of uninitialized value in join or string at temp line 41, <DATA> line 16.
# => ,
Use of uninitialized value in join or string at temp line 41, <DATA> line 16.
#title -fruit- =>
Use of uninitialized value in join or string at temp line 41, <DATA> line 16.
#title -vegetables- =>
Use of uninitialized value in join or string at temp line 41, <DATA> line 16.
apple =>
Use of uninitialized value in join or string at temp line 41, <DATA> line 16.
cherry =>
Use of uninitialized value in join or string at temp line 41, <DATA> line 16.
cucumber =>


Whatever you used for posting really screwed up the formatting of the text.
What we received is apparently not the same as what you thought you posted.
-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top