By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,177 Members | 976 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,177 IT Pros & Developers. It's quick & easy.

How to replace the predefined variable which is set after the decleration

P: 3
I have a hash , and in the value part of that (which is a string , So that variable interpolation can happen) i have used a variable .
Now later in the program i set this variable and try to substitute the value of it in the value of hash but it is returning null;

Expand|Select|Wrap|Line Numbers
  1. %Hash=('add'=> " test  $type ");
  2. $type = 1000;
  4. print " $Hash{$type};";
Aug 15 '09 #1
Share this Question
Share on Google+
4 Replies

Expert Mod 2.5K+
P: 3,503
You have to define the variable $type before you use it somewhere, otherwise, the place where its used will be filled with a null value.

My suggestion is that you add the following to the beginning of your script:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
If you had them in place, you would have seen the following output to your screen:

Expand|Select|Wrap|Line Numbers
  1. Global symbol "%Hash" requires explicit package name at line 20.
  2. Global symbol "$type" requires explicit package name at line 20.
  3. Global symbol "$type" requires explicit package name at line 21.
  4. Global symbol "%Hash" requires explicit package name at line 23.
  5. Global symbol "$type" requires explicit package name at line 23.
  6. Execution of aborted due to compilation errors.
Also, this line:

Expand|Select|Wrap|Line Numbers
  1. print " $Hash{$type};";
is not correct. The first semi-colon needs to be rmoved so it looks like this:

Expand|Select|Wrap|Line Numbers
  1. print " $Hash{$type}";
Another issue is that $type is the value and the way you are referencing it is as if it were a key, so you will always get the following error:

Expand|Select|Wrap|Line Numbers
  1. Use of uninitialized value within %Hash in concatenation (.) or string at line 24.
To have this code work the way you expect it, you will have to have it as follows:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  5. # Variables
  6. my $type = "1000";
  7. my %Hash=('add' => " test  $type ");
  10. #print " $Hash{$type}";
  11. print("$Hash{add}");
I hope this helps.


Aug 15 '09 #2

P: 3
Thanks jeff ,
and print " $Hash{$type};"; was a typing mistake.

And what actually my problem is that i want to make the code more generic and to do that i use the variable inside the hash and i replace that variable at the runtime.

So, because of this reason i cant hard code the $type before the hash.

What i want is something like if i can some how refer to the variable and can change the value stored in that variable at the runtime .

Anyways thansk for the reply.
Aug 15 '09 #3

Expert 2.5K+
P: 4,059
You can change the value of a variable, but not when it is used in a string. When you put a variable in a string its value replaces the variable, and then perl builds the string. The variable is not stored in the string only its value.
Aug 16 '09 #4

P: 3
Thanks KevinADC for the reply..
Aug 16 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.