curious behavior with shift (perl 5.8)?

Discussion in 'Perl Misc' started by s_p_a_m_mob@hotmail.com, May 12, 2005.

  1. Guest

    Hi there,


    []$ perl
    $a = [1,2,3,4,5,6];
    foreach(@{$a}){
    my $b = unshift @{$a};
    my $c = shift @{$a};
    print $b,$c;
    }
    615243

    That is ok but:

    []$ perl
    $a = [1,2,3,4,5,6];
    foreach(@{$a}){
    my $b = shift @{$a};
    my $c = shift @{$a};
    print $b,$c;
    }
    1234


    Why are 5 and 6 missing?

    Marc-Olivier Bernard

    ########################
    perl -V says:

    Summary of my perl5 (revision 5.0 version 8 subversion 0)
    configuration:
    Platform:
    osname=linux, osvers=2.4.21-1.1931.2.382.entsmp,
    archname=i386-linux-thread-multi
    uname='linux stripples.devel.redhat.com 2.4.21-1.1931.2.382.entsmp
    #1 smp wed aug 6 17:18:52 edt 2003 i686 i686 i386 gnulinux '
    config_args='-des -Doptimize=-O2 -g -pipe -march=i386 -mcpu=i686
    -Dmyhostname=localhost -Dperladmin=root@localhost -Dcc=gcc -Dcf_by=Red
    Hat, Inc. -Dinstallprefix=/usr -Dprefix=/usr -Darchname=i386-linux
    -Dvendorprefix=/usr -Dsiteprefix=/usr
    -Dotherlibdirs=/usr/lib/perl5/5.8.0 -Duseshrplib -Dusethreads
    -Duseithreads -Duselargefiles -Dd_dosuid -Dd_semctl_semun -Di_db
    -Ui_ndbm -Di_gdbm -Di_shadow -Di_syslog -Dman3ext=3pm -Duseperlio
    -Dinstallusrbinperl -Ubincompat5005 -Uversiononly -Dpager=/usr/bin/less
    -isr'
    hint=recommended, useposix=true, d_sigaction=define
    usethreads=define use5005threads=undef useithreads=define
    usemultiplicity=define
    useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
    use64bitint=undef use64bitall=undef uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
    Compiler:
    cc='gcc', ccflags ='-D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS
    -DDEBUGGING -fno-strict-aliasing -I/usr/local/include
    -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/include/gdbm',
    optimize='-O2 -g -pipe -march=i386 -mcpu=i686',
    cppflags='-D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS
    -DDEBUGGING -fno-strict-aliasing -I/usr/local/include
    -I/usr/include/gdbm'
    ccversion='', gccversion='3.2.2 20030222 (Red Hat Linux 3.2.2-5)',
    gccosandvers=''
    intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t',
    lseeksize=8
    alignbytes=4, prototype=define
    Linker and Libraries:
    ld='gcc', ldflags =' -L/usr/local/lib'
    libpth=/usr/local/lib /lib /usr/lib
    libs=-lnsl -lgdbm -ldb -ldl -lm -lpthread -lc -lcrypt -lutil
    perllibs=-lnsl -ldl -lm -lpthread -lc -lcrypt -lutil
    libc=/lib/libc-2.3.2.so, so=so, useshrplib=true, libperl=libperl.so
    gnulibc_version='2.3.2'
    Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-rdynamic
    -Wl,-rpath,/usr/lib/perl5/5.8.0/i386-linux-thread-multi/CORE'
    cccdlflags='-fPIC', lddlflags='-shared -L/usr/local/lib'




    Characteristics of this binary (from libperl):
    Compile-time options: DEBUGGING MULTIPLICITY USE_ITHREADS
    USE_LARGE_FILES PERL_IMPLICIT_CONTEXT
    Locally applied patches:
    MAINT18379
    Built under linux
    Compiled at Aug 13 2003 11:47:58
    @INC:
    /usr/lib/perl5/5.8.0/i386-linux-thread-multi
    /usr/lib/perl5/5.8.0
    /usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi
    /usr/lib/perl5/site_perl/5.8.0
    /usr/lib/perl5/site_perl
    /usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi
    /usr/lib/perl5/vendor_perl/5.8.0
    /usr/lib/perl5/vendor_perl
    /usr/lib/perl5/5.8.0/i386-linux-thread-multi
    /usr/lib/perl5/5.8.0
     
    , May 12, 2005
    #1
    1. Advertising

  2. wrote:
    >
    > []$ perl
    > $a = [1,2,3,4,5,6];
    > foreach(@{$a}){
    > my $b = unshift @{$a};
    > my $c = shift @{$a};
    > print $b,$c;
    > }
    > 615243
    >
    > That is ok but:
    >
    > []$ perl
    > $a = [1,2,3,4,5,6];
    > foreach(@{$a}){
    > my $b = shift @{$a};
    > my $c = shift @{$a};
    > print $b,$c;
    > }
    > 1234
    >
    >
    > Why are 5 and 6 missing?


    perldoc perlsyn

    [snip]

    Foreach Loops

    The "foreach" loop iterates over a normal list value and sets the
    variable VAR to be each element of the list in turn. If the variable
    is preceded with the keyword "my", then it is lexically scoped, and is
    therefore visible only within the loop. Otherwise, the variable is
    implicitly local to the loop and regains its former value upon exiting
    the loop. If the variable was previously declared with "my", it uses
    that variable instead of the global one, but it's still localized to
    the loop. This implicit localisation occurs only in a "foreach" loop.

    The "foreach" keyword is actually a synonym for the "for" keyword, so
    you can use "foreach" for readability or "for" for brevity. (Or
    because the Bourne shell is more familiar to you than csh, so writing
    "for" comes more naturally.) If VAR is omitted, $_ is set to each
    value.

    If any element of LIST is an lvalue, you can modify it by modifying VAR
    inside the loop. Conversely, if any element of LIST is NOT an lvalue,
    any attempt to modify that element will fail. In other words, the
    "foreach" loop index variable is an implicit alias for each item in the
    list that you're looping over.

    If any part of LIST is an array, "foreach" will get very confused if
    you add or remove elements within the loop body, for example with
    "splice". So don't do that.
    ^^^^^^^^^^^^^^^^^^
    ^^^^^^^^^^^^^^^^^^
    ^^^^^^^^^^^^^^^^^^


    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, May 12, 2005
    #2
    1. Advertising

  3. Guest

    I missed that. Thanks to you John!

    Marc-Olivier BERNARD
     
    , May 13, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roberto Gallo

    Shift - byte[] buf shift

    Roberto Gallo, Jan 27, 2004, in forum: Java
    Replies:
    3
    Views:
    2,071
    Thomas Schodt
    Jan 27, 2004
  2. Wenjie
    Replies:
    3
    Views:
    1,039
    Ron Samuel Klatchko
    Jul 11, 2003
  3. Santosh Nayak

    Left Shift / Right Shift Operators

    Santosh Nayak, Nov 30, 2006, in forum: C Programming
    Replies:
    16
    Views:
    1,456
    CBFalconer
    Nov 30, 2006
  4. Sanny
    Replies:
    38
    Views:
    3,421
    Thomas Richter
    Apr 29, 2011
  5. devphylosoff

    what "shift" does, if not "$_ = shift;" ?

    devphylosoff, Nov 29, 2007, in forum: Perl Misc
    Replies:
    3
    Views:
    334
    Michele Dondi
    Dec 4, 2007
Loading...

Share This Page