I created an array like so:
while(@results = $execute->fetchrow())
{
my $active = 'true';
if($results[1] == 0)
{
$active = 'false';
}
my @campaign = ($results[0], $active);
push(@campaign_names, @campaign);
}
Later, when I need to access the name of the campaign (which is the first element of the campaign array), I can’t seem to extract it. What is the proper syntax?
foreach $campaign (@campaign_names)
{
print ????;
}
Thanks!
The problem is you’re pushing an array onto the end of
@campaign_names, when what you want is an array reference. Here’s how I’d write it:I’ve cleaned it up a bit by using a ternary conditional (
?:) to figure out the value of$active. The[ ... ]constructs an anonymous array reference (a scalar pointing to an array) which is then pushed onto@campaign_names.When we loop over those later, two important things to notice are that we use
myin the loop variable to keep it local to the loop block, and that we use->to dereference the elements in the array pointed to by the array reference.