469,273 Members | 1,638 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,273 developers. It's quick & easy.

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

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;
  3.  
  4. print " $Hash{$type};";
  5.  
Aug 15 '09 #1
4 2246
numberwhun
3,503 Expert Mod 2GB
@yash0101
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;
  3.  
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 hashtest.pl line 20.
  2. Global symbol "$type" requires explicit package name at hashtest.pl line 20.
  3. Global symbol "$type" requires explicit package name at hashtest.pl line 21.
  4. Global symbol "%Hash" requires explicit package name at hashtest.pl line 23.
  5. Global symbol "$type" requires explicit package name at hashtest.pl line 23.
  6. Execution of hashtest.pl aborted due to compilation errors.
  7.  
Also, this line:

Expand|Select|Wrap|Line Numbers
  1. print " $Hash{$type};";
  2.  
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}";
  2.  
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 hashtest.pl line 24.
  2.  
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;
  3.  
  4.  
  5. # Variables
  6. my $type = "1000";
  7. my %Hash=('add' => " test  $type ");
  8.  
  9.  
  10. #print " $Hash{$type}";
  11. print("$Hash{add}");
  12.  
I hope this helps.

Regards,

Jeff
Aug 15 '09 #2
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
KevinADC
4,059 Expert 2GB
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
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.

Similar topics

5 posts views Thread by Stephan Hoffmann | last post: by
6 posts views Thread by jamilsh | last post: by
134 posts views Thread by James A. Donald | last post: by
8 posts views Thread by Peter van Schie | last post: by
3 posts views Thread by estafford | last post: by
3 posts views Thread by Hvid Hat | last post: by
2 posts views Thread by =?windows-1252?Q?=22=C1lvaro_G=2E_Vicario=22?= | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.