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

I know this might sound a bit off the wall but there is a method to my madness. What I'm trying to do is put 2 variables together, one being a hash key and a number from a for loop inside the brackets...i.e. $hash{'$a'}. As is obvious the keys are numbers.

This brings up another question...is it possible to count the number of keys in a hash?

Basically what I'm trying to do is create a new hash in a new txt file using input from a form, or, if no input is in the form use the value from an existing hash. Form inputs listed as "input1, input2, etc.,. Like using a form to make it easier for a user to create a language file in a different language by reading the existing file and entering the info for the new file in the form.

Something like(not yet tried due to the variable issue)

tie (%hash, "DB_File", $dbfile) or die "Can't open $dbfile: $!\n"; %hash = ( for($b=0;$b<$cnt;$b++){$a=$b+1; #$cnt being the number of keys in the +hash $new"."$a="new_input_from_form"."$a";#. form is in a .pl file if($new"."$a ne ""){$new_key_and_value="'$a' => '$new"."$a',";} #use v +alue from the form in the hash elsif($new"."$a eq ""){$new_key_and_value="'$a' => '$hash{'$a'}',";} # +use the old value from the existing hash $new_key_and_value }#close the for loop ); #close the hash untie %hash

Replies are listed 'Best First'.
Re: Double Variables
by jwkrahn (Abbot) on Jun 23, 2011 at 04:02 UTC
    is it possible to count the number of keys in a hash?

    Yes it is, using the keys operator in scalar context:

    my $number_of_keys_in_hash = keys %hash;


    %hash = ( for($b=0;$b<$cnt;$b++){$a=$b+1; #$cnt being the number of keys in the +hash $new"."$a="new_input_from_form"."$a";#. form is in a .pl file if($new"."$a ne ""){$new_key_and_value="'$a' => '$new"."$a',";} #use v +alue from the form in the hash elsif($new"."$a eq ""){$new_key_and_value="'$a' => '$hash{'$a'}',";} # +use the old value from the exi +sting hash $new_key_and_value }#close the for loop ); #close the hash

    You can't enclose multiple statements inside parentheses, they don't work like that.    You have to use a code block of some kind.

    The expression  $new"."$a makes no sense.    There is no operator between the $new variable and the string "." and between the string and the $a variable.

    The $b variable is only there to set the value of the $a variable so why use it at all?    And you really shouldn't use the $a and $b variables at all.

Re: Double Variables
by GrandFather (Saint) on Jun 23, 2011 at 04:13 UTC

    You'd be much better off telling us about the big picture, showing us a little of your input data and showing us what you want the output to look like. Aside from anything else, you don't really store a hash in a file, although you may store the data contained in a hash in a file. But if you are trying to do that you would probably be better off using either YAML or a database (DBD::SQLite perhaps).

    True laziness is hard work

      I'm putting together a "prototype" script of what I'm trying to accomplish and will post it in a few days. You mentioned about not storing a hash in a file. In the .txt file the info is as follows:

      my %hash={ 'key1' => 'value1', 'key2' => 'value2', )

      When the hash is tied I call the values like:

      print"The first value is $hash{'key1'}.";

      How should I go about this? I have so much to learn about perl and spend hours trying to figure things out.

        Don't worry about a prototype script as a way of showing us what you are trying to do. Instead describe in words what the project is about. If you show us code without giving us the background we can criticise the code for you, but we can't help with finding a good solution for the project. So far it looks like you have latched on to a solution for a small part of the project, but in fact could probably make better use of some higher level direction.

        True laziness is hard work