Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

What way to weigh an AoA?

by SamCG (Hermit)
on May 17, 2006 at 00:17 UTC ( [id://549922]=perlquestion: print w/replies, xml ) Need Help??

SamCG has asked for the wisdom of the Perl Monks concerning the following question:

If it please the monks, I'll start with an explanation. Actually, this node was going to have quite different questions, but writing this node helped me sort some of them out.

I set up a sqlite database, with a users table:
my %user = ('Mouse'=>'Mickey', 'Spears'=>'Britney' .. more, see below) +; for my $lname (keys %user) { my $mail = "$user{$lname}.$lname\@acompany.com"; &add_user($dbh, $user{$lname}, $lname, $mail); }
I did a sql query in a module I wrote:
sub user_list { my ($db_ref) = @_; my $users = $db_ref->selectall_arrayref("SELECT last_name, first_n +ame from user") or die "$!\n"; return $users; }
I have no problem passing in the database handle reference, and the $users reference comes back fine. But then I have to iterate through it. It took me a while to figure out how to do this, eventually using Data::Dumper to figure out what my data structure actually looked like -- an array of arrays.
$VAR1 = [ [ 'Mouse', 'Mickey' ], [ 'Spears', 'Britney' ], [ 'Lohan', 'Lindsey' ], [ 'Martin', 'Ricky' ], [ 'Burns', 'Jeremy' ], [ 'Smithers', 'Erwin' ], [ 'Simpson', 'Homer' ], [ 'Flanders', 'Ned' ] ];

I figured out how to iterate through this with print "$user->[$_]->[0], $user->[$_]->[1]\n" for 0..5; However, I'd like to just use the last subscript of the array (a la something like $$#user). However, 0..$$#user seems to go far higher than I'm expecting. It gives me all the users, and then a couple of hundred commas (note the comma in the print statement, above). Actually, when I print $$#user alone, it gives me 3460. I'm a bit surprised it doesn't either 1) just give me all the values and stop, or 2) go on forever. I'd guess it's give me some type of exponential set. How do I get it to give me what I want (7, in this case with 8 users)?
Thanks,
Sam

update:Thank you all for the info.

I'll look into the techniques suggested by davidrw and Anonymous Monk. I agree that the hash slice is a bit more clear.

Zaxo, Leeriness appreciated. This is a sample, most of the time I'd expect to be entering users one at a time (and they're keyed by user id on the insert), so there will be no clobbering here.

The reason for the difference in variables is because some of the code is in a module, and some in script. The variables are okay, though I confess I didn't use strict in the sample code (an oversight), and this would have given me an error. Specifically, something like:

Can't use string ("4052") as a symbol ref while "strict refs" in use a +t H:\script\office.pl line 13.
I don't think I would have figured it out from this, as it pretty much tells me what I'd already determined.


-----------------
s''limp';@p=split '!','n!h!p!';s,m,s,;$s=y;$c=slice @p1;so brutally;d;$n=reverse;$c=$s**$#p;print(''.$c^chop($n))while($c/=$#p)>=1;

Replies are listed 'Best First'.
Re: What way to weigh an AoA?
by GrandFather (Saint) on May 17, 2006 at 00:35 UTC

    Probably what you want is $#$users. The oA bit doesn't matter. It's the fact that you have a reference to the array that is giving you grief.

    use strict; use warnings; my $users = [(0..7)]; print $#$users;

    Prints:

    7

    DWIM is Perl's answer to Gödel
Re: What way to weigh an AoA?
by davidrw (Prior) on May 17, 2006 at 00:35 UTC
    0..$$#user seems to go far higher than I'm expecting
    I think you're looking for just 0 .. $#$user


    As a side note/excercise, try Dumper'ing out the results if you change it to be:
    my $users = $db_ref->selectall_arrayref("SELECT last_name, first_name +from user", {Slice=>{}}) or die "$!\n";
    You might prefer (hash keys are much more meaningful to read than array indices) the way this then looks:
    printf "%s, %s\n", $user->[$_]->{last_name}, $user->[$_]->{first_name} + for 0..5; printf "%s, %s\n", @{$user->[$_]}{ qw/last_name first_name/ } for 0..5 +; # using a hash slice # or, if going through all rows: printf "%s, %s\n", $_->{last_name}, $_->{first_name} for @$user; printf "%s, %s\n", @{$_}{ qw/last_name first_name/ } for @$user;
Re: What way to weigh an AoA?
by Errto (Vicar) on May 17, 2006 at 04:45 UTC
    Others have pointed out the correct syntax. I just wanted to comment on what $$#foo actually does. The weird number you're seeing is the process ID. Perl treats $$ as a variable name (see perlvar) and the # introduces a comment just like it normally does. The fact that you didn't get a syntax error is a coincidence due to the fact that this print statement must have been at the end of a block.
Re: What way to weigh an AoA?
by Zaxo (Archbishop) on May 17, 2006 at 00:43 UTC

    I think use warnings; and use strict; would go a long way toward solving this for you. You call your arrayref $user sometimes, and some of those may be in the code.

    The last index of $users will be in $#$users, not $$#users. Observe,

    $ perl -Mstrict -we'my $foo = ["a".."z"];print $#$foo,$/' 25 $ perl -Mstrict -we'my $foo = ["a".."z"];print $$#foo,$/' 14354 $
    I'm not entirely sure what $$#users represents, but that explains your extra array elements.

    I'm leery of your hash keyed to surname. When you insert Minnie, Mickey will get clobbered.

    After Compline,
    Zaxo

Re: What way to weigh an AoA?
by Anonymous Monk on May 17, 2006 at 01:50 UTC

    Are you just trying to run through the returned rows?

    If so, something like :

    ( Untested )

    foreach $row ( @$users ) { @current_record = @{$row}; foreach $column ( @current_record ) { print "column = <$column>\t"; } print "\n"; }
    may work.

    Doing it this way you don't need to know how many
    records are being returned.

    Written from ancient memory so may need some work.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://549922]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-19 18:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found