2012-05-02 9 views
5

Sto costruendo un array di hash di arrayAccrescimento del valore aggiunto array se condizione è esaudito

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

C'è un modo per aggiungere solo la seconda hash interno solo se una condizione viene esaudito? Qualcosa di simile a questo:

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

ho cercato di riscrivere il mio codice utilizzando 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 
    }, 
); 

ma diventa molto illeggibili, come ho di predefinire tutti gamma interna prima di utilizzarli, che in alcuni casi sono diverse 100 linee di codice sopra l'utilizzo.

C'è un modo per aggiungere direttamente la condizione if dove inserisco l'elemento-array?

risposta

8

Utilizzare i conditional operator, è utilizzabile come espressione.

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

Grazie, funziona esattamente come mi lo ha bisogno – Pit

+1

"Operatore ternario" non è il suo nome, è solo una descrizione di quanti operandi richiede. L'operatore condizionale non è l'unico operatore ternario di Perl (es. 'dbmopen') Risolto. – ikegami

6

Si sta memorizzando un riferimento di array nel proprio innerarray (esempio 1) ma durante la riscrittura si tenta di memorizzare l'array.
Prova a modificare:

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 
    }, 
) ; 

Ed eri anche perdere un ; due volte.

E per la vostra domanda su inlining alcuni contenuti ...

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

Hai ragione, ma non risolve il problema iniziale :( – Pit

+0

@Pit Ho aggiornato la mia risposta. Date un'occhiata a questo. – dgw

+1

Sì, stessa soluzione @daxim – Pit

2

Si può fare:

#!/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; 

uscita:

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

Ma, perché?

si può anche fare:

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

ma, ancora una volta, perché ?

In particolare, l'incorporamento di questo nell'inizializzazione della struttura dati nasconde visivamente ciò che accade dal lettore del codice. Se le condizioni sono anche leggermente complicate, sei tenuto a introdurre errori difficili da rintracciare.

2

Oltre all'operatore condizionale (ea volte in combinazione con esso), map è spesso utile in circostanze simili.

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

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