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 dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

variable within a variable 1

Status
Not open for further replies.

jpasquini

Programmer
Apr 20, 2006
44
US
Hello Forum,

I have ransacked Google until my brain is fried, and read at least a dozen explanations of a variable within a variable (including reading 'perldoc -q expand').
Also, the 'wonder line' in another post: "$line =~ s/(\$\w+)/$1/eeg" where someone said "THANK YOU, YOU SOLVED EVERYTHING!!!" and not another word.

What in the name of the dungeons of Saddam is eeg? I have no idea and can't understand any of the other posts.


Basically what I am trying to do, is have @Workbutton contain several work buttons, like "Create", "Validate", "Exit".

So is it possible to say something like ${"$WorkButton"_b}?
i.e., to use it to define several TK buttons in a loop, Create_b, Validate_b, Exit_b?

I've done it in other languages, but Perl has me drooling. I picture maybe "perl" referring to wrenching what you need out of a tightly closed clam, after hours of finger numbing labor.......maybe that's unfair..........




 
Here you go
Code:
#!/usr/bin/perl

my @workbutton = (
    'Create','Validate','Exit'
);

for my $i ( 0 .. $#workbutton ) {
    ${"$workbutton[$i]\_b"} = $i;   
}

print "$Create_b";

M. Brooks
 
This kind of thing always makes me vaguely uneasy (probably as a result of using 'created' variables in the OS/390 macro assembler in a previous life). If the language you are using has no other way of doing it, fine; when you have a language that supports proper arrays and hash maps, why go to the bother of creating compound variable names?

'use strict;' is your friend. If it complains about something, it's usually with good reason. What's wrong with
Code:
#!/usr/bin/perl
use strict;

my @workbutton = (
    'Create','Validate','Exit'
);

my %buttons;
 
for (my $i = 0; $i < @workbutton; $i++) {
    $buttons{$workbutton[$i]} = $i;   
}

foreach (sort keys %buttons) {
	print "\$buttons{$_} = $buttons{$_}\n";
}
? It does a similar job, but it's much cleaner, and you don't have to construct and evaluate compound names.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
mbrooks,

Why did you have 'print "$Create_b";' at the end? I don't get that part. Have tried the code and am getting, "Bareword i not allowed while strict subs in use.." Am looking....
 
Ah. There you go. That troublesome 'use strict;' again...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
So is it possible to say something like ${"$WorkButton"_b}?
i.e., to use it to define several TK buttons in a loop, Create_b, Validate_b, Exit_b?

Isn't this what you described?

As for the bareword error. Since the variable name is dependent on element names of the array.
They are not being defined using 'local' or 'my'.

If you add
Code:
my $Create_b;
my $Validate_b;
my $Exit_b;

then you won't get those errors.




this to the hader of the script you will not get this error.

M. Brooks
 
mbrooks

I wasn't disputing that you can do it (I ran your example and it works perfectly). I was just pointing out that there are other ways to go about it, and just because you can do something in a language, it doesn't necessarily mean that you should.

If jpasquini is used to other less flexible languages he/she might be more comfortable with the concept of using a hash.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
cue ishnid with a post on the danger of soft references ;-)

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
mbrooks, steve,

This code in my case did the trick (save the commands, will work them out). Thanks for your help!

jpasquini



####
#### Auto workbuttons
####

my (@workbutton_f,@workbutton_b);

my @workbuttons = (
'Create','Validate','Exit'
);

for my $i ( 0 .. $#workbuttons ) {


$workbutton_f[$i] = $w_Levels-> Frame(
-width=> 17,
-height=> 2,
-background=> 'black',
-label=> "STEP $i",
)->pack(-side=> 'left', -padx=> 8 );

$workbutton_b[$i] = $workbutton_f[$i]->Button(
-state=> 'disabled',
-text => 'Create',
-font => "{Arial} 11 {bold}",
#-wraplength=> 54,
-width => 17,
-height=> 2,
-background => 'SeaGreen3',
-activebackground => 'magenta',
-foreground => 'blue',
-activeforeground => 'blue',
-borderwidth=> 3,
-relief => 'solid',
-relief => 'raised',
-command => \&create_b_click,
)->pack();


}
 
If you make those variables from mbrooks' first post lexical with `my', you're not changing the values of them at all. Soft references operate on package variables, not lexicals. If you *really* do want that code to work under strict (and you'd have to turn off strict refs anyway), you'd need to do something like:
Code:
#!/usr/bin/perl -w
use strict;

my @workbutton = (
    'Create','Validate','Exit'
);

for my $i ( 0 .. $#workbutton ) {
    no strict 'refs';
    ${"$workbutton[$i]\_b"} = $i;
}

print "$::Validate_b";

or

Code:
#!/usr/bin/perl -w
use strict;

our ( $Create_b, $Validate_b, $Exit_b );

my @workbutton = (
    'Create','Validate','Exit'
);

for my $i ( 0 .. $#workbutton ) {
    no strict 'refs';
    ${"$workbutton[$i]\_b"} = $i;
}

print "$Validate_b";

I feel a bit dirty for posting that :)

Using soft references is asking for trouble. They can be extremely useful when you really know what you're doing. In cases such as this, however, it's purely a result of being too lazy to use a hash. I agree with stevexff's posts above on that. Have a look at this excellent article on the subject. That's the one that Paul's correctly predicted I'd post :)
 
I was giving you till this evening ;-)

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 

Good article.
I really liked this paragraph, it's worth framing:

"When people come into XXXXX asking how to do something stupid, I'm never quite sure what to do. I can just answer the question as asked, figuring that it's not my problem to tell people that they're being stupid. That's in my self-interest, because it takes less time to answer the question that way, and because someone might someday pay me to clean up after their stupidity, as happened in this instance. But if I do that, people might jump on me for being a smart aleck, which has happened at times. (``Come on, help the poor guy out; if you know what he really needs why don't you just give it to him?'')"

LOL---- yes, name your profession, and you will run into people like this. But never underestimate the power of us clods in large groups............just like cattle we will eventually find the way out ;)
 
Come to think of it, I think that was the same guy I talked to at the IRS about my tax return...............
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top