Hi,
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?

Thanks,
M


Useless use of array element in void context at L2_Q9.pl line 23.
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.
Odd number of elements in hash assignment at L2_Q9.pl line 23, <STDIN>
line 1.
Use of uninitialized value in concatenation (.) or string at L2_Q9.pl
line 28, <STDIN> line 1.
jim +>


#!/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";
}


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to