nested loops

nested loops

am 21.12.2010 01:04:45 von Mark M Huntress

I want to nest a for loop in a for loop in a while loop, and I can't even g=
et just the first nesting to work yet. Here is the relevant part of my scri=
pting.=20



open(XYZ,"$ARGV[0]ready.xyz");
#while () {
#chomp($_);
for ($z =3D 0, $z <=3D $#sortedstart, $z++) {
for ($i =3D $sortedstart[$z], $i <=3D $sortedend[$z], $i++) {
if ($_ =3D~ /^\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. Wh=
en it prints the values, it only gives $z=3D1.=20

Does anyone see why this isn't working?=

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: nested loops

am 21.12.2010 01:17:13 von Uri Guttman

>>>>> "MMH" == Mark M Huntress writes:

MMH> I want to nest a for loop in a for loop in a while loop, and I
MMH> can't even get just the first nesting to work yet. Here is the
MMH> relevant part of my scripting.

MMH> open(XYZ,"$ARGV[0]ready.xyz");

don't use @ARGV stuff like that before you check that you have args.

always check for success with open calls

also use lexical handles for i/o.

all of these are common points you can google for more info


MMH> #while () {
MMH> #chomp($_);
MMH> for ($z = 0, $z <= $#sortedstart, $z++) {

where did you get that syntax from? it isn't any normal perl loop. in
fact it is a foreach loop over 3 values (afaict) and nothing close to
what you envisioned it to be.

read perldoc perlsyn for the two ways to write for loops - a c style and
a perl foreach style. yours is a strange conjunction of the two styles
and makes no sense at all.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: nested loops

am 21.12.2010 01:23:07 von Shawn H Corey

On 10-12-20 07:04 PM, Mark M Huntress wrote:
> open(XYZ,"$ARGV[0]ready.xyz");

The three argument open is preferred and always check your opens for errors.

my $file = "$ARGV[0]ready.xyz";
open my $xyz_fh, '<', $file or die "could not open $file: $!\n";


> #while () {
> #chomp($_);

while( <$xyz_fh> ){
chomp;

> for ($z = 0, $z<= $#sortedstart, $z++) {
> for ($i = $sortedstart[$z], $i<= $sortedend[$z], $i++) {

It's hard to determine what these for statements are doing without
knowing what's in the arrays.

> if ($_ =~ /^\s+$i\s+(\D).*\s+(.+\.\d+\s+.+\.\d+\s+.+\.\d+)\s+/) {

For complex patterns, use the /x to allow whitespace in them. See
`perldoc perlre` and read the section on "Modifiers"

> $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?


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: nested loops

am 21.12.2010 02:40:11 von Rob Dixon

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 () {
> #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-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: nested loops

am 21.12.2010 03:30:11 von jwkrahn

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");

Better as:

open XYZ, '<', "$ARGV[0]ready.xyz"
or die "Cannot open '$ARGV[0]ready.xyz' because: $!";


> #while () {
> #chomp($_);
> for ($z = 0, $z<= $#sortedstart, $z++) {

You are iterating over a list of three numbers. If @sortedstart is
empty then you get:

for $_ ( 0, 0, 0 ) {

If @sortedstart is NOT empty then you get:

for $_ ( 0, 1, 0 ) {

What you probably want is:

for my $z ( 0 .. $#sortedstart ) {


> for ($i = $sortedstart[$z], $i<= $sortedend[$z], $i++) {

Again, you are iterating over a list of three numbers. If
$sortedend[$z] is less than $i you get:

for $_ ( $sortedstart[$z], 0, $sortedstart[$z] ) {

If $sortedend[$z] is NOT less than $i you get:

for $_ ( $sortedstart[$z], 1, $sortedstart[$z] ) {

What you probably want is:

for my $i ( $sortedstart[ $z ] .. $sortedend[ $z ] ) {




John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/