Closed p5pRT closed 17 years ago
Hi!
When I try to execute this program:
#!/usr/bin/perl
$i=-20;
$j=-10;
$k=-5;
for(;$i\<=20;$i++){
for(;$j\<=10;$j++){
for(;$k\<=5;$k++){
printf("%d %d %d\n"\,$i\,$j\,$k);
}
}
}
I have the following result:
-20 -10 -5
-20 -10 -4
-20 -10 -3
-20 -10 -2
-20 -10 -1
-20 -10 0
-20 -10 1
-20 -10 2
-20 -10 3
-20 -10 4
-20 -10 5
that is only internal loop is executed by perl. Why?
When I initialize variables in for statement directly
(i.e. for($foo=-20;$foo\<=20;$foo++) {} instead of
$foo=-20;for(;$foo\<=20;$foo++){})
the problem disappears.
Output of perlbug -d attached.
Best regards\, Anton.
Flags: category= severity=
Site configuration information for perl v5.8.7:
Configured by Gentoo at Thu Feb 9 05:14:04 Local time zone must be set--see zic manual page 2006.
Summary of my perl5 (revision 5 version 8 subversion 7) configuration: Platform: osname=linux\, osvers=2.6.12-gentoo-r9\, archname=x86_64-linux uname='linux poseidon 2.6.12-gentoo-r9 #1 smp fri sep 2 18:47:52 utc 2005 x86_64 amd opteron(tm) processor 844 authenticamd gnulinux ' config_args='-des -Darchname=x86_64-linux -Dcccdlflags=-fPIC -Dccdlflags=-rdynamic -Dcc=x86_64-pc-linux-gnu-gcc -Dprefix=/usr -Dvendorprefix=/usr -Dsiteprefix=/usr -Dlocincpth= -Doptimize=-O2 -pipe -Duselargefiles -Dd_semctl_semun -Dscriptdir=/usr/bin -Dman1dir=/usr/share/man/man1 -Dman3dir=/usr/share/man/man3 -Dinstallman1dir=/usr/share/man/man1 -Dinstallman3dir=/usr/share/man/man3 -Dman1ext=1 -Dman3ext=3pm -Dinc_version_list=5.8.0 5.8.0/x86_64-linux 5.8.2 5.8.2/x86_64-linux 5.8.4 5.8.4/x86_64-linux 5.8.5 5.8.5/x86_64-linux 5.8.6 5.8.6/x86_64-linux -Dcf_by=Gentoo -Ud_csh -Ui_ndbm -Ui_gdbm -Di_db -Dusrinc=/usr/include/gentoo-multilib/amd64 -Dlibpth='/usr/local/lib64 /lib64 /usr/lib64'' hint=recommended\, useposix=true\, d_sigaction=define usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef useperlio=define d_sfio=undef uselargefiles=define usesocks=undef use64bitint=define use64bitall=define uselongdouble=undef usemymalloc=n\, bincompat5005=undef Compiler: cc='x86_64-pc-linux-gnu-gcc'\, ccflags ='-fno-strict-aliasing -pipe -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64'\, optimize='-O2 -pipe'\, cppflags='-fno-strict-aliasing -pipe' ccversion=''\, gccversion='3.4.4 (Gentoo 3.4.4-r1\, ssp-3.4.4-1.0\, pie-8.7.8)'\, gccosandvers='' intsize=4\, longsize=8\, ptrsize=8\, doublesize=8\, byteorder=12345678 d_longlong=define\, longlongsize=8\, d_longdbl=define\, longdblsize=16 ivtype='long'\, ivsize=8\, nvtype='double'\, nvsize=8\, Off_t='off_t'\, lseeksize=8 alignbytes=8\, prototype=define Linker and Libraries: ld='x86_64-pc-linux-gnu-gcc'\, ldflags ='' libpth=/usr/local/lib64 /lib64 /usr/lib64 libs=-lpthread -lnsl -ldb -ldl -lm -lcrypt -lutil -lc perllibs=-lpthread -lnsl -ldl -lm -lcrypt -lutil -lc libc=/lib/libc-2.3.5.so\, so=so\, useshrplib=false\, libperl=libperl.a gnulibc_version='2.3.5' Dynamic Linking: dlsrc=dl_dlopen.xs\, dlext=so\, d_dlsymun=undef\, ccdlflags='-rdynamic' cccdlflags='-fPIC'\, lddlflags='-shared'
Locally applied patches:
@INC for perl v5.8.7: /etc/perl /usr/lib/perl5/site_perl/5.8.7/x86_64-linux /usr/lib/perl5/site_perl/5.8.7 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.7/x86_64-linux /usr/lib/perl5/vendor_perl/5.8.7 /usr/lib/perl5/vendor_perl /usr/lib/perl5/5.8.7/x86_64-linux /usr/lib/perl5/5.8.7 /usr/local/lib/site_perl .
Environment for perl v5.8.7: HOME=/home/ivashin LANG=en_US.UTF-8 LANGUAGE (unset) LD_LIBRARY_PATH=/home/ivashin/localusr/lib:/home/ivashin/src/root/lib:/usr/X11R6/lib64:/usr/X11R6/lib LOGDIR (unset) PATH=/home/ivashin/localusr/bin:/home/ivashin/src/root/bin:/cern/2005/bin:/usr/local/bin:/usr/bin:/bin:/opt/bin:/usr/x86_64-pc-linux-gnu/gcc-bin/3.4.4:/opt/blackdown-jre-1.4.2.03/bin:/usr/kde/3.5/bin:/usr/qt/3/bin:/usr/games/bin:/sbin:/usr/sbin PERL_BADLANG (unset) SHELL=/bin/bash
On Tue\, Sep 19\, 2006 at 07:57:03AM -0700\, Anton Ivashin wrote:
When I try to execute this program: #!/usr/bin/perl $i=-20; $j=-10; $k=-5; for(;$i\<=20;$i++){ for(;$j\<=10;$j++){ for(;$k\<=5;$k++){ printf("%d %d %d\n"\,$i\,$j\,$k); } } }
that is only internal loop is executed by perl. Why? When I initialize variables in for statement directly (i.e. for($foo=-20;$foo\<=20;$foo++) {} instead of $foo=-20;for(;$foo\<=20;$foo++){}) the problem disappears.
Thank you for your report. However\, this is a bug in your code\, not in Perl.
You answered your own question. Your code initializes the variables outside the loops. After the first pass through the inner loop\, $k is equal to 6\, and it remains so for the rest of the execution. If you want $k to start at -5 each time\, you need to actually set it -5 each time.
This should show you what is happening:
#!/usr/bin/perl $i=-20; $j=-10; $k=-5; for(;$i\<=20;$i++){ printf("i loop: %d %d %d\n"\,$i\,$j\,$k); for(;$j\<=10;$j++){ printf("j loop: %d %d %d\n"\,$i\,$j\,$k); for(;$k\<=5;$k++){ printf("k loop: %d %d %d\n"\,$i\,$j\,$k); } } }
Ronald
The RT System itself - Status changed from 'new' to 'open'
@smpeters - Status changed from 'open' to 'resolved'
-----Original Message----- From: Anton Ivashin (via RT) [mailto:perlbug-followup@perl.org] Sent: 19 September 2006 15:57 To: bugs-bitbucket@rt.perl.org Subject: [perl #40365] Nested for loops: only internal loop is executed
# New Ticket Created by Anton Ivashin # Please include the string: [perl #40365] # in the subject line of all future correspondence about this issue. # \<URL: http://rt.perl.org/rt3/Ticket/Display.html?id=40365 >
Hi! When I try to execute this program: #!/usr/bin/perl $i=-20; $j=-10; $k=-5; for(;$i\<=20;$i++){ for(;$j\<=10;$j++){ for(;$k\<=5;$k++){ printf("%d %d %d\n"\,$i\,$j\,$k); } } }
When the inner loop completes $k>5 so every time the inner loop is reached it terminates immediately ($k is not reset) so no further printing occurs
Try this to see what is happening in your code:
use strict; my $i=-20; my $j=-10; my $k=-5; for(;$i\<=20;$i++){ printf("Outer:\t%4d\n"\,$i); for(;$j\<=10;$j++){ printf("Middle:\t%4d%4d\n"\,$i\,$j); for(;$k\<=5;$k++){ printf("Inner:\t%4d%4d%4d\n"\,$i\,$j\,$k); } } }
Try this to see what you wanted to see:
use strict; my $i=-20; for(;$i\<=20;$i++){ my $j=-10; for(;$j\<=10;$j++){ my $k=-5; for(;$k\<=5;$k++){ printf("%4d%4d%4d\n"\,$i\,$j\,$k); } } }
Robin
This e-mail and any attachments may contain confidential and/or privileged material; it is for the intended addressee(s) only. If you are not a named addressee\, you must not use\, retain or disclose such information.
NPL Management Ltd cannot guarantee that the e-mail or any attachments are free from viruses.
NPL Management Ltd. Registered in England and Wales. No: 2937881 Registered Office: Serco House\, 16 Bartley Wood Business Park\, Hook\, Hampshire\, United Kingdom RG27 9UY
Migrated from rt.perl.org#40365 (status was 'resolved')
Searchable as RT40365$