Displaying variables-list

P

Peter Pippinger

Hello NG,

I´m very new to perl. I have a list in %info. It has allways two parts: NAME and
VALUE.
For example:
name1,value1,name2,value2,name3,value3... and so on.


If i do the following...
**
foreach $test (%info){
print "$test \n";
}
**
....all the content of %info will be shown. But i need to have the content
devided into the two variables. So that i can do something like that:

LOOPBEGINNING
print "$name $value";
LOOPEND

How can i do that?

Thanks for any hints!
Peter
 
G

Gunnar Hjalmarsson

Peter said:
I have a list in %info. It has allways two parts: NAME and VALUE.
For example:
name1,value1,name2,value2,name3,value3... and so on.

If i do the following...
**
foreach $test (%info){
print "$test \n";
}
**
...all the content of %info will be shown. But i need to have the
content devided into the two variables. So that i can do something
like that:

LOOPBEGINNING
print "$name $value";
LOOPEND

How can i do that?

You are asking a frequently asked question. You should check out the
Perl FAQ before posting here.

The answer to your question can be found at:
http://www.perldoc.com/perl5.8.0/pod/perlfaq4.html#Data--Hashes-(Associative-Arrays)
 
T

Tad McClellan

Peter Pippinger said:
If i do the following...
**
foreach $test (%info){
print "$test \n";
}
**
...all the content of %info will be shown. But i need to have the content
devided into the two variables. So that i can do something like that:

LOOPBEGINNING
print "$name $value";
LOOPEND

How can i do that?


perldoc -f each
 
D

David Oswald

Peter Pippinger said:
Hello NG,

I´m very new to perl. I have a list in %info. It has allways two parts: NAME and
VALUE.
For example:
name1,value1,name2,value2,name3,value3... and so on.


If i do the following...
**
foreach $test (%info){
print "$test \n";
}
**
...all the content of %info will be shown. But i need to have the content
devided into the two variables. So that i can do something like that:
Hmm, I could expand on your code snippet as follows:
my $bool;
foreach $test ( %info ) {
next if ++$bool % 2;
print $test, "\n";
}

But instead of kludging your way through it, I recommend RTFM; particularly
the part about perldoc -f keys and perldoc -f values.
 
J

Jürgen Exner

Peter said:
I´m very new to perl. I have a list in %info.

No, you don't.
You have a hash %info. A hash is a mapping from a domain (usually called
keys) into a codomain (usually called values) and is something quite
different then a list.
It has allways two
parts: NAME and VALUE.
For example:
name1,value1,name2,value2,name3,value3... and so on.


If i do the following...
**
foreach $test (%info){

You are using a hash in list context. Usually this indicates you are using
the wrong data structure and in general it is a bad idea unless you know
exactly what you are doing.
print "$test \n";
}
**
...all the content of %info will be shown. But i need to have the
content devided into the two variables. So that i can do something
like that:

LOOPBEGINNING
print "$name $value";
LOOPEND

How can i do that?

Maybe all you are looking for is the keys() function (untested):

foreach $test (keys(%info)){
print "$test $info{$test} \n";
}

For further details please see the man page "perldoc -f keys".

jue
 
S

Steve Grazzini

Andrew Shitov said:
Threre are two keywords in perl: keys and values. In the code abouve you
try to use a hash in array context. Not good idea ;-) 'Foreach' requires
array, so give it to it.

Actually -- foreach() operates on a list, not an array. And a %hash
in list (not "array") context is the list of all the key/value pairs
flattened together.
keys %info returns an array of all the keys of %hash. Likewise values
%hash produses a list of all of its values.

So, you should run something like this:

foreach my $key (keys %info){
print "$key=$info{$key}\n";
}

Right. If you're not going to sort the output, you could also use
while() and each().

while (my ($k,$v) = each %info) {
print "$k => $v\n";
}
 

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
474,142
Messages
2,570,818
Members
47,362
Latest member
eitamoro

Latest Threads

Top