fix a per script

Discussion in 'Perl Misc' started by fred78980@yahoo.com, Jul 30, 2008.

  1. Guest

    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
     
    , Jul 30, 2008
    #1
    1. Advertising

  2. wrote:
    > 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;


    next if /^\s*$/; # ignore blank lines

    >
    > 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
    >


    The problem is the blank line at the end. This is read as data and then
    split on \s*=>\s* which fils as the blank line does not contain =>. From
    then on $val is undefined - leading to the complaints from Perl.


    --
    RGB
     
    RedGrittyBrick, Jul 30, 2008
    #2
    1. Advertising

  3. John Bokma Guest

    RedGrittyBrick <> wrote:

    >> while(<DATA>){
    >>
    >> chomp;

    >
    > next if /^\s*$/; # ignore blank lines


    You might want to do that before the chomp.

    --
    John
     
    John Bokma, Jul 30, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S01aX3N0YXRl?=

    Quick one - Is SESSION per browser instance or per IP Address?

    =?Utf-8?B?S01aX3N0YXRl?=, Apr 4, 2006, in forum: ASP .Net
    Replies:
    7
    Views:
    5,949
    gerry
    Apr 10, 2006
  2. Razvan
    Replies:
    1
    Views:
    443
    tony vee
    Sep 10, 2004
  3. Xah Lee
    Replies:
    22
    Views:
    1,189
    Tim Roberts
    Mar 21, 2006
  4. Xah Lee
    Replies:
    23
    Views:
    1,136
    Tim Roberts
    Mar 21, 2006
  5. Xah Lee
    Replies:
    21
    Views:
    856
    Tim Roberts
    Mar 21, 2006
Loading...

Share This Page