Well, there are some possible points of confusion. Your snippet of code doesn't show what you do with @array after the while loop; that suggest me that you could be reading the file, modifying each line and then throwing away the modified line. Anyway, I'll try to work on the snippet.

On the first iteration, @array contains nothing (since you commented the assignment @array = () ;), so the subsequent foreach won't run, and if $line contains a leading whitespace, it won't catch it.

Your code could be patched by using the peculiarities of split when working on $_, like this

while (<FH>) { chomp ; @array = split ; # do something with @array }
But if preserving the value of $_ it is vital for you, you could change it slightly this way:
while (<FH>) { $line = $_ ; chomp $line ; $line =~ s/^\s// ; # delete leading whitespace, if any @array = split /\s+/,$line ; # do something with @array; $_ is preserved so you # can use it again }

That's all I could do with the information you sent ;-). If these solutions don't fit, just ask specifying better.

--bronto

PS: ...and if you need more information on the behaviour of split with no arguments, there is no better source than perldoc -f split;-)


In reply to Re: problems with arrays by bronto
in thread problems with arrays by Anonymous Monk

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.