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 7891223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:33:49+00:00 2026-06-03T06:33:49+00:00

I am building an array of hashes of arrays my @array = ( {label

  • 0

I am building an array of hashes of arrays

my @array = (
    {label => 'first hash'},
    {label => 'second hash',
     innerarray => [
        {label => 'first inner hash'},
        {label => 'second inner hash'},
      ]
    },
);

Is there a way to only add the second inner hash only if a condition is fullfilled?
Something like this:

my @array = (
    {label => 'first hash'},
    {label => 'second hash',
     innerarray => [
        {label => 'first inner hash'},
        {label => 'second inner hash'} if 1==1,
      ]
    },
);

I tried to rewrite my code using push:

my @innerarray = ();
push @innerarray, {label => 'first inner hash'};
push @innerarray, {label => 'second inner hash'} if 1==1;

my @array = (
    {label => 'first hash'},
    {label => 'second hash',
     innerarray => \@innerarray
    },
);

But it becomes very illegible, as I have to predefine all inner array before using them, which in some cases is several 100 lines of code above the usage.

Is there any way to add the if-condition directly where I insert the array-element?

  • 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-06-03T06:33:51+00:00Added an answer on June 3, 2026 at 6:33 am

    Use the conditional operator, it is usable as expression.

    my @array = (
        {label => 'first hash'},
        {
            label      => 'second hash',
            innerarray => [
                {label => 'first inner hash'},
                (1 == 1)
                    ? {label => 'second inner hash'}
                    : (),
            ]
        },
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have an array of arrays in Ruby, [[100,300], [400,500]] that I'm building
I am building an array based off comparing two other arrays. But when I
I'm trying to balance a BST by first building an array (inorder) and then
Often, I am building an array by iterating through some data, e.g.: my_array =
I´m building a jquery animation from a multidimensional array, and in the callback of
In the building process of an array I'm trying to call a function, checkIfNodeExists()
I am building two arrays in c# and pass them to a js function
Currently, I'm building several arrays in Powershell, but to make the code more portable,
I am building an app with complex data which I use arrays for. Some
I am building an array and I want it to be a fixed size

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.