Avoiding 'uninitialized value' warning?

D

dgp

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
 
G

Gunnar Hjalmarsson

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

John W. Krahn

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
 
C

ced

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

Anno Siegel

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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top