> >> > I have created a module, and inside one of the Package methods, I
> >> have
> >> > the following code:
> >> >
> >> > $href = $getPlanInfo->fetchrow_hashref();
> >> >         foreach my $key (keys %$href) {
> >> >                 print "$key : $href->{$key}\n";
> >> >                 $name = $key;
> >> >                 $self->{$name} = $href->{$key};
> >> >         }
> >
> > So in the above you want to loop over the result set storing them to
> > the
> > object, this sounds like the perfect use of an array reference stored
> > to
> > the object. Something like:
> >
> > while (my $href = $getPlanInfo->fetchrow_hashref) {
> >    push @{$self->{$name}}, $href;
> > }
> >
> > Now $self->{$name} contains a list, similar to what you mention with
> > the
> > indexes below, but let Perl handle that for you.  Then you can loop
> > over
> > the list as with any array reference, or index into it similar to any
> > array.
> >
> > foreach my $element (@{$self->{$name}}) {
> >     foreach my $key (keys %$element) {
> >          print "$key: $element->{$name}\n";
> >     }
> > }
> 
> Wow! ...and I *almost* understand how it works! Admittedly, I'm just
> begun familiarizing myself with references the last few days, but this
> looks so far like what I need. I am going to research how, and why
> this works, but am I correct with this?:
>

perldoc perlreftut
perldoc perlref
perldoc perllol
perldoc perldsc

Are excellent resources. If you are into the whole book thing then I
would highly suggest the Learning PORM book from O'Reilly[1].
 
> (based on your code above, assuming 2 rows in db for user)
> 
> @{$self->{$name}}[0] == %hash (name & val)   # from db row 1
> @{$self->{$name}}[1] == %hash2 (name & val) # from db row 2
> 

I think you are thinking about them correctly....

$self->{$name} contains an array reference as its value. So the above
slices into that array reference to retreive a particular indexes value,
which happens to be a hash reference.  To make things easier and
generally better looking, Perl provides a simpler access syntax, so for
the first element of the array above you would write,

$self->{$name}->[0]

And the second becomes,

$self->{$name}->[1],

So then to access into the next level, aka to get specific information
from a record you could issue,

$self->{$name}->[0]->{'license_number'};

(Obviously I am making up the names of the fields, but hopefully you get
the point.)  The listed docs should provide much more thorough,
accurate, and readable examples/descriptions.

> I truly was never aware the true flexibility of Perl before. Wow...if
> I had of researched this stuff before...I don't want to even imagine
> the time I would of saved myself ;o)
> 
> Steve

Yep, welcome to a whole new world.  References often take some time to
"just get", but once you just get them, they make things significantly
easier.

<snip>

http://danconia.org

[1] http://www.oreilly.com/catalog/lrnperlorm/


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to