in reply to How to create variables for each hash key that has a value.

Yes it's possible with package variables but bad practice because if you can't control the keys how do you avoid naming conflicts?

It would be better to choose a very short hash name.

update
> create variables (array or scalar) only for those hash keys that are not empty

this is a very puzzling request, because how do you write code for variables which may or may not exist?

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

  • Comment on Re: How to create variables for each hash key that has a value.

Replies are listed 'Best First'.
Re^2: How to create variables for each hash key that has a value.
by Perl300 (Friar) on Jun 26, 2019 at 21:56 UTC

    Thank you LanX and GrandFather for your response.

    this is a very puzzling request, because how do you write code for variables which may or may not exist?

    These are fields that will/won't be sent from front end. My code for each of these fields is in separate if(@arr_0), if($scal_1) blocks. So if any of these variables don't exist, the related code will not execute.

    Why? What is the bigger goal?

    The reasons I am trying to find if this is possible is:

    1) If this is possible, I'll create variables only for those fields that were selected by user. (And so will have non empty values)

    2) This will also prevent need for any immediate code changes at back end when fields are added or removed from the front end. If a field is removed from front end, the variable will not be created for it and related code block for that field will not execute. When fields is added at least the variable will be created for new field. I can make use of that. I'll still have to write code specific to that field.

    Do you know that a hash value can be anything, including a reference to another (nested) hash or a reference to an array?

    Thank you for suggesting this. I know about it but will be thinking in that direction now. Will try to see if I can make use of this somehow.

      > These are fields that will/won't be sent from front end. My code for each of these fields is in separate if(@arr_0), if($scal_1) blocks. So if any of these variables don't exist, the related code will not execute.

      This is a misconception, a variable must already exist to be tested the way you do (at least under use strict ) , but it can hold a false value if you wanna disable the block.

      It looks like all var-names are known beforehand, otherwise you wouldn't be able to write code testing it!

      hence you are free to write

      our @arr_0 = @{ $hash{arr_0} // [] } ; our $scal_1 = $hash{scal_1} ;

      both will be false if missing.

      BTW use exists not defined to test hash elements.

      still a very weird requirement, about how many variables are we talking?

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice