develooper Front page | perl.perl5.porters | Postings from June 2011

[perl #93234] Re: each() skips over elements when current value removed from array

From:
Pedro Melo
Date:
June 21, 2011 13:45
Subject:
[perl #93234] Re: each() skips over elements when current value removed from array
Message ID:
rt-3.6.HEAD-29417-1308657461-1837.93234-75-0@perl.org
# New Ticket Created by  Pedro Melo 
# Please include the string:  [perl #93234]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=93234 >


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/s
 bin
>  :/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




nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About