F
fred78980
I have this script that sort and erase duplicate. When I run the
script I receive this message and I am not able to fix it.
Use of uninitialized value in hash element at ./pgm1.pl line 21,
<DATA> line 10.
#title -vegetables-#
Use of uninitialized value in concatenation (.) or string at ./pgm1.pl
line 48, <DATA> line 10.
=>
Please help me to fix it.
Thanks
#!/usr/bin/perl
use warnings;
use strict;
my $title;
my %titles;
my $key;
my $val;
my $rec;
while(<DATA>){
chomp;
if (substr($_,0,1) eq "#") {
$title = $_;
$titles{$title} = [];
}
else {
my ($key,$val) = split(/\s*=>\s*/);
$rec = {};
$rec->{$key}=$val;
push (@{$titles{$title}},$rec);
}
}
sub cmpkey
{
if(((keys %$a)[0] cmp (keys %$b)[0])==0)
{
return (values %$a)[0] cmp (values %$b)[0];
}
else
{
return (keys %$a)[0] cmp (keys %$b)[0];
}
}
foreach $title (keys (%titles))
{
print "$title\n";
for $rec (sort cmpkey @{$titles{$title}})
{
foreach $key (keys (%$rec))
{
foreach $val (values (%$rec))
{
print "$key => $val\n";
}
}
}
}
__DATA__
#title -fruit-#
red => strawberry
orange => orange
red => apple
orange => carot
red => cherry
#title -vegetables-#
green => cucumber
red => tomatoes
script I receive this message and I am not able to fix it.
Use of uninitialized value in hash element at ./pgm1.pl line 21,
<DATA> line 10.
#title -vegetables-#
Use of uninitialized value in concatenation (.) or string at ./pgm1.pl
line 48, <DATA> line 10.
=>
Please help me to fix it.
Thanks
#!/usr/bin/perl
use warnings;
use strict;
my $title;
my %titles;
my $key;
my $val;
my $rec;
while(<DATA>){
chomp;
if (substr($_,0,1) eq "#") {
$title = $_;
$titles{$title} = [];
}
else {
my ($key,$val) = split(/\s*=>\s*/);
$rec = {};
$rec->{$key}=$val;
push (@{$titles{$title}},$rec);
}
}
sub cmpkey
{
if(((keys %$a)[0] cmp (keys %$b)[0])==0)
{
return (values %$a)[0] cmp (values %$b)[0];
}
else
{
return (keys %$a)[0] cmp (keys %$b)[0];
}
}
foreach $title (keys (%titles))
{
print "$title\n";
for $rec (sort cmpkey @{$titles{$title}})
{
foreach $key (keys (%$rec))
{
foreach $val (values (%$rec))
{
print "$key => $val\n";
}
}
}
}
__DATA__
#title -fruit-#
red => strawberry
orange => orange
red => apple
orange => carot
red => cherry
#title -vegetables-#
green => cucumber
red => tomatoes