newbie question

A

Abhinav

Hi,

I have the following piece of code which checks for a word within <> .
What I want to do is, if this matches, I want to change the value of a
variable, which is having an integer value, and whose name is same as
the matched string, to 1.

Thus, if i match <x>
I need to do
$x = 1 ;

Is there any way i can use the value of the matched text ($1) to create
the variable name ?

The code is :

if (/<(\w+)>/)
{
# What to put here , so that the corresponding variable gets declared
# and initialized ?
#something like,

my '$'.$1 = 1 ; # I know it is wrong, but just to signify what i want ..
}



Thanks
AB
 
A

Anno Siegel

Abhinav said:
Hi,

I have the following piece of code which checks for a word within <> .
What I want to do is, if this matches, I want to change the value of a
variable, which is having an integer value, and whose name is same as
the matched string, to 1.

Thus, if i match <x>
I need to do
$x = 1 ;

Is there any way i can use the value of the matched text ($1) to create
the variable name ?

The code is :

if (/<(\w+)>/)
{
# What to put here , so that the corresponding variable gets declared
# and initialized ?
#something like,

my '$'.$1 = 1 ; # I know it is wrong, but just to signify what i want ..
}

Use a hash for that. Creating variable names from data outside the
program is a bad idea. See "perldoc -q 'variable name'" for the
reason.

my %matched;
$matched{ $1} = 1 if /<(\w+)>/;

Then use $matched{ a} instead of $a.

Anno
 
J

Jim Cochrane

Hi,

I have the following piece of code which checks for a word within <> .
What I want to do is, if this matches, I want to change the value of a
variable, which is having an integer value, and whose name is same as
the matched string, to 1.

Thus, if i match <x>
I need to do
$x = 1 ;

You'll greatly increase your chances of getting useful help by summarizing
your question in the subject field - 'newbie question' is way too general.
Many experts will just ignore such subjects.

You're lucky Anno is so patient :)
 
A

Abhinav

Jim said:
[SNIP]


You'll greatly increase your chances of getting useful help by summarizing
your question in the subject field - 'newbie question' is way too general.
Many experts will just ignore such subjects.
Hi Jim,

You are right ! But i just _couldn't_ come up with a short but
descriptive subjectfor this one .. :( :)
I didnt try hard enough ... in fact, it wont be wrong to say that I
didnt try at all ..
You're lucky Anno is so patient :)

True ..Thanks to you both :)

Regards
AB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top