fix a per script



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

.



Relevant Pages

  • Re: Perl Script - Not Getting Results When Using Command Line Arguments
    ... @SORTED_DIRLIST = sort @DIRLIST; ... # foreach $result ... The only section that works is the first one with no command line ... you should also move the actual work your script does into subroutines. ...
    (perl.beginners)
  • Which line?
    ... I have a script here, I have been going over and over. ... Every time I run it I get several errors, and I have tried to fix them to no avail. ... Sort names by first name\n"; ... foreach $name (@keys) { ...
    (perl.beginners)
  • Re: return sorted hashes from subroutines
    ... I am trying to write script to retrieve info from a file that looks like: ... but I am struggling to sort the hash. ... foreach my $e{ ...
    (perl.beginners)
  • Re: Hang with powerd/powernow with SMP enabled
    ... Uwe Laverenz schrieb: ... the rc script. ... I see that a fix for that sort of ...
    (freebsd-stable)
  • Another Dereference Post by me
    ... I've learned quite a bit messing with this script over the last ... Now I'm trying to sort the resulting ... duplicate values in that column. ... foreach my $line { ...
    (comp.lang.perl.misc)

Loading