Popcorn Dave has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks,

Please forgive the title if it's not quite descriptive enough but here's my dilema.

I'm writing a solitare game in Perl/Tk and I have 5 arrays with 5 elements each. I thought about using a 2 dimensional array, but I am unsure how you would unshift a value off that type of data structure, so for the present I'm sticking to the 5 arrays.

I'm trying to use eval to build the code to compare two array values, but I am getting an error saying that the index, that I am trying to concatenate with my array base name, needs to be defined.

Here's the code I've been playing with to test this:

#!/usr/bin/perl -w use strict; my ($i, $j); my @a1=(1,2,3,4,5); my @a2=(2,1,3,5,4); my @a3=(3,4,5,1,2); my @a4=(4,2,3,5,1); my @a5=(5,4,3,2,1); for $i(1..4){ for $j($i+1..5){ if (eval{$a.$i}[0] == eval{$a.$j}[0]){ print "Matched one!\n"; last}; }; };

Can someone possibly shed some light on how I can accomplish what I'm trying to do? I'd rather do it this way than a series of if match then last type of thing.

Thanks in advance!

Some people fall from grace. I prefer a running start...

Replies are listed 'Best First'.
(tye)Re: Using eval to build array structure
by tye (Sage) on Jul 17, 2002 at 16:51 UTC

    Please switch to a 2-dimensional array. See References Quick Reference for the very short story on how to dereference complex data structure references in Perl.

    #!/usr/bin/perl -w use strict; my ($i, $j); my @a= ( [1,2,3,4,5], [2,1,3,5,4], [3,4,5,1,2], [4,2,3,5,1], [5,4,3,2,1], ); for $i(1..4){ for $j($i+1..5){ if ($a[$i][0] == $a[$j][0]){ print "Matched one!\n"; unshift @{$a[$i]}, pop @{$a[$j]}; push @{$a[$j]}, pop @{$a[$i]}; last; } } }

            - tye (but my friends call me "Tye")
Re: Using eval to build array structure
by dragonchild (Archbishop) on Jul 17, 2002 at 16:54 UTC
    Let's fix the base problem, so you don't have to even think about eval at all.
    use strict; my @gameboard = ( [ 1, 2, 3, 4, 5 ], [ 2, 3, 4, 5, 1 ], [ 3, 4, 5, 1, 2 ], [ 4, 5, 1, 2, 3 ], [ 5, 1, 2, 3, 4 ], ); for my $i (1 .. 4) { for my $j ($i + 1 .. 5) { if ($gameboard[$i][0] == $gameboard[$j][0]) { print "Matched $i,0 and $j,0\n"; } } }

    These are called array references. It's the way Perl (from 5.000 onwards) does multi-level data structures. Read up on them - they'll make your life very easy.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      I agree with both Tye and dragonchild. However, I have just a little nit with both of their sample codes. The Range for the $i subsrcript should be 0 .. 4 since perl arrays are zero indexed.

      so,

      for my $i (1 .. 4) {
      should be
      for my $i (0 .. 4) {

      and,

      for $j($i+1..5){
      should be
      for $j($i+1..4){

      -meta4
        I would agree with you, yes, that arrays do start from 0 in Perl. However what I was originally trying to do was to get @array1, @array2, etc. build using the loop index. Since my arrays were numbered 1-5, they are correct in their examples ( at least for my problem ).

        Some people fall from grace. I prefer a running start...

Re: Using eval to build array structure
by ehdonhon (Curate) on Jul 17, 2002 at 16:38 UTC
    I am unsure how you would unshift a value off that type of data structure,

    $val = unshift ( @{$a->[$i]} );

    I am getting an error saying that the index, that I am trying to concatenate with my array base name, needs to be defined.

    This is sloppy, there has to be a better way. But here is at least one solution...

    use strict; for $i(1..4) { for $j($i+1..5){ no strict 'refs'; if ([eval{"\@a$i"}]->[0] == [eval{"\@a$j"}]->[0]){ print "Matched one!\n"; last}; }; };
Re: Using eval to build array structure
by hiseldl (Priest) on Jul 17, 2002 at 16:48 UTC
    Try replacing: if (eval{$a.$i}[0] == eval{$a.$j}[0]){ with:  if (@{"$a$i"}[0] == @{"$a$j"}[0]){ That works in my Perl Decoder Ring. :-)

    --
    .dave.

Re: Using eval to build array structure
by Popcorn Dave (Abbot) on Jul 17, 2002 at 16:55 UTC
    Thanks to all!

    Now that I see the 2d array, that makes sense. I will definitely change that over.

    Some people fall from grace. I prefer a running start...