In the code below, the sub article_title1 shows the error I made and sub article_title2 shows the hack that fixed it. Given this, I've two questions:

  1. Exactly what is going wrong in the first sub?
  2. Is there an idiomatic version of the second sub?
It looks as though the internal pointer to the array element is getting trashed by the dual action of for and shift, but I'm not clear on the details.

#!/perl/bin/perl # use strict; use warnings; use diagnostics; my @text_master = <DATA>; chomp @text_master; my @text = @text_master; article_title1(); print "\n"; @text = @text_master; article_title2(); sub article_title1 { local $_ = shift(@text); # left in for side effect if (@text) { for (@text) { print "# TEXT-1 = '$_'\n"; last if (/^$/); shift(@text); } } print "# TEXT-2 = '$_'\n" for @text; } sub article_title2 { local $_ = shift(@text); # left in for side effect if (@text) { my @text_copy = @text; for (@text) { print "# TEXT-1 = '$_'\n"; last if (/^$/); shift(@text_copy); } @text = @text_copy; } print "# TEXT-2 = '$_'\n" for @text; } __DATA__ ==Play Chess: Click the join icon to play Matches_Sought.gif Matches Sought Graph You can also start a game by clicking a dot in the Matches Sought grap +h. Rest the mouse pointer over a dot. Games on ICC are often described by their time limits.

Which results in:

# TEXT-1 = 'Click the join icon to play'
# TEXT-1 = 'Matches_Sought.gif Matches Sought Graph'
# TEXT-1 = 'You can also start by clicking a dot in the graph.'
# TEXT-1 = 'Rest the mouse pointer over a dot.'
# TEXT-1 = 'Games on ICC are often described by their time limits.'
# TEXT-2 = ''
# TEXT-2 = 'Rest the mouse pointer over a dot.'
# TEXT-2 = ''
# TEXT-2 = 'Games on ICC are often described by their time limits.'

# TEXT-1 = 'Click the join icon to play'
# TEXT-1 = ''
# TEXT-2 = ''
# TEXT-2 = 'Matches_Sought.gif Matches Sought Graph'
# TEXT-2 = ''
# TEXT-2 = 'You can also start by clicking a dot in the graph.'
# TEXT-2 = ''
# TEXT-2 = 'Rest the mouse pointer over a dot.'
# TEXT-2 = ''
# TEXT-2 = 'Games on ICC are often described by their time limits.'

--hsm

"Never try to teach a pig to sing...it wastes your time and it annoys the pig."

In reply to for mistake with shift by hsmyers

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.