naming a variable with the datum from another variable

D

Duncan Harris

I need to create a separate associative array for each column of data
that I have. However, the number of columns varies. I wish to name
these arrays systematically, %x1, %x2 %x3 etc.

I wish to use a for loop to go through each of these in order and
process them.

How can I write the name of the variable %x# where # represents is the
counter for the loop?

(I know about for loops already and am writing a script).

Thanks in advance for any help.
 
G

Gunnar Hjalmarsson

Duncan said:
I need to create a separate associative array for each column of data
that I have. However, the number of columns varies. I wish to name
these arrays systematically, %x1, %x2 %x3 etc.

I wish to use a for loop to go through each of these in order and
process them.

How can I write the name of the variable %x# where # represents is the
counter for the loop?

You can make the associative arrays anonymous, and access them through a
hash of hashes:

my %col;
$col{"x$_"} = {} for 1..5;
 
A

Anno Siegel

Duncan Harris said:
I need to create a separate associative array for each column of data
^^^^^^^^^^^^^^^^^
That's how they're named in awk and, yes, in some older Perl literature
too. These days, the common name is "hash".
that I have. However, the number of columns varies. I wish to name
these arrays systematically, %x1, %x2 %x3 etc.

No, you don't. Whenever it looks like you want numbered variable names,
what you really want is a variable with numbered values: an array. In
this case each element of the array will be (a reference to) a hash:

my @x;
for ( @column_data ) {
my %col;
# build %col from $_
push @x, \ %col;
}

Now, $x[ 0] is the first of your columns, $x[ 1] is the second, etc.
So $x[ 1]->{ gaga} would be the element of the second column whose key
is "gaga". For more about access through references, see perlref and
perlreftut.

Anno
 
J

Jürgen Exner

Duncan said:
I need to create a separate associative array for each column of data
that I have. However, the number of columns varies. I wish to name
these arrays systematically, %x1, %x2 %x3 etc.

Don't. What you are trying to do is called symbolic references and it has
all kinds of very bad implications. See DejaNews (aka Google) for numerous
previous discussions about this topic.
Also I strongly recommend to read the FAQ entry "How can I use a variable as
a variable name?". It also tells you how to use the proper Perl data
structures instead.
I wish to use a for loop to go through each of these in order and
process them.

No need to use symbolic references for loops. Just the opposite. With the
proper data structure (e.g. an array of hashes) you just loop through the
content of the top array without ever bothering about that those are
references
How can I write the name of the variable %x# where # represents is the
counter for the loop?

No need for a counter. Just do
for (@everything) {
proccess_column($_);
}

jue
 
C

Chris Mattern

Duncan said:
I need to create a separate associative array for each column of data
that I have. However, the number of columns varies. I wish to name
these arrays systematically, %x1, %x2 %x3 etc.

I wish to use a for loop to go through each of these in order and
process them.

How can I write the name of the variable %x# where # represents is the
counter for the loop?

(I know about for loops already and am writing a script).

Thanks in advance for any help.

You need to be using an array of hashes.

foreach my $i (@x) {
foreach my $j (sort(keys %$i)) {
do_something($i{$j});
}
}

See perldoc perllol for more information about how to
use references to make such nested data structures. It
talks in terms of an array of arrays, but the principle's
mostly the same.

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
C

Chris Mattern

Anno said:
Why the "sort"?

Anno

Why not? Sometimes it's nice to get the hash keys in
a predictable order.

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
A

Anno Siegel

Chris Mattern said:
Why not? Sometimes it's nice to get the hash keys in
a predictable order.

Because it's sometimes nice is not a valid reason to invoke an n*log n
algorithm. Other times it is utterly irrelevant. There is nothing in
the problem to indicate one or the other.

The false idiom "sort keys ..." is all too popular. We should not promote
its mindless application on clpm.

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top