Perl / perl5

🐪 The Perl programming language
https://dev.perl.org/perl5/
Other
1.96k stars 555 forks source link

Re: each() skips over elements when current value removed from array #11454

Closed p5pRT closed 12 years ago

p5pRT commented 13 years ago

Migrated from rt.perl.org#93234 (status was 'rejected')

Searchable as RT93234$

p5pRT commented 13 years ago

From @melo

Ignore this\, I just read the manpage again\, it clearly states that this only applies to hashes :/

Bye\,

On Tue\, Jun 21\, 2011 at 12​:55 PM\, \melo@​simplicidade\.org wrote​:

This is a bug report for perl from melo@​simplicidade.org\, generated with the help of perlbug 1.39 running under perl 5.14.1.

----------------------------------------------------------------- The each manpage has this to say about deleting elements while iterating​:

   If you add or delete    a hash’s elements while iterating over it\, entries may be    skipped or duplicated‐‐so don’t do that.  Exception​: It is    always safe to delete the item most recently returned by    "each()"

The exception only applies to hashes\, not arrays. With arrays\, elements are skiped when I remove the current one​:

   my @​n = (1..10);    while (my ($i\, $v) = each @​n) {      print " $i => $v\n";         ## print all pairs      splice(@​n\, $i\, 1) if $v % 2; ## remove odd numbers    }

Expected output​:

   0 => 1    1 => 2    2 => 3    3 => 4    4 => 5    5 => 6    6 => 7    7 => 8    8 => 9    9 => 10

Actual output​:

   0 => 1    1 => 3    2 => 5    3 => 7    4 => 9

The contents of @​n after the while loop are correct​: only even numbers.

So either each() has a bug while deleting the current element while traversing a array\, or the documentation should be clearer stating that this exception only applies to hashes.

Personaly\, it would be great if it was bug :)

Thanks\,

[Please do not change anything below this line] ----------------------------------------------------------------- --- Flags​:    category=core    severity=low --- Site configuration information for perl 5.14.1​:

Configured by melo at Fri Jun 17 15​:54​:30 WEST 2011.

Summary of my perl5 (revision 5 version 14 subversion 1) configuration​:

 Platform​:    osname=darwin\, osvers=9.8.0\, archname=darwin-2level    uname='darwin dogshouse.local 9.8.0 darwin kernel version 9.8.0​: wed jul 15 16​:55​:01 pdt 2009; root​:xnu-1228.15.4~1release_i386 i386 '    config_args='-de -Dprefix=/Users/melo/perl5/perlbrew/perls/perl-5.14.1'    hint=recommended\, useposix=true\, d_sigaction=define    useithreads=undef\, usemultiplicity=undef    useperlio=define\, d_sfio=undef\, uselargefiles=define\, usesocks=undef    use64bitint=undef\, use64bitall=undef\, uselongdouble=undef    usemymalloc=n\, bincompat5005=undef  Compiler​:    cc='cc'\, ccflags ='-fno-common -DPERL_DARWIN -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include'\,    optimize='-O3'\,    cppflags='-fno-common -DPERL_DARWIN -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include'    ccversion=''\, gccversion='4.0.1 (Apple Inc. build 5488)'\, gccosandvers=''    intsize=4\, longsize=4\, ptrsize=4\, doublesize=8\, byteorder=1234    d_longlong=define\, longlongsize=8\, d_longdbl=define\, longdblsize=16    ivtype='long'\, ivsize=4\, nvtype='double'\, nvsize=8\, Off_t='off_t'\, lseeksize=8    alignbytes=8\, prototype=define  Linker and Libraries​:    ld='env MACOSX_DEPLOYMENT_TARGET=10.3 cc'\, ldflags =' -fstack-protector -L/usr/local/lib'    libpth=/usr/local/lib /usr/lib    libs=-ldbm -ldl -lm -lutil -lc    perllibs=-ldl -lm -lutil -lc    libc=/usr/lib/libc.dylib\, so=dylib\, useshrplib=false\, libperl=libperl.a    gnulibc_version=''  Dynamic Linking​:    dlsrc=dl_dlopen.xs\, dlext=bundle\, d_dlsymun=undef\, ccdlflags=' '    cccdlflags=' '\, lddlflags=' -bundle -undefined dynamic_lookup -L/usr/local/lib -fstack-protector'

