On Nov 1, 2004, at 10:40 PM, 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);
Thank you for the replies. They were helpful, but as the Perl community sez, "There is more than one way to do it." and I did it a different way.
The bottom line is, I avoided the problem by pointing $self->{author_ids} to an array not an array of arrays. I used DBI's selectall_arrayref, as before. I then loop through each item in $ids to build a simple array and assign that to $self->{author_ids}:
# build a list of author ids my @author_ids = (); my $ids = $dbh->selectall_arrayref($query); foreach (@{$ids}) { push (@author_ids, $_->[0]) } $self->{author_ids} = ([EMAIL PROTECTED]);
I then have this method in my package dealing with updating $self->{author_ids}:
sub author_ids {
# get the input my $self = shift; my @author_ids = @_; # check for input if (@author_ids) { $self->{author_ids} = ([EMAIL PROTECTED]) } else { # rebuild author_ids @author_ids = (); foreach (@{$self->{author_ids}}) { push(@author_ids, $_) } } # done return @author_ids; }
Again, thank you. Once again, well crafted questions to mailing lists return helpful replies:
my $question = 'How do I manipulate array references?';
$question = &craft_well($question);
my @helpful_replies = &send(address => '[EMAIL PROTECTED]', message => $question);
-- Eric Morgan