473,320 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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;
  3.  
  4. print " $Hash{$type};";
  5.  
Aug 15 '09 #1
4 2400
numberwhun
3,509 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

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

Similar topics

5
by: Stephan Hoffmann | last post by:
Hi, I use XML mainly as a source for HTML. HTML browsers 'know' certain entity references like é or ä. When I use XSL to transform XML to HTML or XML, these entities are replaced...
6
by: jamilsh | last post by:
I have seen lot of C/C++ code but couldn't understand why we use letter 'm' with lot of variable declaration. See example below. private: static NXTSTRUCT_t mTaGat; static DDXYZSTRUCT_t...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
8
by: Peter van Schie | last post by:
Hi all, Give an xml document that looks something like this: <?xml version="1.0" encoding="iso-8859-1"?> <?xml-stylesheet type="text/xsl"...
3
by: estafford | last post by:
Newbie looking for some insight here. I am writing a block to check limitations on a query string as follows: public bool validNumber(string qstring){ bool isValidNumber; foreach(char c in...
6
by: Chris Anderson | last post by:
Anyone know of a fix (ideally) or an easy workaround to the problem of escape characters not working in regex replacement text? They just come out as literal text For example, you'd think that thi...
1
by: jagdpenta | last post by:
hi all i 'm new to this forum please send me reply and take me out from this problem What is the php predefined variable that tells the What types of images that php supports E-Mail address...
3
by: Hvid Hat | last post by:
Hi I want to highlight (make it bold) a word in some text I'm getting in XML format. My plan was to replace the word with a bold (or span) tag with the word within the tag. I've found the code...
2
by: =?windows-1252?Q?=22=C1lvaro_G=2E_Vicario=22?= | last post by:
Is there any way to tell PHP predefined variables ($GLOBALS, $argv, $argc, $_GET, $_POST…) from *global* user-defined variables? Neither $GLOBALS nor get_defined_vars() put user data apart. I’m...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.