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

Discussion in 'Perl Misc' started by Jack, Jul 19, 2006.

  1. Jack

    Jack Guest

    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 !
    Jack, Jul 19, 2006
    #1
    1. Advertising

  2. Jack

    David Squire Guest

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

    ----
    David Squire, Jul 19, 2006
    #2
    1. Advertising

  3. Jack

    Paul Lalli Guest

    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
    Paul Lalli, Jul 19, 2006
    #3
  4. Jack

    Ted Zlatanov Guest

    On 19 Jul 2006, 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::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;
    Ted Zlatanov, Jul 19, 2006
    #4
  5. Jack

    David Squire Guest

    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
    David Squire, Jul 19, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. paresh

    alias for a variable

    paresh, Feb 15, 2007, in forum: C Programming
    Replies:
    14
    Views:
    552
    CBFalconer
    Feb 15, 2007
  2. grocery_stocker
    Replies:
    9
    Views:
    733
    grocery_stocker
    May 24, 2008
  3. Replies:
    1
    Views:
    1,112
    Fresno Bob
    Sep 17, 2008
  4. Colin Beighley

    Alias array / array of aliases

    Colin Beighley, Oct 5, 2011, in forum: VHDL
    Replies:
    12
    Views:
    1,602
    Tricky
    Oct 14, 2011
  5. Isaac Won
    Replies:
    9
    Views:
    349
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page