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

for ($count=1; $count<=32; ++$count) { ..Not working

Status
Not open for further replies.

ability1

Programmer
Dec 24, 2002
23
US
I have the following counter

for ($count=1; $count<=32; ++$count) {
$text = ${'text_e_gallery'.$count};
}

I have a flat file that I require that has the follwing scalar values

$text_e_gallery1='text blah blahh';

I can print out the values one at a time but when I try to print for each scalar value. it only prints $text_e_gallery32 over and over again (32 times).
 
When you pull your information out of the flat file, instead of assigning them to scalars, assign each line to an element in an array. This way, you can handle your prints later in the program like the following:

Code:
open (FILE, "/dir/to/file.ext");

my @text_e_gallery = <FILE>;

close (FILE);

chomp @text_e_gallery;

foreach my $text (@text_e_gallery)
{
    print $text;
}

- Rieekan
 
all that did is print out all the data of the flat file like

$scalar1='data for this variable';$scalar2='data for this variable';$scalar3='data for this variable';$scalar4='data for this variable';$scalar1='data for this variable';$scalar5='data for this variable';$scalar6='data for this variable';$scalar7='data for this variable';$scalar8='data for this variable';$scalar9='data for this variable';$scalar10='data for this variable';$scalar11='data for this variable';
 
Does your text file actually look like:

Code:
$text_e_gallery1='text blah blah';
$text_e_gallery2='text blah blah';
$text_e_gallery3='text blah blah';
$text_e_gallery4='text blah blah';
 
yes.

All ive been doing is requiring the file then spiting out the scalar values.

 
I got ya, is there any reason you don't want to store all the scalars in the flat file in an array? Typically, trying to reference scalars as you are doing isn't a great idea. In fact, if you have the using the strict refs pragma, it will complain.
 
They are stored in a flat file because different users edit the flat file and the perl automatically imports the flat file without changing the perl script. I guess I could write an array like

@array=($text_e_gallery1,$text_e_gallery2,$text_e_gallery3,$text_e_gallery4,$text_e_gallery5);

for each $text (@array){
print "$text";
}
But I figured there must be an easier way.
 
<file >
text1 blah blah
text2 blah blah
text3 blah blah
text4 blah blah
</file>

open FH, "/dir/to/file.ext";
@array=<FH>;
close FH;
foreach (@array) {
print $_;
}
#same thing
for ($i=0;$i=$#array;$i++) {
print $array[$i];
}

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top