Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create a variable named after the output of another 1

Status
Not open for further replies.

nix45

MIS
Nov 21, 2002
478
US
This is a re-post since the original thread was deleted for unknown reasons, which seems to be a common happening on Tek-Tips. They also locked my account for unknown reasons and didn't reinstate it until yesterday without telling me why.

I asked this question last week, and it was answered, but I never got a chance to write down what they said.

Lets say I have a variable called $var and its equal to 'perl'.

$var = 'perl';

How could I create an array named after whatever $var is equal to? In this case, the @perl array should be created.

Thanks,
Chris
 
Quite Simple...

@$var;

The $var is de-eferenced and 'perl' is put as the name to the array.
 
I am not trying to be offensive, but I am sitting here straining my brain (thanks :) ) to come up with a reason for the need to have a variable decide a variable.

Can you explain your need. Like I said, I am not trying to be offensive, just trying to learn too.

Thanks,



bd1.gif

If I wasn't Blue, I would just be a Dragon...
 
I've used dynamically configured variables many times. It made sense at the time ;)

I know one place it really helps is when you have a large data-set that is based on a looping variable, I had to do some oddness with this to handle this funky javascript tree menu application I integrated. It was purely for compact, logic-free and easy to manage code, no efficiency gains of any sort :)

 
logic free code <grin> sounds like your stuff alright siberian... :)

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Har har har ;)

I remember my concrete example now. I wrote a base class and then derived something like 10 other classes off it. The class to instantiate was determined by a CGI argument on a particular page.

So, rather then do all sorts of switching and logic to figure out what object to load I just used the contents of a the variable to instantiate the class.

That was bugging me! I hate it when I can't remember what I did ;)
 
thanks h011ywood, I was hoping it would be something easy like that.

bluedragon2, thats not offensive at all. I actually can't remember why I needed that (its been 2 weeks), but it was something like this...

foreach (@list) {
$count++;
if (something) {
@$count = qw(a b c);
} else {
@$count = qw(d e f);
}
}

I was trying to get a CGI script to create a dynamic HTML table using elements in a list and couldn't figure out how to create the vertical columns, so I figured I would do something like above. I know that doesn't make much sense. I'll probably post the full problem in the next day or so if I can't figure it out.

Thanks guys,
Chris
 
Thanks for the comments, I am not a programmer by trade (network security engineer), just get to dabble with it in my current postion, but I is learnin... :)



[Blue]Blue[/Blue] [Dragon]

If I wasn't Blue, I would just be a Dragon...
 
I'm in the same position as you. I'm mainly a Linux sysadmin, but I also deal with Cisco, NetWare, and sadly enough Windows. I just learned Perl this month and find it incredibly useful for system administration tasks. I've also been dabbling with CGI a bit since I learned it. Perl is the only language that I know right now, but I've been thinking of teaching myself Javascript next.

Chris
 
fyi, here's another reason why I wanted this. This is a subroutine inside a CGI spam filter that I wrote...

sub create_arrays {
$name = pop @_;
foreach (@_) {
if ($_ =~ /$search/i) {
push @$name, $_;
}
}
}

&create_arrays(@bayesian, matches_bay);
&create_arrays(@blacklist, matches_black);
&create_arrays(@keywords, matches_key);
&create_arrays(@headers, matches_head);


Chris
 
Hi nix45

i stumbled across this post as a bit of luck, i am looking to write a spam filter and need a starting point. I was wondering if it would be possible for you to point me in the right direction?

thanks
Dan
 
COIS2004, I didn't write a spam filter, I just wrote a CGI interface to search through the log files that our spam filter creates.

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top