develooper Front page | perl.beginners | Postings from February 2002

Re: Arrays 1x3 or 3x1 - The real questions

Thread Previous | Thread Next
From:
Jonathan E. Paton
Date:
February 12, 2002 09:10
Subject:
Re: Arrays 1x3 or 3x1 - The real questions
Message ID:
20020212170950.29263.qmail@web14607.mail.yahoo.com
Hi all,

> Let's start off with some simple code..
>
> my $arg = &SomeFunction ( my @arry = qw/one two three/)
>
> sub SomeFunction {
>   my @array = @_[0];
>   for (my $i =0; i < @array; i ++ ) {
>     print "$array[0][$i]
>   }
> }
>
> Ok now I understand what the problem is, but I don't
> know how to fix it.  I only have a 1 x 3 array so only
> "one" gets printed - instead of the desired 
> "one two three"..
>
> How do I fix this?  This seems simple enough..  
>

# You need to pass a reference!
my $arg = SomeFunction(\my @arry =qw/one two three/);

sub SomeFunction {
  my $array = shift;

  # Iterate over rows
  foreach my $row (@{$array}) {

    # Iterate over columns
    foreach my $element (@{$row}) {
      print "$element  ";
    }

    # Newline to start next row
    print "\n";
  }
}

perldoc perlref    # Read up on references

Jonathan Paton

__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About