Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Two dimentional array

by algonquin (Sexton)
on Sep 11, 2004 at 11:30 UTC ( #390253=perlquestion: print w/replies, xml ) Need Help??

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

Hello Everybody, what started as small weekend project turn out to be bigger than expected. As I explained in my previous posting, I was trying to retreive data from a web page. i got it finally working by using TokeParser. Now I need to create a two dimentinal array (all integers) with 21 rows and 3 columns. Once I've done that I need to do some math with the contents of the array. I need to create sets of three elements and add those numbers together for each set. I want to try all possible combinations (the order matters). Example: a1+a2+a3, a1+a2+b3, a1+a2+c3.... after all 21 rows are exhausted a1+b2+a3, a1+b2+b3 and so on.... I don't know how to do this with Perl. Does anyone has an idea? Thanks a million in advance.

Replies are listed 'Best First'.
Re: Two dimentional array
by tachyon (Chancellor) on Sep 11, 2004 at 12:17 UTC

    This is pretty basic stuff. Do you know how to do it in any language? If so perl does for loops and 2D arrays very similarly to many languages. Here is a little bit of code to get you started that uses a nice perl loop idiom (neater than the usual for($i=0; $i<21; $i++ ) { })

    my @ary; my $c = 0; for my $row( 0..20 ) { for my $col ( 0..2 ) { $ary[$row][$col] = $c++; } } # have a look print "\t|0\t1\t2\n----+------------\n"; for my $row( 0..20 ) { print "$row\t|"; for my $col ( 0..2 ) { print "$ary[$row][$col]\t"; } print "\n"; } printf " The answer is %d --Deep Thought from Hitch Hikers Guide to the Galaxy\n", $ary[14][ +0];

    Note that like most real languages Perl indexes its arrays starting from 0



Re: Two dimentional array
by svsingh (Priest) on Sep 11, 2004 at 12:03 UTC
    This seems like homework, so I'll be vague. Try doing this with three for loops. The outermost loop interates through the As and the innermost loop iterates over the Cs.

    There's probably a better (more effecient) way to do this, but until I've had some coffee, this is what's coming to mind. You may also want to try the Permute modules on CPAN.

Re: Two dimentional array
by calin (Deacon) on Sep 11, 2004 at 19:24 UTC

    Two dimensional array of integers? Smells like C... :-)

    package Array2D; use strict; use Carp; use constant sizeof_int => length pack 'i', 1337; sub new { my $class = shift; @_ == 2 or croak "must call ${class}::new with x, y dimensions"; my ($dx, $dy) = @_; $dx > 0 && $dy > 0 or croak "${class}::new : illegal x, y dimensions"; bless { dx => $dx, dy => $dy, buf => "\0" x ($dx * $dy * sizeof_int) }, $class; } sub get { my $ary = shift; my $class = ref $ary; @_ == 2 or croak "must call ${class}::get with x, y positions"; my ($x, $y) = @_; my ($dx, $dy) = @{$ary}{qw/dx dy/}; $x >= 0 && $y >= 0 && $x < $dx && $y < $dy or croak "${class}::get : subscripts out of range"; unpack 'i', substr ( $ary->{buf}, ($x + $y * $dx) * sizeof_int, sizeof_int ); } sub set { my $ary = shift; my $class = ref $ary; @_ == 3 or croak "must call ${class}::get with x, y positions and new value"; my ($x, $y, $val) = @_; my ($dx, $dy) = @{$ary}{qw/dx dy/}; $x >= 0 && $y >= 0 && $x < $dx && $y < $dy or croak "${class}::set : subscripts out of range"; substr ( $ary->{buf}, ($x + $y * $dx) * sizeof_int, sizeof_int, pack 'i', $val ); (); } 1;
      Wow, looks good. I'm gonna experiment with it as soon as I get a chance. Thanks a lot.
        Wow, looks good. I'm gonna experiment with it as soon as I get a chance. Thanks a lot.

        :-) I wrote that short module just to show that multi-dimensional arrays can be done a la C, if you have nothing better to do. The only advantage is size - array elements occupy exactly four bytes (x86) which are packed into a pre-allocated string. There's no overhead introduced by individual elements or rows.

        Of course Perl has a native array type. Multi-dimensional arrays are not a fundamental type but they're easily constructed with references. See perldata, perldsc.

        The following code has serious side effects. Don't blindly cut and paste into your program (unless you like trouble).

        #!/usr/bin/perl my @ary = map {["${_}1", "${_}2", "${_}3"]} 'a'..'u'; $\ = "\n"; $" = $, = ","; split ($,), print map {$ary[$_[$_]][$_]} 0..$#_ for glob join $,, map {"{@{[0..20]}}"} 1..3;
Re: Two dimentional array
by diotalevi (Canon) on Sep 11, 2004 at 11:43 UTC
    When is your assignment due?
      The last one was due in the fall of 1996. But you are right it looks kind of suspicious. Anyways, thanks for your attentions.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://390253]
Approved by castaway
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2020-07-12 22:43 GMT
Find Nodes?
    Voting Booth?

    No recent polls found