Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > how to alias an array with a variable in a loop ?

Reply
Thread Tools

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

 
 
Jack
Guest
Posts: n/a
 
      07-19-2006
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 !

 
Reply With Quote
 
 
 
 
David Squire
Guest
Posts: n/a
 
      07-19-2006
Jack wrote:
> 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";
}

----
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      07-19-2006
David Squire wrote:
> 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

 
Reply With Quote
 
Ted Zlatanov
Guest
Posts: n/a
 
      07-19-2006
On 19 Jul 2006, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> 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:umper will help you see the
structure of the example data I provided).

Ted


use Data:umper;

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

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

print Dumper \@caa;

 
Reply With Quote
 
David Squire
Guest
Posts: n/a
 
      07-19-2006
Paul Lalli wrote:
> David Squire wrote:
>> 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.


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


DS
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Alias array / array of aliases Colin Beighley VHDL 12 10-14-2011 02:52 PM
reference/alias in perl vs reference/alias in C++ grocery_stocker C++ 9 05-24-2008 04:32 AM
XP X64 Pro, IE7, Delphi 2007, IntraWeb, MS Access, ODBC Alias problem: Insufficient memory for this operation. Alias: SomeDatabase. Skybuck Flying Windows 64bit 13 01-09-2008 07:34 PM



Advertisments