How can I name my scalar with a for loop?

V

vorticitywolfe

How can I name/define my scalar with a for loop?

@stn=('A','B','C');

$count=0;
foreach($stn(@stn){

$Q.$count=$stn;

}

desired output:

Q1=A
Q2=B
Q3=C

Thanks for your help!

Jonathan
 
G

Gunnar Hjalmarsson

@stn=('A','B','C');

$count=0;
foreach($stn(@stn){

$Q.$count=$stn;

}

desired output:

Q1=A
Q2=B
Q3=C

use strict;
use warnings;
my @stn = ('A','B','C');
my $count = 0;
foreach my $stn ( @stn ) {
print 'Q' . ++$count . "=$stn\n";
}
 
V

vorticitywolfe

use strict;
use warnings;
my @stn = ('A','B','C');
my $count = 0;
foreach my $stn ( @stn ) {
print 'Q' . ++$count . "=$stn\n";
}

Thanks Gunnar,

Close to what I asked for, didn't quite work for my use, since I'm
trying to define the variable as $Q1 not just print Q1.

My goal is to define a bunch of identical labels in a perl tk gui
rather than repeating thousands of lines of code.
that is the final result will be like this:

$Q1=$side->Label(-text=>$stn1)->pack();

where $Q1 is defined by the for loop.

I appreciate the help though, I'll keep on fiddling and see if I can
get it to work.

Jonathan
 
J

Jürgen Exner

Close to what I asked for, didn't quite work for my use, since I'm
trying to define the variable as $Q1 not just print Q1.

Are you trying to use a variable's content as a variable name? Bad idea(TM).
See "perldoc -q 'variable name'" and gazillions of previous discussions
about symbolic references for explanations about why this is a particulary
bad idea and what to do instead.

jue
 
M

Martijn Lievaart

My goal is to define a bunch of identical labels in a perl tk gui rather
than repeating thousands of lines of code. that is the final result will
be like this:

$Q1=$side->Label(-text=>$stn1)->pack();

where $Q1 is defined by the for loop.

I appreciate the help though, I'll keep on fiddling and see if I can get
it to work.

Do you need something like this?

my $n=0;
for my $stn (@stn) {
$Q[$n++]=$side->Label(-text=>$stn)->pack();
}

Read up on arrays.

HTH,
M4
 
J

John Bokma

How can I name/define my scalar with a for loop?

@stn=('A','B','C');

$count=0;
foreach($stn(@stn){

$Q.$count=$stn;

}

desired output:

Q1=A
Q2=B
Q3=C


use strict;
use warnings;

my @stn = ( 'A' .. 'B' );
my %vars;
my $count = 1;

for my $name ( @stn ) {

$vars{ "Q$count" } = $name;
$count++;
}

print "$vars{ Q2 }\n";

Untested, should print B
 
R

Randal L. Schwartz

vorticitywolfe> Q1=A
vorticitywolfe> Q2=B
vorticitywolfe> Q3=C

If you ever find yourself naming a variable with a sequential distinguisher,
like "1" "2" "3" or "A" "B" "C", that's a sure sign that you're heading down
the wrong path, as those variables probably belong in some sort of larger data
structure.

Others have already given the details, but I wanted to make sure you
understood this.

print "Just another Perl hacker,"; # the original
 
D

Darren Dunham

Close to what I asked for, didn't quite work for my use, since I'm
trying to define the variable as $Q1 not just print Q1.

My goal is to define a bunch of identical labels in a perl tk gui
rather than repeating thousands of lines of code.
that is the final result will be like this:

$Q1=$side->Label(-text=>$stn1)->pack();

where $Q1 is defined by the for loop.

But the name 'Q1' isn't special, right? Don't use individually named
scalars, stick the references in an array.

my @label_refs;
foreach my $stn (@stn)
{
push @label_refs, $side->Label(-text=>$stn)->pack();
}

Depending on what you're doing, it might be easier to store in a hash so
you can pull them by name:

my %label_refs;
foreach my $stn (@stn)
{
$label_refs{$stn}=$side->Label(-text=>$stn)->pack();
}
 
V

vorticitywolfe

But the name 'Q1' isn't special, right? Don't use individually named
scalars, stick the references in an array.

my @label_refs;
foreach my $stn (@stn)
{
push @label_refs, $side->Label(-text=>$stn)->pack();

}

Depending on what you're doing, it might be easier to store in a hash so
you can pull them by name:

my %label_refs;
foreach my $stn (@stn)
{
$label_refs{$stn}=$side->Label(-text=>$stn)->pack();

}

--
Darren Dunham (e-mail address removed)
Senior Technical Consultant TAOS http://www.taos.com/
Got some Dr Pepper? San Francisco, CA bay area
< This line left intentionally blank to confuse you. >



Thank you everyone,

That is enough to get me somewhere :) I'll keep working out the
logistics and go from there! Thanks again!

Jonathan
 

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

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top