473,591 Members | 2,872 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

3 New Member
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 2412
numberwhun
3,509 Recognized Expert Moderator Specialist
@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
yash0101
3 New Member
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 Recognized Expert Specialist
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
yash0101
3 New Member
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
2307
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 by what they refer to. Is there a way to avoid that?
6
1546
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 mEditGat; Thanks,
134
7833
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 means that if I misspell a variable name, my program will mysteriously fail to work with no error message. If you don't declare variables, you can inadvertently re-use an variable used in an enclosing context when you don't intend to, or
8
1528
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" href="http://localhost/archiefassistent/xsl/fulldoc.xsl"?> <result> <currentpage>1</currentpage> <document id="5">
3
1294
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 qstring){ if(!Char.IsNumber(c)){ isValidNumber = false; break; }
6
8836
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 Regex.Replace("<stuff>text</stuff>", "<stuff>", "<stuff>\n" would give yo <stuff text</stuff
1
1800
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 blocked: See forum rules. moderator Thanks in advance
3
3948
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 below and it works fine as long as I'm not adding tags around the to parameter. Can anyone explain to me why it doesn't work with tags? And it needs to be XSLT 1.0. This works: X<xsl:value-of select="'little steak'"/>X This doesn't work:...
2
2688
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 writing a class to generate PHP definition files for syntax highlighting with two features: 1. It takes data from current install (e.g., you get function names from loaded extensions). 2. You can choose whether to include user data or not.
0
7935
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8236
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7995
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6642
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3851
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3893
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1467
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.