Adding a value to the array if the condition is met

I create 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 add a second internal hash only if the condition is filled? Something like that:

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 with 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, since I have to predefine all internal arrays before using them, which in some cases amounts to several 100 lines of code above use.

Is there a way to add an if condition directly where I insert an array element?

+5
source share
4 answers

Use a conditional operator, it can be used as an expression.

my @array = (
    {label => 'first hash'},
    {
        label      => 'second hash',
        innerarray => [
            {label => 'first inner hash'},
            (1 == 1)
                ? {label => 'second inner hash'}
                : (),
        ]
    },
);
+8
source

innerarray ( 1), .
:

my @innerarray = () ;
push @innerarray, {label => 'first inner hash'};
push @innerarray, {label => 'second inner hash'} if 1==1; # Not a real condition ...

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

; .

...

my @array = (
    {label => 'first hash'},
    {label => 'second hash',
     innerarray => [
        {label => 'first inner hash'},
        ( $condition == 1 ? {label => 'second inner hash'} : () ) ,
      ]
    },
) ;
+6

:

#!/usr/bin/env perl

use strict; use warnings;

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

use YAML;
print Dump \@array;

:

---
- label: first hash
- innerarray:
    - label: first inner hash
  label: second hash

But why?

You can also do:

    ({label => 'second inner hash'}) x (1 == 0),

but, again, why?

In particular, embedding this in the initialization of the data structure visually hides what comes from the code reader. If the conditions are even a little complicated, you must carefully monitor the errors.

+2
source

In addition to the conditional statement (and sometimes in combination with it), mapit is often useful in such circumstances.

my @labels = (
    'first inner hash',
    'second inner hash',
);

my @array = (
    {label => 'first hash'},
    {
        label      => 'second hash',
        innerarray => [
            ( map { { label => $_ } } @labels ),
        ]
    },
);
+2
source

All Articles