Locally applied patches​:

--- @​INC for perl 5.14.1​:    /usr/local/git/lib/perl5/site_perl    /opt/subversion/lib/svn-perl    /Users/melo/perl5/perlbrew/perls/perl-5.14.1/lib/site_perl/5.14.1/darwin-2level    /Users/melo/perl5/perlbrew/perls/perl-5.14.1/lib/site_perl/5.14.1    /Users/melo/perl5/perlbrew/perls/perl-5.14.1/lib/5.14.1/darwin-2level    /Users/melo/perl5/perlbrew/perls/perl-5.14.1/lib/5.14.1    .

--- Environment for perl 5.14.1​:    DYLD_LIBRARY_PATH (unset)    HOME=/Users/melo    LANG (unset)    LANGUAGE (unset)    LC_CTYPE=UTF-8    LD_LIBRARY_PATH (unset)    LOGDIR (unset)    PATH=/Library/Frameworks/Python.framework/Versions/Current/bin​:/Users/melo/perl5/perlbrew/bin​:/Users/melo/perl5/perlbrew/perls/perl-5.14.1/bin​::/Users/melo/bin​:/Users/melo/.scripts.d/bin​:/Users/melo/bin/MultiMarkdown/bin​:/Users/melo/bin/apps/bin​:/Users/melo/work/admin/oss/bin​:/usr/local/gd/bin​:/usr/local/pv/bin​:/usr/local/gearman/bin​:/usr/local/gearman/sbin​:/usr/local/libxml2/bin​:/usr/local/redis/bin​:/usr/local/gitflow/bin​:/opt/subversion/bin​:/Users/melo/.gem/ruby/1.8/bin​:/usr/local/bin​:/usr/bin​:/usr/sbin​://bin​://sbin​:/opt/subversion//bin​:/Users/melo/work/evolui/e1/bin​:/Users/melo/work/evolui/e5/dev/bin​:/usr/local/gd/bin​:/usr/local/gearman/bin​:/usr/local/gearman/sbin​:/usr/local/gearmand/bin​:/usr/local/gearmand/sbin​:/usr/local/git/bin​:/usr/local/libevent/bin​:/usr/local/libidn/bin​:/usr/local/libmcrypt/bin​:/usr/local/libxml2/bin​:/usr/local/mysql/bin​:/usr/local/nginx/sbin​:/usr/local/OpenAMQ/bin​:/usr/local/pv/bin​:/usr/local/readline​:/usr/local/redis/bin​:/usr/bin​:/bin​:/usr/sbin  :/sbin​:/usr/local/bin​:/usr/texbin​:/usr/X11/bin​:/Users/melo/bin​:/Users/melo/.rvm/bin    PERL5LIB=/usr/local/git/lib/perl5/site_perl​:/opt/subversion/lib/svn-perl​:    PERLBREW_HOME=/Users/melo/.perlbrew    PERLBREW_PATH=/Users/melo/perl5/perlbrew/bin​:/Users/melo/perl5/perlbrew/perls/perl-5.14.1/bin    PERLBREW_PERL=perl-5.14.1    PERLBREW_ROOT=/Users/melo/perl5/perlbrew    PERLBREW_VERSION=0.24    PERL_AUTOINSTALL_PREFER_CPAN=1    PERL_BADLANG (unset)    PERL_CPANM_OPT=--skip-installed --mirror=http​://cpan.perl.pt/    SHELL=/bin/bash

-- Pedro Melo @​pedromelo http​://www.simplicidade.org/ http​://about.me/melo xmpp​:melo@​simplicidade.org mailto​:melo@​simplicidade.org

p5pRT commented 13 years ago

From am0c@perl.kr

I don't agree. If you see the first paragraph in perldoc for each function.

When called in list context\, returns a 2-element list consisting of the key and value for the next element of a hash\, or the index and value for the next element of an array\, so that you can iterate over it. When called in scalar context\, returns only the key (not the value) in a hash\, or the index in an array.

It says that it only returns the index in an array when it is performed in the scalar context.

p5pRT commented 13 years ago

The RT System itself - Status changed from 'new' to 'open'

p5pRT commented 12 years ago

@iabyn - Status changed from 'open' to 'rejected'