Avoiding 'uninitialized value' warning?

Discussion in 'Perl Misc' started by dgp@dodgeit.com, Sep 19, 2005.

  1. Guest

    I recenctly received some help from this group (thanks again) in
    comparing the first two fields of comma delimited data in a text file.
    One of the suggestions used the following line to define a variable
    containing the first two comma delimited fields from a line:

    my $key = join ',', (split /,/ )[0,1];

    This seems to work but if 'use warnings;' is envoked the line gives the
    following warning: Use of uninitialized value in join or string...

    I was able to avoid the warning if I broke up the line and used an
    additional variable:
    my @key = split /,/;
    my $keyvalue = join(',', @key[0,1]);

    The original line looks a more "Perlish" but what is the "proper" way
    to accomplish this and avoid the warnings. Should I turn off 'use
    warnings'?

    Thanks for any help,
    Dave
    , Sep 19, 2005
    #1
    1. Advertising

  2. wrote:
    > ... define a variable
    > containing the first two comma delimited fields from a line:
    >
    > my $key = join ',', (split /,/ )[0,1];
    >
    > This seems to work but if 'use warnings;' is envoked the line gives the
    > following warning: Use of uninitialized value in join or string...
    >
    > I was able to avoid the warning if I broke up the line and used an
    > additional variable:
    > my @key = split /,/;
    > my $keyvalue = join(',', @key[0,1]);


    Please post a short but _complete_ program that demonstrates the
    observed behaviour.

    > The original line looks a more "Perlish" but what is the "proper" way
    > to accomplish this and avoid the warnings. Should I turn off 'use
    > warnings'?


    There is most likely no reason to do so.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Sep 19, 2005
    #2
    1. Advertising

  3. wrote:
    > I recenctly received some help from this group (thanks again) in
    > comparing the first two fields of comma delimited data in a text file.
    > One of the suggestions used the following line to define a variable
    > containing the first two comma delimited fields from a line:
    >
    > my $key = join ',', (split /,/ )[0,1];
    >
    > This seems to work but if 'use warnings;' is envoked the line gives the
    > following warning: Use of uninitialized value in join or string...
    >
    > I was able to avoid the warning if I broke up the line and used an
    > additional variable:
    > my @key = split /,/;
    > my $keyvalue = join(',', @key[0,1]);


    That should give you the same warning.


    > The original line looks a more "Perlish" but what is the "proper" way
    > to accomplish this and avoid the warnings. Should I turn off 'use
    > warnings'?


    You could do it like this:

    my %csv;
    while ( <CSV> ) {
    /,/ or next;
    my $key = join ',', ( split /,/ )[ 0, 1 ];
    $csv{ $key } = $_;
    }
    close CSV;

    while ( <OLD> ) {
    /,/ or next;
    my $key = join ',', ( split /,/ )[ 0, 1 ];
    $_ = $csv{ $key } if exists $csv{ $key };
    print OUT;
    }



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Sep 19, 2005
    #3
  4. Guest

    wrote:
    > I recenctly received some help from this group (thanks again) in
    > comparing the first two fields of comma delimited data in a text file.
    > One of the suggestions used the following line to define a variable
    > containing the first two comma delimited fields from a line:
    >
    > my $key = join ',', (split /,/ )[0,1];
    >
    > This seems to work but if 'use warnings;' is envoked the line gives the
    > following warning: Use of uninitialized value in join or string...
    >
    > I was able to avoid the warning if I broke up the line and used an
    > additional variable:
    > my @key = split /,/;
    > my $keyvalue = join(',', @key[0,1]);
    >
    > The original line looks a more "Perlish" but what is the "proper" way
    > to accomplish this and avoid the warnings. Should I turn off 'use
    > warnings'?


    I'm confused how your workaround avoided the problem. If you're
    splitting a string such as "ab," for instance, which will generate
    only a single element in @key, both solutions will emit a warning.
    (In this particular case, a way to avoid a warning would be to pass
    a limit param. to split, eg., split /,/, "ab,", 2. But then you'd
    have know in advance what kind of string was being split)

    Something like this though would avoid the warning:

    my $keyvalue = join(',', map defined $_ ? $_ : '', @key[0,1]);

    hth,
    --
    Charles DeRykus
    , Sep 20, 2005
    #4
  5. Anno Siegel Guest

    <> wrote in comp.lang.perl.misc:
    > I recenctly received some help from this group (thanks again) in
    > comparing the first two fields of comma delimited data in a text file.
    > One of the suggestions used the following line to define a variable
    > containing the first two comma delimited fields from a line:
    >
    > my $key = join ',', (split /,/ )[0,1];
    >
    > This seems to work but if 'use warnings;' is envoked the line gives the
    > following warning: Use of uninitialized value in join or string...
    >
    > I was able to avoid the warning if I broke up the line and used an
    > additional variable:
    > my @key = split /,/;
    > my $keyvalue = join(',', @key[0,1]);


    As has been noted this doesn't avoid the warning.

    > The original line looks a more "Perlish" but what is the "proper" way
    > to accomplish this and avoid the warnings. Should I turn off 'use
    > warnings'?


    If Perl's default behavior of treating undef as an empty string is what
    you want, that's exactly the way to go. But don't switch off warnings
    altogether. Put "no warnings 'uninitialized'" immediately before the
    offending statement. Take care that this happens in a small block.
    If there isn't one, insert a bare block.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Sep 20, 2005
    #5
    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. Liang
    Replies:
    2
    Views:
    748
    Liang
    Aug 11, 2004
  2. Replies:
    1
    Views:
    3,011
    Jens Thoms Toerring
    May 25, 2008
  3. kermit

    Avoiding IE warning messages

    kermit, Sep 26, 2005, in forum: ASP General
    Replies:
    6
    Views:
    189
    kermit
    Sep 30, 2005
  4. Jon
    Replies:
    4
    Views:
    145
    Paul Lalli
    Sep 19, 2005
  5. neilsolent
    Replies:
    3
    Views:
    132
    Justin C
    Nov 26, 2009
Loading...

Share This Page