move array elements to hash

move array elements to hash

am 14.04.2011 21:29:48 von mark baumeister

Hi,
I am trying to move array elements (populated from the ) 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,
line 1.
Argument "jim" isn't numeric in numeric eq (==) at L2_Q9.pl line 17,
line 1.
Odd number of elements in hash assignment at L2_Q9.pl line 23,
line 1.
Use of uninitialized value in concatenation (.) or string at L2_Q9.pl
line 28, 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 = ) {
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-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: move array elements to hash

am 16.04.2011 14:51:48 von Shawn H Corey

On 11-04-14 03:29 PM, mark baumeister wrote:
> #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 =) {
> chomp($kv);
> push(@kv, $kv);
> last if ($kv == "stop");
> }
>

Have you tried:

%hash = @kv;


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

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

Re: move array elements to hash

am 16.04.2011 20:02:20 von Rob Dixon

On 14/04/2011 20:29, mark baumeister wrote:
> Hi,
> I am trying to move array elements (populated from the) 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,
> line 1.
> Argument "jim" isn't numeric in numeric eq (==) at L2_Q9.pl line 17,
> line 1.
> Odd number of elements in hash assignment at L2_Q9.pl line 23,
> line 1.
> Use of uninitialized value in concatenation (.) or string at L2_Q9.pl
> line 28, 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 =) {
> chomp($kv);
> push(@kv, $kv);
> last if ($kv == "stop");

The reason you are getting these errors is because you are using a
numeric comparison. Write

last if $kv eq "stop";

for a string comparison.

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

Your loop should read

for ($i = 0; $i < @kv; $i += 2) {
$hash{$kv[$i]} = $kv[$i + 1];
}

but, as Shawn says, it is a dreadful way to populate a Perl hash.

%hash = @kv;

works fine as long as the data in the array are ordered as you
describe, and there is an even number of elements.

Rob

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

Re: move array elements to hash

am 17.04.2011 03:04:36 von jwkrahn

mark baumeister wrote:
> Hi,

Hello,

> I am trying to move array elements (populated from the) 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,
> line 1.
> Argument "jim" isn't numeric in numeric eq (==) at L2_Q9.pl line 17,
> 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,
> 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, 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 =) {
> 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-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: move array elements to hash

am 17.04.2011 19:22:44 von Jim Gibson

At 12:29 PM -0700 4/14/11, mark baumeister wrote:
>Hi,
>I am trying to move array elements (populated from the ) 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,
> line 1.
>Argument "jim" isn't numeric in numeric eq (==) at L2_Q9.pl line 17,
> line 1.
>Odd number of elements in hash assignment at L2_Q9.pl line 23,
>line 1.
>Use of uninitialized value in concatenation (.) or string at L2_Q9.pl
>line 28, 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 = ) {
> chomp($kv);
> push(@kv, $kv);
> last if ($kv == "stop");


You should reverse the order of the above two lines. You should be
testing $kv for 'stop' BEFORE pushing it onto the array. Otherwise,
you are left with 'stop' as the last element of the array, probably
not what you want.

>}
>
># 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];

You are overwriting the hash each iteration through the loop. Add
elements to your hash this way:
$hash{$kv[$i]} = $kv[$i+1];

> $i++;

You can put '$i += 2' in your for loop statement instead of $i++ and
eliminate this line.

>}

Or you can just try this:

%hash = @kv;

>
>while ( ($key, $value) = each %hash ) {
> print "$key +> $value\n";
>}


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