Hi :)

Hmm, I've never done that :) when I see those statements i think: I'd write while array not empty ... modify array so

A quick scan of my files shows always scalar @array
while( @letters > 2 ){ while( @dirs ){ while( @files ){ while( @ARGV ){ while( @nodes ){ ...

I thought, hey, maybe in list context..

$ perl -MData::Dump -e " my @f = ( 1, 0, undef, 6 ); while( my($q)=sh +ift @f ){ dd( $q ); $r++; die if $r > 10; } " 1 0 undef 6 undef undef undef undef undef undef undef Died at -e line 1.

But as you can see infinite loop is infinite :) same as $ perl -e " my @f = ( 1, 0, undef, 6 ); while( my@q=shift @f ){ dd( @q ); $r++; die if $r > 10; } "

because shift returns undef to signal no more elements, even in scalar context

I remember a recent discussion about that shift in list context buggy?

So, very nice thread/question gvandeweyer, makes made me examine why I write code the way I write ... its amazing how much stuff just slips into your mind while you're unaware ... I believe I learned to write while(@array) from good code examples ... after its the way I think :)

Hmm, I'm getting another idea, if you didn't notice your array values can be undef while testing/developing, a supplemental warning could be useful; should be easy to write since Perl::Critic::Policy is xpaths , and naming is always the hard part
Perl::Critic::Policy::BuiltinFunctions::ProhibitWhileShift - don't write while($foo=shift@bar) instead write while(@bar)
Perl::Critic::Policy::ControlStructures::ProhibitWhileShiftArray - don't write while($foo=shift@bar) instead write while(@bar)
Perl::Critic::Policy::Variables::ProhibitWhileLoopShiftArray - don't write while($foo=shift@bar) instead write while(@bar)
Perl::Critic::Policy::ControlStructures::LoopTestConditionArray - don't write while($foo=shift@bar) instead write while(@bar)

Thanks again gvandeweyer


In reply to Re: Different result for 'foreach' vs 'while shift' arrayref (while(@array)) by Anonymous Monk
in thread Different result for 'foreach' vs 'while shift' arrayref by gvandeweyer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.