PDA

View Full Version : Perl Problem


sambartle
15-02-2004, 08:14
I know this is probably really easy...

Is there a way to attach a number onto the end of a scalar name inside another scalar variable..

for i = 1 to 3 (i know this isnt perl's for - its just for easiness)
$currentpart = '$part' + i;
print "$currentpart";
next i

id like it to print out...

$part1
$part2
$part3

(ie add the i value to the $part bit (not the contents of $part))

i tried to use . as well for string joining ('$part' . i) but am happy to report i don't know perl and at this rate never will.

shifty.ricky
15-02-2004, 21:27
you could just do

print "$part$i";

burgi
19-03-2004, 15:20
If you're new to perl I would highly recommend the O'Reilly series of books, specifically:

'Learning Perl' ISBN 1-56592-284-0 - great for noobs
'Perl Cookbook' ISBN 1-56592-243-3 - great examples - tells you the best way to do almost anything you'd want
'Programming Perl' ISBN 0-596-00027-8 - the Bible - co-authored by Larry Wall - Perl's inventor.

Re: your question

Perl is a dodgy old language but it lets you do cool stuff very easily, try this:

$var1 = "This is variable one";
$var2 = "This is variable two";
$var3 = "This is variable three";
for($i = 1; $i < 4; $i++) {
$loopVar = "var".$i;
print "The content of \$$loopVar is: ".$$loopVar."\n";
}

Burgi

sambartle
22-03-2004, 18:46
Thanks to both of you, ive solved the problem now.

burgi ill definitly have a look at those as Perl is something i'd liek to learn more about.