Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I was wondering if I can use if statement or any other conditional statement inside a hash as I have done here. If yes how is it done. Just correct me if I am wrong.
$i = 1; %hash = ( ('abc' => 7) if (! $i), 'hk' => 5, 'jk' => 6 ); foreach $key (keys %hash) { print "the key is $key and val is $hash{$key}\n" }

2005-02-05 Edited by Arunbear: Changed title from 'Problem with hash', as per Monastery guidelines

Replies are listed 'Best First'.
Re: Initializing a hash using conditions
by dave_the_m (Monsignor) on Feb 03, 2005 at 12:04 UTC
    if is a statement modifier, so you can't use it there. Try something like:
    $i ? () : ('abc' => 7)
    which inserts either zero or two elements into the list of key-value pairs, depending on the condition.

    Dave.

Re: Initializing a hash using conditions
by Anonymous Monk on Feb 03, 2005 at 12:06 UTC
    %hash = ( $i ? (abc => 7) : (), hk => 5, jk => 6, );

      Anon Monk++, nice neat solution that does not create the key abc unless the condition evaluates true. However I think you switched the logic of the OP

      # OP %hash = ( ('abc' => 7) if (! $i), # abc if not $i 'hk' => 5, 'jk' => 6 # Anonymonk %hash = ( $i ? (abc => 7) : (), # create abc if $i hk => 5, jk => 6, ); # /me thinks %hash = ( $i ? () : (abc => 7), # create abc if not $i hk => 5, jk => 6, );

      Cheers,
      R.

      Pereant, qui ante nos nostra dixerunt!
Re: Initializing a hash using conditions
by holli (Abbot) on Feb 03, 2005 at 12:04 UTC
    %hash = ( 'abc' => $i ? 7 : 0, 'hk' => 5, 'jk' => 6 );
    see perlop

    holli, regexed monk
Re: Initializing a hash using conditions
by monkey_boy (Priest) on Feb 03, 2005 at 12:03 UTC
    Maybe, dont know to be honest, but what you wrote wont compile.
    For simplicity , why not just do this...
    my $i = 1; my %hash = ( 'hk' => 5, 'jk' => 6 ); $hash{'abc'} = 7 if (! $i); foreach my $key (keys %hash) { print "the key is $key and val is $hash{$key}\n" }



    Im so sick of my Signature...
Re: Initializing a hash using conditions
by rir (Vicar) on Feb 03, 2005 at 14:34 UTC
    I prefer moving the conditional out of the initial assignment, but that is a thoughtless prejudice of habit and society.

    I recommend the inline ternary solutions that you've recieved. I post because those solve your immediate problem but don't address the fundamental problem with your code:

    %hash = ( ( 'abc' => 7) if ( ! $i), );
    The primary problem is that you are placing a statement where an expression is expected. A general solution to this problem is to use a do BLOCK that ends with the value you want.

    %hash = ( do{ if ( $i) { abc => 7 } else { } }, );
    Be well,
    rir
Re: Initializing a hash using conditions
by punkish (Priest) on Feb 03, 2005 at 12:29 UTC
    for questions that start with...
    I was wondering if I can...

    the answer is... why not just try it and see if you can. If it is wrong, Perl will complain, especially if you have used -w and strict.

    The above won't compile. However, if you change it ever so slightly, it will. Keep in mind that the right side of => or any element in an array, for that matter, can be the result of an expression. Convert your if... else... statement into the ternary form and it will work...

    %hash = ( 'abc' => !$i ? 7 : '', 'hk' => 5, 'jk' => 6 ); # a ternary expression is $var = condition ? if_true : if_false

    Good luck.

Re: Initializing a hash using conditions
by stvn_skuo (Initiate) on Feb 04, 2005 at 07:07 UTC
    Use the repetition operator:
    my $i = 1; my %hash = ( ('abc' => 7) x (! $i), 'hk' => 5, 'jk' => 6 ); my %other_hash = ( ('abc' => 7) x (!! $i), 'hk' => 5, 'jk' => 6 );