Hi Deb -

> -----Original Message-----
> From: Deb [mailto:[EMAIL PROTECTED]
> Sent: Saturday, March 01, 2003 7:17 PM
> To: R. Joseph Newton; Perl List
> Subject: Re: shifting through arrays of line data
> 
> 
> This (code below) makes sense to me, but I was talking this over with a
> co-worker on Friday, and then I tried putting together some 2-dimensional
> hashes - which hurts my head at the moment.  So I went to
> perl.plover.com/FAQs to read (again) his article on references, 
> and I still
> have a mental block.  Conceptually I understand multi-dimensioned 
> hashes, but
> in practice I have a LOT of trouble with the syntax.
> 
> I need to work hard to solidify how to put it together in a real coded
> situation.
> 
> OTH, This code posted below makes more sense to my brain.  I was on this
> track earlier, but digressed into the above approach - which is 
> probably more
> elegant, but difficult for me to see how to do in practice.
> 
> deb
> 
> 
> 
> R. Joseph Newton <[EMAIL PROTECTED]> had this to say,
> 
> > Deb wrote:
> > 
> > > Hi Guys,
> > >
> > > I have an array in which each element is a line commandline 
> data.  It looks
> > > something like this -
> > >
> > > @Array contains lines:
> > >
> > > post1: -r [EMAIL PROTECTED] -x cat-100 -h post1
> > > post2: -x tel -h post2
> > > post3: -h post3 -x hifi
> > 
> > The getRelationships sub here has a few less typos, and the 
> test stub runs well.
> > 
> > #!/usr/bin/perl -w
> > 
> > use strict;
> > use warnings;
> > 
> > sub getRelationship($$);
> > 
> > testGetRelationships();
> > 
> > sub testGetRelationships {
> >   my $string = "post1: -r [EMAIL PROTECTED] -x cat-100 -h post1";
> >   my %relationships;
> > 
> >   getRelationship($string, \%relationships);
> > 
> >   foreach my $key (keys %relationships) {
> >      print "$key:=$relationships{$key}\n";
> >   }
> > }
> > 
> > sub getRelationship ($$) {
> >   my ($commandline, $relationships) = @_;
> >  # print "$commandline\n";       #debug
> >   my @commands = split /\s+-/, $commandline;
> >   my $key = shift(@commands);
> >   foreach (@commands) {
> >     if (s/^x\s+//) {$$relationships{$key} = $_;}
> >   }
> > }
> > 
> > 

I went through (over?) the same hurdle several years ago. I'm
happy for you that the light is beginning to burn.

However, I would advise you to get comfortable with the
'->' syntax also; _most_ 'professional' scripts
use this representation (just cruse some source code
on CPAN).

'->' is easy (again, once the light bulb shines, the
church bell chimes, the fat lady sings, ...). Here's
my trick:

Given:

  $something->{$key};

I see the '->' telling me that the preceding ($something)
is a reference. Next, the {} tells me that something is
a reference to a hash and $key is the hash key. The same
syntax goes for array references:

  $something->[$index];

Now, to use '->' in multi dimensions, just string them out:

  $some_hash_ref->{$some_hash_key}->{$another_hash_key}->[$array_index];

Read right to left as:

  The value of element number $array_index in an array referenced by
  the key $another_hash_key in a hash referenced by the key
  $some_hash_key in a hash referenced by $some_hash_ref!!!

Easy, yeah?

Oh well, keep on trucking'

Aloha => Beau;



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

Reply via email to