how to alias an array with a variable in a loop ?

J

Jack

Hi does anyone know how to do this ?

this works but its only 1 of my 377 chunkarrays !! :
@chunkarray0 = sort {$a <=> $b} @chunkarray0;

this automation doesnt and I cant figure out why:
for ($t=0;$t<=$count;$t++) {
$chunkarray = 'chunkarray'.$t;
$cmd = '@'.$chunkarray.'= sort {$a <=>$b} @'.$chunkarray.';'; print
"COMMAND : ", $cmd."\n";
$cmd;
}

any tips GREATLY appreciated !
 
D

David Squire

Jack said:
Hi does anyone know how to do this ?

this works but its only 1 of my 377 chunkarrays !! :
@chunkarray0 = sort {$a <=> $b} @chunkarray0;

this automation doesnt and I cant figure out why:

Because you can't just plonk a variable down as a line of code and
expect it to be interpreted as an instruction!
for ($t=0;$t<=$count;$t++) {
$chunkarray = 'chunkarray'.$t;
$cmd = '@'.$chunkarray.'= sort {$a <=>$b} @'.$chunkarray.';'; print
"COMMAND : ", $cmd."\n";
$cmd;

Are you surprised that this didn't work???
}

any tips GREATLY appreciated !

I would do it with references, and most likely keep those references in
a single overarching data structure (i.e. an array of array refs, or
perhaps a hash of array refs). For example:


----

#!/usr/bin/perl
use strict;
use warnings;

my @array1 = qw( a d g s g s );
my @array2 = qw( sad ds fg sdf fg);
my @array3 = qw( er rty tyr erw rty );

my @arrays = (\@array1, \@array2, \@array3);

foreach my $arrayref (@arrays) {
my @sortedarray = sort {$a cmp $b} @$arrayref;
print join ' ', @sortedarray;
print "\n";
}

----
 
P

Paul Lalli

David said:
I would do it with references, and most likely keep those references in
a single overarching data structure (i.e. an array of array refs, or
perhaps a hash of array refs). For example:


----

#!/usr/bin/perl
use strict;
use warnings;

my @array1 = qw( a d g s g s );
my @array2 = qw( sad ds fg sdf fg);
my @array3 = qw( er rty tyr erw rty );

my @arrays = (\@array1, \@array2, \@array3);

Allow me to suggest that when you find yourself appending 1, 2, 3, etc
to the end of your variable names, stop what you're doing, erase it
all, and do it right from the outset. Do not create three separate
arrays and then put references to them in a fourth big array. Create
one array that contains references to anonymous arrays. That way when
you need to edit your code later to add or remove an array, you only
have one change to make, not two:

my @arrays = (
[ qw/a d g s g s/ ],
[ qw/sad ds fg sdf fg/ ],
[ qw/er rty tyr erw rty/ ],
);
foreach my $arrayref (@arrays) {
my @sortedarray = sort {$a cmp $b} @$arrayref;
print join ' ', @sortedarray;
print "\n";

Unless you've futzed with the $" variable, these last two lines are
more succinctly written:
print "@sortedarray\n";

Paul Lalli
 
T

Ted Zlatanov

Hi does anyone know how to do this ?

this works but its only 1 of my 377 chunkarrays !! :
@chunkarray0 = sort {$a <=> $b} @chunkarray0;

this automation doesnt and I cant figure out why:
for ($t=0;$t<=$count;$t++) {
$chunkarray = 'chunkarray'.$t;
$cmd = '@'.$chunkarray.'= sort {$a <=>$b} @'.$chunkarray.';'; print
"COMMAND : ", $cmd."\n";
$cmd;
}

Put your @chunkarray0 ... @chunkarray377 variables in an array (let's
say it's called @caa). You need to store references, not the
variables themselves. Look at 'perldoc perldata' to learn about
Perl's built-in data types and what they can do for you.

Then iterate over the array (Data::Dumper will help you see the
structure of the example data I provided).

Ted


use Data::Dumper;

my @caa = ( [2,3,4,1,0], [10,14,12,9,4]);

foreach my $ca (@caa)
{
@$ca = sort { $a <=> $b } @$ca;
}

print Dumper \@caa;
 
D

David Squire

Paul said:
Allow me to suggest that when you find yourself appending 1, 2, 3, etc
to the end of your variable names, stop what you're doing, erase it
all, and do it right from the outset.

I could not agree more. I was merely addressing the OPs actual problem
statement.


DS
 

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
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top