--- Timothy Johnson <[EMAIL PROTECTED]> wrote:
> For starters, you should get in the habit of adding 'use warnings'
> and 'use strict' to the beginning of your scripts.

Absolutely. Make them a mantra. Fix code until they are happily silent.
THEN all you have to watch out for are the insidious little things....

> Secondly, it looks like you are trying to use $jon as a reference to
> @jon, but you never assign it that value.  You should probably change
> 
>    @address = qw/ $jon $marsha $ben $abe /;

This is not an array of arrays. This is an array of strings that look
like names with dollar signs in front of them. If you print the values,
they will be '$jon' &c., not whatever was in the variable with that
name.

> to
> 
>    @address = (\@jon,\@marsha,\@ben,\@abe);

That would work, though I'd recommend doing it manually. See below.

> Thirdly, I would get rid of the C-style for statement and just do a
> Perl foreach()

Try to always do this. for(;;) works in Perl, but foreach() is
virtually always more efficient. Even if you *must* have indices, use 

  foreach my $ndx (0..$#ary) {

or some such.

>    foreach $item(@address){
>      print $item->[0];
>      print ${$item}[1];
>    }

You can even say $$item[0], and for subaddressing items just add then
next index: $$item[1][3]

> Finally, you should check out 'perldoc perllol' (List of Lists)

And actually write yourself some testing examples. :)
 
> -----Original Message-----
> I was wondering how I would could access arrays of arrays. I would
> like to be able to get to (print, for now) the value in each nested
> array. Here is what I've been trying:
> @jon = qw/ "jon" "hansfelt" "123-1122" /;
> @ben = qw/ "ben" "jones" "222-1231" /;
> @marsha = qw/ "marsha" "padgett" "333-9087" /;
> @abe = qw/ "abe" "johnson" "421-4623" /;

Did you mean for all those quotes to be part of the strings?
qw// does the quoting, and it's single-quotish. Whitespaced elements
are returned individually, and things like quotes and dollar signs are
included, not interpolated. This is like saying

  @jon = ( '"jon"','"hansfelt"','"123-1122"');

just as 

  @a = qw/ $a $b /;

is like saying 

  @a = ('$a','$b');
  
which doesn't reference any variables, since those are just single
quoted strings.


> @address = qw/ $jon $marsha $ben $abe /;

As mentioned above, this doesn't work. You could say

>    @address = (\@jon,\@marsha,\@ben,\@abe);

But then changing @jon changes @address, and vice-versa, which is
sometimes handy, but generally not what you want.

Don't do this unless you need to. c.f. perllol, but mainly, be aware
that the [] operator will return a reference top an anonymous array
containing copies of the list of items it surrounds.

You could do it this way:

  @a = ( [ 1,2,3 ],
         [ qw / a b c / ] );

which makes a 2-dimensional array, so that $a[1][1] is "b", and
$a[0][2] is 3. 

Technically, Perl doesn't do multi-dimensional arrays, but since a
reference to an array is a scalar that you can put into an array, it
will dereference them for you as a convenience factor, so that you
don't have to write $a[0]->[0] (though you can if you want to....but
don't.)

Thus, to create the data structure I think you wanted above:

  @address = ( [ qw/ jon    hansfelt 123-1122 / ],
               [ qw/ ben    jones    222-1231 / ],
               [ qw/ marsha padgett  333-9087 / ],
               [ qw/ abe    johnson  421-4623 / ],
  );

> @address = sort @address;

Again, this won't work. The values in @address are going to be
references to anonymous arrays containing the data itself, so they will
look like ARRAY(0x40025158), and sort will stringify them and put them
in order by the ASCII representation of their memory addresses, which
is hardly what you want. If you want them sorted, you should probably
look into using a hash:

  %address = ( jon    => [ qw/ jon    hansfelt 123-1122 / ],
               ben    => [ qw/ ben    jones    222-1231 / ],
               marsha => [ qw/ marsha padgett  333-9087 / ],
               abe    => [ qw/ abe    johnson  421-4623 / ],
  );

which would let you say $address{jon}[2] to get jon's phone number.
Yep, it's that easy. Yep, you can mix and match arrays and hashes.

but then, if you were doing that, maybe you should just look at putting
all the values in hashes, so that you can pull them at will like a
little databaserather than having to do complex searches -- and Lo!
there was an anonymous hash constructor {}! and again, Lo!, it works
the same way! So that if you want them available by name, you can say:

  @address = ( jon    => { first => 'jon',
                           last  => 'hansfelt',
                           phone => '123-1122',  },
               ben    => { first => 'ben',
                           last  => 'jones',
                           phone => '222-1231',  },
               marsha => { first => 'marsha',
                           last  => 'padgett',
                           phone => '333-9087',  },
               abe    => { first => 'abe',
                           last  => 'johnson',
                           phone => '421-4623',  },
  );

which lets you say $address{jon}{phone}, which is more convenient and
readable. Obviously, by that point, you might want a better key than
"jon", but this illustrates the point.

> for ($i = 0; $i <= $#address; $i++) {
>      @temp = $address[$i];
>      #print @temp."\n";
>      print $temp[0]."\n";
>      print $temp[1]."\n";
>  }

Now, let's try to duplicate this with our hash structure:

  for my $fname (sort keys %address) {
    print "$fname\n$address{$fname}{last}\n";
  }

Or for the line-purists :)

  print "$_\n$address{$_}{last}\n" for sort keys %address;

Does that help?



__________________________________________________
Do you Yahoo!?
Yahoo! Shopping - Send Flowers for Valentine's Day
http://shopping.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to