471,896 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,896 software developers and data experts.

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;
  4. print " $Hash{$type};";
Aug 15 '09 #1
4 2343
3,503 Expert Mod 2GB
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 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.
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 hashtest.pl 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
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
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 zermasroor | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.