in reply to Re: refering to a variable by using other variables
in thread refering to a variable by using other variables

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.

  • Comment on Re^2: refering to a variable by using other variables

Replies are listed 'Best First'.
Re^3: referring to a variable by using other variables
by Athanasius (Archbishop) on Jul 21, 2014 at 12:50 UTC
Re^3: refering to a variable by using other variables
by roboticus (Chancellor) on Jul 21, 2014 at 14:12 UTC

    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.

      Hello Roboticus Sorry i don't understand your code answer and it doesn't seem to work either? Was going to test it so that i could make sense out of it..But it fails. were you going for something like?
      $varbindhash{"OID_$i"} = $OIDs[$i];
      Also the soft reference solution didn't work either since it required turning of use strict; I can't put everything inside one hash and post it to elastic since i want each line containing 3 values of an unknown number of lines to be viewed by itself with only the values belonging to that line..If that makes any sense=). If i put and post everything in a hash it will look like one variable with all the values in it. Anyway sorry for the late reply just didn't have time to try to elaborate the reference problem..Will try now.. One of the ARGVs that comes with NetSNMP::TrapReceiver::register is for SNMP VARBINDs which have references to a multi dimensional array. And i just can-t manage to de-reference that multiarray reference when put into a new hash or get them approved as JSON format. I don't really understand how perl references works but single arrays has no de reference problem as seen below must be something special with multi but i can't figure it out.

      Here is a simulation of what it looks like (see last line), the fun thing is that together with JSON function to_json() it gets de-referenced:

      #!/usr/bin/perl use strict; #use JSON; my $alex = "alex"; my $alexAge = "35"; my $tristan = "tristan"; my $tristanAge = "5"; my $krista_bell = "kristabell"; my $krista_bellAge = "2"; my %newHash = ("$alex" => "$alexAge", "$tristan" => "$tristanAge", "$k +rista_bell" => "$krista_bellAge"); my @array = ('test_0_0', 'test_0_1', 'test_0_2'); my $arrayRef = \@array; printf("\narray = @array, ArrayRef = $arrayRef, de-referenced arrayRef +: @{$arrayRef}, de-referenced arrayRef2: @$arrayRef\n\n"); my @multiArray = ( ['value_on_0_0', 'value_on_0_1', 'value_on_0_2'], ['value_on_1_0', 'value_on_1_1', 'value_on_1_2'], ['value_on_2_0', 'value_on_2_1', 'value_on_2_2'] ); my $multiArrayRef = \@multiArray; printf("\nmultiArray = @multiArray, MultiArrayRef = $multiArrayRef, de +-referenced multiArrayRef: @{$multiArrayRef}, de-referenced multiArra +yRef2: @$multiArrayRef\n\n"); printf("\n\n"); my $i = 0; my $c0 = 0; my $c1 = 1; my $c2 = 2; foreach my $x (@multiArray) { printf("\n\nX Ref: $x,\nX De-Ref: @$x,\nRow[$i] Column[$c0]: $ +x->[0], Row[$i] Column[$c1]: $x->[1], Row[$i] Column[$c2]: $x->[2]"); printf("\nVariations on first value on row[$i]: x[0] = $x->[0] + OR x[0] = @$x[0] OR x[0] = ${$x}[0]"); my %varbind = ( ("row" . "$i" . "c" . "$c0") => @$x[0], ("row" . "$i" . "c" . "$c1") => @$x[1], ("row" . "$i" . "c" . "$c2") => @$x[2] #("row" . "$i" . "c" . "$c0") => $x->[0], #("row" . "$i" . "c" . "$c1") => $x->[1], #("row" . "$i" . "c" . "$c2") => $x->[2] ); printf("\n\n Value of current varbind: "); print %varbind; push(@{$newHash{"varbinds"}}, {%varbind}); $i++; } printf("\n\nNext line will be printout of newhash\n"); print %newHash; printf("\n\n\n"); #printf("\nto_json(%newHash)");
      And here is the output.

      array = test_0_0 test_0_1 test_0_2, ArrayRef = ARRAY(0x17dc7b8), de-re +ferenced arrayRef: test_0_0 test_0_1 test_0_2, de-referenced arrayRef +2: test_0_0 test_0_1 test_0_2 multiArray = ARRAY(0x153b088) ARRAY(0x153b688) ARRAY(0x17dce18), Multi +ArrayRef = ARRAY(0x17dca40), de-referenced multiArrayRef: ARRAY(0x153 +b088) ARRAY(0x153b688) ARRAY(0x17dce18), de-referenced multiArrayRef2 +: ARRAY(0x153b088) ARRAY(0x153b688) ARRAY(0x17dce18) X Ref: ARRAY(0x153b088), X De-Ref: value_on_0_0 value_on_0_1 value_on_0_2, Row[0] Column[0]: value_on_0_0, Row[0] Column[1]: value_on_0_1, Row[0] + Column[2]: value_on_0_2 Variations on first value on row[0]: x[0] = value_on_0_0 OR x[0] = val +ue_on_0_0 OR x[0] = value_on_0_0 Value of current varbind: row0c2value_on_0_2row0c1value_on_0_1row0c0v +alue_on_0_0 X Ref: ARRAY(0x153b688), X De-Ref: value_on_1_0 value_on_1_1 value_on_1_2, Row[1] Column[0]: value_on_1_0, Row[1] Column[1]: value_on_1_1, Row[1] + Column[2]: value_on_1_2 Variations on first value on row[1]: x[0] = value_on_1_0 OR x[0] = val +ue_on_1_0 OR x[0] = value_on_1_0 Value of current varbind: row1c2value_on_1_2row1c1value_on_1_1row1c0v +alue_on_1_0 X Ref: ARRAY(0x17dce18), X De-Ref: value_on_2_0 value_on_2_1 value_on_2_2, Row[2] Column[0]: value_on_2_0, Row[2] Column[1]: value_on_2_1, Row[2] + Column[2]: value_on_2_2 Variations on first value on row[2]: x[0] = value_on_2_0 OR x[0] = val +ue_on_2_0 OR x[0] = value_on_2_0 Value of current varbind: row2c0value_on_2_0row2c2value_on_2_2row2c1v +alue_on_2_1 Next line will be printout of newhash varbindsARRAY(0x15562a8)alex35tristan5kristabell2

Re^3: refering to a variable by using other variables
by AppleFritter (Vicar) on Jul 21, 2014 at 12:48 UTC

    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] };