mark baumeister wrote:
Hi,
Hello,
I am trying to move array elements (populated from the<STDIN>) into a
hash
as pairs [i] and [i + 1] and then print them out using the code below.
If I enter "bob" as the first element and hit enter I get the
error messages below. I guess there are multiple problems with my
code.
For one it appears that my $kv variable is supposed to take numeric
values. I'm not sure if this is the reason the program aborts once
I hit enter after my first entry (i.e. "bob").
Any hints on why I my $kv varible is expected to be numeric
or any other obvious problems with my code?
Useless use of array element in void context at L2_Q9.pl line 23.
That is from the line:
%hash = $kv[$i] => $kv[$i + 1];
You get that because the '=' operator has higher precedence than the
'=>' operator and perl sees it as:
( %hash = $kv[$i] ) => $kv[$i + 1];
And the array element $kv[$i + 1] is just sitting there in void context.
You need to use parentheses around a list on the right-hand side of the
'=' operator:
%hash = ( $kv[ $i ], $kv[ $i + 1 ] );
input key/value pairs: first a key then return, then a value then
return, etc. To stop entering key/value pairs type 'stop'
jim
Argument "stop" isn't numeric in numeric eq (==) at L2_Q9.pl line 17,
<STDIN> line 1.
Argument "jim" isn't numeric in numeric eq (==) at L2_Q9.pl line 17,
<STDIN> line 1.
You get those to from this line:
last if ($kv == "stop");
Where $ky contains "jim". You are trying to compare two non-numeric
values with a numeric comparison operator so perl warns you that they
are non-numeric. The result of this is that perl will convert "jim" and
"stop" to the number 0 before comparing.
You have to use the string comparison operators to compare two strings.
Odd number of elements in hash assignment at L2_Q9.pl line 23,<STDIN>
line 1.
That is also from the line:
%hash = $kv[$i] => $kv[$i + 1];
Where you are only assigning $kv[$i] to %hash and 1 is an odd number.
Use of uninitialized value in concatenation (.) or string at L2_Q9.pl
line 28,<STDIN> line 1.
That is from the line:
print "$key +> $value\n";
Because %hash has been assigned an odd number of elements that means
that one $value is uninitialized which gives this warning.
#!/usr/bin/perl -w
use strict;
my %hash;
my $kv;
my @kv;
my @v;
my $i;
my $key;
my $value;
#create key - value pairs to go into a hash by first entering each
into a list @k or @v
print "input key/value pairs: first a key then return, then a value
then return, etc. To stop entering key/value pairs type 'stop'\n";
while ($kv =<STDIN>) {
chomp($kv);
push(@kv, $kv);
last if ($kv == "stop");
}
# move each key or value located at each index of array @kv into a
hash so that they pair up e.g. $kv[i] with $kv[i +1] etc.
for($i = 0; $i< $#kv + 1; $i++) {
%hash = $kv[$i] => $kv[$i + 1];
$i++;
}
while ( ($key, $value) = each %hash ) {
print "$key +> $value\n";
}
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/