Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested Iterations on a Hash Work Correctly #441

Closed
p5pRT opened this issue Aug 26, 1999 · 2 comments
Closed

Nested Iterations on a Hash Work Correctly #441

p5pRT opened this issue Aug 26, 1999 · 2 comments

Comments

@p5pRT
Copy link

p5pRT commented Aug 26, 1999

Migrated from rt.perl.org#1282 (status was 'resolved')

Searchable as RT1282$

@p5pRT
Copy link
Author

p5pRT commented Aug 26, 1999

From mike@tecc.co.uk

#!/usr/bin/perl -w

# I'm using perl version 5.004_04 built for sun4-solaris
# The manual (man perlfunc) says​:
#
# There is a single iterator for each hash, shared by all
# each(), keys(), and values() function calls in the program
#
# But the following program appears to prove this false, by working
# correctly in producing all nine possible (key,key) pairs. (Not that
# this is exactly a problem, but it is a surprise.)

use strict;

my %hash = (foo => "fish",
  bar => "bat",
  baz => "bathtub");

foreach my $i (keys %hash) {
  foreach my $j (keys %hash) {
  print("$i,$j\n");
  }
}


Site configuration information for perl 5.00404​:

Configured by stuart at Wed Jan 14 11​:34​:01 GMT 1998.

Summary of my perl5 (5.0 patchlevel 4 subversion 4) configuration​:
  Platform​:
  osname=solaris, osvers=2.6, archname=sun4-solaris
  uname='sunos handbag.tecc.co.uk 5.6 generic sun4u sparc sunw,ultra-2 '
  hint=recommended, useposix=true, d_sigaction=define
  bincompat3=y useperlio=undef d_sfio=undef
  Compiler​:
  cc='gcc -B/usr/ccs/bin/', optimize='-O', gccversion=2.7.2.3
  cppflags='-I/usr/local/include'
  ccflags ='-I/usr/local/include'
  stdchar='unsigned char', d_stdstdio=define, usevfork=false
  voidflags=15, castflags=0, d_casti32=define, d_castneg=define
  intsize=4, alignbytes=8, usemymalloc=y, prototype=define
  Linker and Libraries​:
  ld='gcc -B/usr/ccs/bin/', ldflags =' -L/usr/local/lib'
  libpth=/usr/local/lib /lib /usr/lib /usr/ccs/lib
  libs=-lsocket -lnsl -ldl -lm -lc -lcrypt
  libc=/lib/libc.so, so=so
  useshrplib=false, libperl=libperl.a
  Dynamic Linking​:
  dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags=' '
  cccdlflags='-fpic', lddlflags='-G -L/usr/local/lib'

Locally applied patches​:
 


@​INC for perl 5.00404​:
  /usr/local/lib/perl5/sun4-solaris/5.00404
  /usr/local/lib/perl5
  /usr/local/lib/perl5/site_perl/sun4-solaris
  /usr/local/lib/perl5/site_perl
  .


Environment for perl 5.00404​:
  HOME=/export/home/staff/mike
  LANG (unset)
  LD_LIBRARY_PATH (unset)
  LOGDIR (unset)
  PATH=/export/home/staff/mike/bin​:/usr/local/bin​:/usr/bin​:/usr/X/bin​:/usr/ccs/bin​:.
  PERL_BADLANG (unset)
  SHELL=/usr/local/bin/bash

@p5pRT
Copy link
Author

p5pRT commented Aug 26, 1999

From [Unknown Contact. See original ticket]

Mike Taylor <mike@​tecc.co.uk> writes​:

This is a bug report for perl from mike@​tecc.co.uk,
generated with the help of perlbug 1.20 running under perl 5.00404.

#!/usr/bin/perl -w

# I'm using perl version 5.004_04 built for sun4-solaris
# The manual (man perlfunc) says​:
#
# There is a single iterator for each hash, shared by all
# each(), keys(), and values() function calls in the program
#
# But the following program appears to prove this false, by working
# correctly in producing all nine possible (key,key) pairs. (Not that
# this is exactly a problem, but it is a surprise.)

Yes, but in _current_ perls when you use keys %hash in a foreach
the itterator is use to build a complete list on the stack before
the body is called. We have started optimizing this behaviour (
i.e. avoiding building the full list) but have only done n..m style
so far.

If you recast your example :

use strict;

my %hash = (foo => "fish",
  bar => "bat",
  baz => "bathtub");

while (defined(my $i = each %hash)) {
  while (defined(my $j = each %hash)) {
  print("$i,$j\n");
  }
}

You will see the documented behaviour. foreach _may_ behave
like that someday - but there is a lot of code that uses
foreach (keys %hash) so we may never dare break it ...

--
Nick Ing-Simmons

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant