On Mon, 01 Nov 2004 22:30:29 -0700, Michael McDonnell
<[EMAIL PROTECTED]> wrote:
> Eric Lease Morgan wrote:
> 
> >
> > How do I manipulate array references?
> >
> > In a package I'm writing I initialize an array reference (I think)
> > through DBI like this:
> >
> >   $self->{author_ids} = $dbh->selectall_arrayref($query);
> 

Actually, from the examples you listed below you probably want to use

   $self->{author_ids} = $dbh->selectcol_arrayref($query);

(Note that the method is selectCOL, not selectALL)
This will give you the one dimensional array ref that you are looking
for, and '$self->{author_ids}' will equal '[1,2,3,4]'.
> >
> > First of all, what sort of foreach loop can I write to iterate through
> > the contents of $self->{author_ids}?
> 

foreach my $author ( @{ $self->{author_ids} } ) {
    print $author;
}
print "\n";

This dereferences the arrayref to treat it like a normal array.

> >
> > Third, if I have a list of integers, how to I assign the items in this
> > list to $self->{author_ids}?
> >
> $self->{author_ids}->[0] = 1;
> $self->{author_ids}->[1] = 2;
> $self->{author_ids}->[2] = 3;
> 
> or
> 
> $self->{author_ids} = [1, 2, 3];

These all work fine now that you have a one dimensional arrayref,
because you called selectcol_arrayref instead of selectall_arrayref.

> I would strongly recommend adding quotation marks to {authors_ids} so
> that it is {'authors_ids'} or defining it as a constant.  The only
> reason why {authors_ids} is equivalent to {'authors_ids'} is because
> PERL is converting it to a constant on the fly for you.  If you
> accidentally picked a name that was already a constant in your namespace
> you might end up with results that are hard to debug (imagine if a
> module put a constant in your name space that you didn't know about).

While not bad advice, this isn't entirely accurate.  The issue is in
fact ambiguity, as Michael alluded to, but it's in the other
direction.  You risk NOT using a constant or sub call where you might
have wanted it.  From the perlref man page:

       A new feature contributing to readability in perl version 5.001 is that
       the brackets around a symbolic reference behave more like quotes, just
       as they always have within a string.  That is,

           $push = "pop on ";
           print "${push}over";

       has always meant to print "pop on over", even though push is a reserved
       word.  This has been generalized to work the same outside of quotes, so
       that

           print ${push} . "over";

       and even

           print ${ push } . "over";

 [cut out irrelevant section]

       Similarly, because of all the subscripting that is done using single
       words, we've applied the same rule to any bareword that is used for
       subscripting a hash.  So now, instead of writing

           $array{ "aaa" }{ "bbb" }{ "ccc" }

       you can write just

           $array{ aaa }{ bbb }{ ccc }

       and not worry about whether the subscripts are reserved words.  In the
       rare event that you do wish to do something like

           $array{ shift }

       you can force interpretation as a reserved word by adding anything that
       makes it more than a bareword:

           $array{ shift() }
           $array{ +shift }
           $array{ shift @_ }

and more specifically in regard to constants, from the constant pragma man page:

       You can get into trouble if you use constants in a context which auto-
       matically quotes barewords (as is true for any subroutine call).  For
       example, you can't say $hash{CONSTANT} because "CONSTANT" will be
       interpreted as a string.  Use $hash{CONSTANT()} or $hash{+CONSTANT} to
       prevent the bareword quoting mechanism from kicking in.  Similarly,
       since the "=>" operator quotes a bareword immediately to its left, you
       have to say "CONSTANT() => 'value'" (or simply use a comma in place of
       the big arrow) instead of "CONSTANT => 'value'".

Hope this helps.

--miker

> 
> --
> Michael McDonnell, GCIA
> Winterstorm Solutions, Inc.
> [EMAIL PROTECTED]
>

Reply via email to