Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6761811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:14:10+00:00 2026-05-26T14:14:10+00:00

I created an array like so: while(@results = $execute->fetchrow()) { my $active = ‘true’;

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:14:11+00:00Added an answer on May 26, 2026 at 2:14 pm

    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:

    while(@results = $execute->fetchrow())
    {
      my $active = $results[1] ? 'true' : 'false';
      push @campaign_names, [ $results[0], $active ];
    }
    
    # later
    
    foreach my $campaign( @campaign_names ) 
    { 
        my $name = $campaign->[0]; 
        my $active = $campaign->[1];
    }
    

    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 my in 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JSON array like this: { forum:[ { id:1, created:2010-03-19 , updated:2010-03-19
I have a multi dimensional array, like this: array('name' => array('title'=>'Title','date'=>'Created')) I store it
Ive got an associative array thats pulled from mysql results and id like to
Imagine that I want to create an array from another array like this: $array
Let's say I create a jagged 2d array like so: public static char[,] phoneLetterMapping
I was trying to create an extension that could slice any array-like class (since
I'd like to create an array from a CSV file. This is about as
How do I create an array in smarty from a given string like 22||33||50
How can I create an array of namespaces? And because it seems like a
I would like to create a multi-dimensional array with two variables but don't know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.