On 21/12/2010 00:04, Mark M Huntress wrote:
I want to nest a for loop in a for loop in a while loop, and I can't
even get just the first nesting to work yet. Here is the relevant
part of my scripting.
open(XYZ,"$ARGV[0]ready.xyz");
#while (<XYZ>) {
#chomp($_);
for ($z = 0, $z<= $#sortedstart, $z++) {
for ($i = $sortedstart[$z], $i<= $sortedend[$z], $i++) {
if ($_ =~ /^\s+$i\s+(\D).*\s+(.+\.\d+\s+.+\.\d+\s+.+\.\d+)\s+/) {
$count++;
print CON " $count $count $1 0 $2\n";
}
print "\nz is $z\n";
print "\ni is $i\n";
}
}
I am expecting it to cycle through several $i values and also $z
values. When it prints the values, it only gives $z=1.
Does anyone see why this isn't working?
Hi Mark
You have written the for statements
for ($z = 0, $z<= $#sortedstart, $z++) {
for ($i = $sortedstart[$z], $i<= $sortedend[$z], $i++) {
with commas instead of semicolons. Also, this is the C form of the for
loop, and almost never the right thing to do in Perl. Instead use
for my $z (0 ... $#sortedstart) {
for my $i ($sortedstart[$z] ... $sortedend[$z]) {
The advice you have been given in the other replies is good, and you
should take note of that as well.
HTH,
Rob
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/