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

Something other then foreach 1

Status
Not open for further replies.

skosterow

Technical User
Feb 23, 2002
135
0
0
US
I have been useing foreach alot lately (just begining perl) and i was woundering if theres something else besides foeach that i can use.

the way im using it is like this:

foreach (@data_read01){
@split_data01 = split (/\|/, $data_read01[$counter01]);
chomp @split_data01;
$hold_data .= &quot;<tr><td></td><td>&nbsp;$split_data01[0]&nbsp;</td><td align=\&quot;center\&quot;>&nbsp;$split_data01[1]&nbsp;</td></tr>&quot;;
$counter01++;
}

but im having to reset $counter01 and use that $counter01++ all the time

any thoughts?
 
Hi,
There is &quot;for loop&quot; , &quot;while loop&quot;, &quot;do while&quot; or &quot;repeat loop&quot; to me it is the samething.

#############################################

for &quot;for loop&quot; is like
for ($line=0; $line<@data_read01; $line++) {
@split_data01= split(/\|/,$data_read01[$line]);
etc....
}

###########################################

for &quot;wile loop&quot; is is more tricky

you open the file like this
Open(FILE, &quot;.....&quot;); #as usual
## but you don't do @data_read=<FILE> ; close(FILE);
#so you do
while(<FILE>){
chomp $_;
@split_data01=split(/\|/,$_);
##and do the rest of the code ......

}

###################################################3

for &quot;Repeat loop&quot; I'm not too sure i'have never used with files;
I guest you can do
open(FILE,....);
@data_read01=<FILE>;
close(FILE);
$number_lines = $#data_read01 - 1; #to get length of the #file
$counter = 0 ;
until($counter == $number_lines){
chomp $data_read01[$counter];
@split_data01= split (/\|/,$data_read01[$counter];
etc.......
$counter ++;
}

##########################################3

for &quot;do while&quot; I never use it.
but i thing (i might be way off)

Do{
#statments;
}while(<FILE>);


hope this helps
Anthony
 
Thanks Anthony the first one works GREAT!!!!
 
Only the first ONE ????
Well what about the others.

also for the repeat loop maybe
you should do for $number_lines
$number_lines =$#data_read01
Because i don't know if it does or does not coun't the EOF the last empty line.

Anthony
 
for($i=0;$i<@data_read01;$i++){
@split_data01 = split (/\|/, $data_read01[$i]);
chomp @split_data01;
print &quot;<tr><td></td>&quot;;
#nested for statement (always make sure the index letters
# (j, and i) are different
for($j=0;$j<@split_data01;$j=$j++){
$hold_data .= &quot;<td>$split_data01[$j] </td>\n&quot;;
}
print &quot;</tr>&quot;;
}

If you have to have that align=center in the second <td>
increment by 2 and add 1 to the second index like this:

for($j=0;$j<@split_data01;$j=$j+=2){
$hold_data .= &quot;<td>$split_data01[$j] </td><td align=center>$split_data01[$j+1] </td>\n&quot;;
}
Thanks
Tricia
yorkeylady@earthlink.net
 
Actually im going to use all of them in different places but where i needed it the first one worked great! I open file read them then do something to the data.

no offense!

;-)
 
Why do not you use the foreach with a variable argument.
Code:
foreach VAR (LIST) BLOCK

So you could write:
Code:
foreach my $data01 (@data_read01){
  @split_data01 = split (/\|/, $data_01);
  chomp @split_data01;
  $hold_data .= &quot;<tr><td></td><td> $split_data01[0] </td><td align=\&quot;center\&quot;> $split_data01[1] </td></tr>&quot;;
}

Another way:
Code:
foreach (@data_read01){ # no VAR : defaults to $_
  my ($a, $b) = split /\|/; # no EXPR : splits $_
  chomp $a, $b;
  $hold_data .= &quot;<tr><td></td><td> $a </td><td align=\&quot;center\&quot;> $b </td></tr>&quot;;
}

For more infos:
Code:
perldoc -f split
perlfoc perlsyn
 
And what do you think of this:
Code:
$hold_data = join &quot;&quot;,
    map {
	chomp(my ($a, $b) = split /\|/);
	&quot;<tr><td></td><td> $a </td><td align=\&quot;center\&quot;> $b </td></tr>&quot;;
    } @data_read01;
to replace the whole foreach loop.

Denis,
trying hard to look like a perl guru.
 
thankks gals and guys!!!

I love this site!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top