PDA

View Full Version : Having trouble with strings ...


mlseim
11-25-2003, 08:07 PM
For the life of me, I just can't figure this out ...

Take this example:

$start = 1;
$end = 10;

for $list ($start..$end) {
$item="thing$list";
@$item=("zero","one","two","three");
}

I end up with 10 arrays ...
@thing1=("zero","one","two","three");
@thing2=("zero","one","two","three");
@thing3=("zero","one","two","three");

... etc ...

Now I want to print out one of the items in one array ...

$test=3;
print "Here's an item --- $thing$test[2] .... ";

See, I want to print out the item in array "thing3[2]"
I can't figure out how to represent "thing3[2]" in the
print statement, because the "3" is a variable ($test)

I want the print statement to look like this in the end:
Here's an item --- two ....

If I do this,
print "Here's an item --- $thing3[2] ... ";
It works OK.

I just can't make the "3" a variable.

dswimboy
11-26-2003, 05:47 AM
@thing3=("zero","one","two","three");

$i = 3;
$name = "thing" . $i;
$item = $$name[2];
print "$item\n";


does that solve your problem?

mlseim
11-26-2003, 02:10 PM
Thankyou! That is it!!

I've programmed in VBasic, C++ ... why didn't I know
that I could use "$$" double dollar signs?
(slapping myself on my forehead)

That's why this forum is here ...

Thanks again for the help,

Max

Jeff Mott
11-27-2003, 04:03 AM
Note though that symbolic references is considered sloppy programming. You could trymy @thing;
$thing[$_] = [qw[zero one two three]] for 0 .. 9;

my $item = 3;
print "Here's at item --- $thing[$item][2]";