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

Hello, Was thinking i could do something like this and first declare a bunch of variables and then use them in some kind of loop where the counter indicated what variables was supposed to be used.
my %varbindhash0; my %varbindhash1; my %varbindhash2; my %varbindhash3; my %varbindhash4; my %varbindhash5; my $i = 0; while ($i < @OIDs) { $varbindhash$i{OID} = $OIDs[$i]; $varbindhash$i{OID_value} = $OID_VALUEs[$i]; $varbindhash$i{OIDs_data_types_id} = $OIDs_DATA_TYPEs_ID[$i]; $varbindhash$i{OIDs_data_types} = $DATA_TYPEs[$i]; $i++; }
But refering to the hash variable using $i doesn't work it seems...Is there a a way to make it work like i want or?

Replies are listed 'Best First'.
Re: refering to a variable by using other variables
by AppleFritter (Vicar) on Jul 21, 2014 at 12:22 UTC

    Howdy cocoon, welcome to the Monastery!

    What you're looking for there is soft (symbolic) references. You could do something along the following lines:

    while ($i < @OIDs) { my $varname = "varbindhash$i"; $$varname{'OID'} = $OIDs[$i]; ... }

    See Symbolic references in perlref for more, as well as Programming Perl chapter 8.

    But I would advise against doing this, and instead suggest turning your pile of hashes into an array of hashes:

    my @varbindhashes = (); ... while ($i < @OIDs) { $varbindhashes[$i]->{'OID'} = $OIDs[$i]; ... }

    Refer to perldsc for more on nested data structures, as well as Programming Perl chapter 9.

      Thank you for the welcome and the answer!! Awsome i am truly greatful. I have to go with approach 1 i am afraid since it started out as a array of hashes and references to arrays and the end product i need to post to elasticsearch as a searchable variable so If i post the array of hashes it just outputs a lot of reference addresses when looking at it from kebana and that will not do=) Anyway thank you so much again!! This will solve it.

        Hello, Was thinking i could do something like this and first declare a bunch of variables and then use them in some kind of loop where the counter indicated what variables was supposed to be used.
        my %varbindhash0; my %varbindhash1; my %varbindhash2; my %varbindhash3; my %varbindhash4; my %varbindhash5; my $i = 0; while ($i < @OIDs) { $varbindhash$i{OID} = $OIDs[$i]; $varbindhash$i{OID_value} = $OID_VALUEs[$i]; $varbindhash$i{OIDs_data_types_id} = $OIDs_DATA_TYPEs_ID[$i]; $varbindhash$i{OIDs_data_types} = $DATA_TYPEs[$i]; $i++; }

        I think you may have misunderstood AppleFritter and dismissed the response a little too quickly. Your code can easily be converted to:

        my %varbindhash; my $i = 0; while ($i < @OIDs) { $varbindhash[$i]{OID} = $OIDs[$i]; $varbindhash[$i]{OID_value} = $OID_VALUEs[$i]; $varbindhash[$i]{OIDs_data_types_id} = $OIDs_DATA_TYPEs_ID[$i]; $varbindhash[$i]{OIDs_data_types} = $DATA_TYPEs[$i]; $i++; }

        If you're having difficulty with the output (i.e., showing reference addresses instead of values) then I expect the error is elsewhere (i.e., where you're trying to output your results. Show that bit of the code, and we can show you the rest of the way.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

        You're welcome! *tips hat*

        I don't have any experience with ElasticSearch or its API, but I don't think using an array of hashes should be a big problem there. Given an array of hashes, you can recover the (anonymous) hashes it contains as follows:

        %hash = %{ $varbindhash[$i] };