Gossamer Forum
Home : General : Perl Programming :

referencing @lol

Quote Reply
referencing @lol
I need some help with this reference.
I'm using -
while (($count,$regions,$region_id) = $sth->fetchrow_array) {
push(@lol, [$regions,$count,$region_id]); }

and accessing each element with -
for $i ( 0 .. $#lol ) {
print "$lol[$i][0] $lol[$i][1] $lol[$i][2]"; }
which works fine.

What I need to do is to reference the @lol and dereference within the loop, but I can't get this to work.

while (($count,$regions,$region_id) = $sth->fetchrow_array) {
push(@lol, [ [$regions,$count,$region_id] ]); }

for $i ( 0 .. $#lol ) {
print "$lol->[$i][0] $lol->[$i][1] $lol->[$i][2]"; }

I've read all the docs but obviously missed the point.

Bob
http://totallyfreeads.com
Quote Reply
Re: [lanerj] referencing @lol In reply to
Hi,

Could you explain a bit more, Im not quite sure what you mean. Also the two push examples you gave seem to be different, one is [] and the other [[]]


But you could eliminate all that code by using fetchall_arrayref

Last edited by:

RedRum: Feb 4, 2002, 2:11 AM
Quote Reply
Re: [RedRum] referencing @lol In reply to
Sorry. I should have explained this better.
I can't use fetchall_arrayref() as the for loop is in a different sub, therefor I needed to reference the @lol.
The extra [] are necessary.
This is from perllol doc -
# assign a reference to list of list references
$ref_to_LoL = [
[ "fred", "barney", "pebbles", "bambam", "dino", ],
[ "homer", "bart", "marge", "maggie", ],
[ "george", "jane", "alroy", "judy", ],
];
print $ref_to_LoL->[2][2];

By the way, can you spot the mistake.

I have 3 @lols and a array to return from the sub, so I think I'll return them as an array of arrays instead of trying to reference each @lol directly.

thanks
Bob
http://totallyfreeads.com
Quote Reply
Re: [lanerj] referencing @lol In reply to
>>By the way, can you spot the mistake.<<

You mean in the perldoc?
Quote Reply
Re: [RedRum] referencing @lol In reply to
Gee if you wanted to pick out all the mistakes in my code you would be there for a month.
Yes perldoc.

Bob
http://totallyfreeads.com
Quote Reply
Re: [lanerj] referencing @lol In reply to
Well it could be written as:

Code:
$ref_to_LoL = [
[ "fred", "barney", "pebbles", "bambam", "dino" ],
[ "homer", "bart", "marge", "maggie" ],
[ "george", "jane", "alroy", "judy" ]
];


....was I right?

....I prefer:

Code:
$ref_to_LoL = [
[qw(fred barney pebbles bambam dino)],
[qw(homer bart marge maggie)],
[qw(george jane alroy judy)]
];

Last edited by:

RedRum: Feb 4, 2002, 4:12 AM
Quote Reply
Re: [lanerj] referencing @lol In reply to
Hmm as for the other qn...how about:

Code:
my $hash = {};

while (($count,$regions,$region_id) = $sth->fetchrow_array) {
$hash->{$region_id} = [$regions,$count];
}

return $hash;

As long as all id's are unique you could use a hash.
Quote Reply
Re: [RedRum] referencing @lol In reply to
Correcto mondo.
An easy one realy but it shouldn't be there. Referencing is hard enough to understand as is.

Bob
http://totallyfreeads.com
Quote Reply
Re: [lanerj] referencing @lol In reply to
>>Referencing is hard enough to understand as is.<<

It is pretty tricky to begin with...a few months ago it totally lost me but I've been practicing quite a bit and it gets easier and more understandable after a while.
Quote Reply
Re: [RedRum] referencing @lol In reply to
Yep could use a hash of lists, but as I have 3 @lols and an array I think the best way is to build an array of arrays and return that.

Bob
http://totallyfreeads.com
Quote Reply
Re: [lanerj] referencing @lol In reply to
How can you have 3 @lols?
Quote Reply
Re: [RedRum] referencing @lol In reply to
3 selects.

#array of array references.
@array_refer = \(@lol1,@lol2,@lol3,@array);
return(@array_refer);

#dereference
sub something {
my @lol1 = @{$_[0]};
my @lol2 = @{$_[1]};
my @lol3 = @{$_[2]};
my @array = @{$_[3]};
for $i ( 0 .. $#lol1 ) {
print "$lol1[$i][0] $lol1[$i][1] $lol1[$i][2]";}
.
.
.
}

Bob
http://totallyfreeads.com
Quote Reply
Re: [lanerj] referencing @lol In reply to
Ah....ok lol so are you saying you've got it working or you still need help?
Quote Reply
Re: [RedRum] referencing @lol In reply to
Thanks, it's working fine.

Bob
http://totallyfreeads.com
Quote Reply
Re: [lanerj] referencing @lol In reply to
Ok hehe Cool

Oh you could always do it this way:

my ($a,$b,$c,$d) = my_routine();

Then in my_routine() do...

# return all arrays
return \(@lol1,@lol2,@lol3,@array);

Then you can just use $a->[0], $b->[0] etc to access the elements.