Loading ...
Sorry, an error occurred while loading the content.

AW: [PBML] removing elements of an arrays on the fly

Expand Messages
  • Lange Gunter, CH
    Thank you, Jeff! You are right! I wanted to do not only a dangerous but also a stupid practice. Before programming it seems to be good practice to switch on
    Message 1 of 3 , Oct 2, 2003
    • 0 Attachment
      Thank you, Jeff!

      You are right! I wanted to do not only a dangerous but also a stupid
      practice. Before programming it seems to be good practice to switch on brain
      ;-)



      > -----Urspr√ľngliche Nachricht-----
      > Von: Jeff 'japhy' Pinyan [SMTP:japhy@...]
      > Gesendet am: Donnerstag, 2. Oktober 2003 16:19
      > An: 'perl-beginner@yahoogroups.com'
      > Betreff: Re: [PBML] removing elements of an arrays on the fly
      >
      > On Oct 2, Lange Gunter, CH said:
      >
      > >is there a way to remove elements from an array as I'm iterating through
      > an
      > >array with foreach?
      >
      > That's a dangerous practice. You'll end up messing things up, because
      > when you have an array like (a,b,c,d), if you're looking at element 'b'
      > and you say "delete me", then the array is (a,c,d). But the next element
      > you get to will be 'd', because Perl internally just goes to the next
      > index in the array, and 'd' is the new $array[2].
      >
      > Use a C-style for loop:
      >
      > for ($i = 0; $i < @arr; $i++) {
      > if (CONDITION) {
      > splice @arr, $i, 1;
      > $i--; # to counteract the $i++ in the for statement
      > }
      > }
      >
      > --
      > Jeff "japhy" Pinyan japhy@... http://www.pobox.com/~japhy/
      > RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/
      > <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
      > [ I'm looking for programming work. If you like my work, let me know. ]
      >
      >
      >
      > Unsubscribing info is here:
      > http://help.yahoo.com/help/us/groups/groups-32.html
      >
      > Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
      >
    • Lange Gunter, CH
      Please, let me ask another question! To get the element count of an array there you will use $#arr! But how to do it with a reference to an array? It is very
      Message 2 of 3 , Oct 2, 2003
      • 0 Attachment
        Please, let me ask another question!

        To get the element count of an array there you will use $#arr! But how to do
        it with a reference to an array? It is very difficult for me to track it
        down ($#refArr does not work, nor $#$refArr or $#@$refArr)

        Best regards
        G





        > -----Urspr√ľngliche Nachricht-----
        > Von: merlyn@... [SMTP:merlyn@...]
        > Gesendet am: Donnerstag, 2. Oktober 2003 16:54
        > An: perl-beginner@yahoogroups.com
        > Betreff: Re: [PBML] removing elements of an arrays on the fly
        >
        > >>>>> "Jeff" == Jeff 'japhy' Pinyan <japhy@...> writes:
        >
        > Jeff> On Oct 2, Lange Gunter, CH said:
        > >> is there a way to remove elements from an array as I'm iterating
        > through an
        > >> array with foreach?
        >
        > Jeff> That's a dangerous practice. You'll end up messing things up,
        > because
        > Jeff> when you have an array like (a,b,c,d), if you're looking at element
        > 'b'
        > Jeff> and you say "delete me", then the array is (a,c,d). But the next
        > element
        > Jeff> you get to will be 'd', because Perl internally just goes to the
        > next
        > Jeff> index in the array, and 'd' is the new $array[2].
        >
        > Jeff> Use a C-style for loop:
        >
        > Jeff> for ($i = 0; $i < @arr; $i++) {
        > Jeff> if (CONDITION) {
        > Jeff> splice @arr, $i, 1;
        > Jeff> $i--; # to counteract the $i++ in the for statement
        > Jeff> }
        > Jeff> }
        >
        > Jeff! It's easier just to count down:
        >
        > for (my $i = $#arr; $i >= 0; $i--) {
        > if (CONDITION on $arr[$i]) {
        > splice @arr, $i, 1;
        > }
        > }
        >
        > --
        > Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777
        > 0095
        > <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
        > Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
        > See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl
        > training!
        >
        >
        > Unsubscribing info is here:
        > http://help.yahoo.com/help/us/groups/groups-32.html
        >
        > Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
        >
      • Charles K. Clarkson
        ... $#$refArr does work: my $foo = [ 1 .. 3 ]; print $#$foo; HTH, Charles K. Clarkson -- Head Bottle Washer, Clarkson Energy Homes, Inc. Mobile Home
        Message 3 of 3 , Oct 2, 2003
        • 0 Attachment
          : -----Original Message-----
          : From: Lange Gunter, CH [mailto:gunter.lange@...]
          : Sent: Thursday, October 02, 2003 10:09 AM
          : To: 'perl-beginner@yahoogroups.com'
          : Subject: AW: [PBML] removing elements of an arrays on the fly
          :
          :
          : Please, let me ask another question!
          :
          : To get the element count of an array there you will
          : use $#arr! But how to do it with a reference to an
          : array? It is very difficult for me to track it down
          : ($#refArr does not work, nor $#$refArr or $#@$refArr)

          $#$refArr does work:

          my $foo = [ 1 .. 3 ];

          print $#$foo;

          HTH,

          Charles K. Clarkson
          --
          Head Bottle Washer,
          Clarkson Energy Homes, Inc.
          Mobile Home Specialists
          254 968-8328
        Your message has been successfully submitted and would be delivered to recipients shortly.