I wanted to go through your simple example line by line to try to shed light:

foreach my $line (@$array) {

Here $line is becoming an alias for each element in the array referenced by $array, one iteration and one element at a time. Any changes made to the contents of $line will propagate back to the corresponding element in @{$array}. So, you're on the right track.

chomp $line;

Assuming $line contains a trailing "newline" character ( \n ), and also assuming that the special variable, $/ (the input record separator) hasn't been tampered with, and is set to the default of \n, chomp will remove the trailing newline character. Here is one area where you may have a problem. If $/ has been changed from its default, you may be expecting to chomp a newline, when in reality you're chomping some other value. More on this later.

print "$line**";

Now in your output you're not printing a newline character. Since you've already chomped the input record separator out of $line, (again assuming $/=="\n") there won't be a newline printed between iterations. This can cause the output buffer to not immediately flush. This isn't necessarily related to your problem, but it could be leading to difficult to read output, unless there's something later on in the loop we don't know about that is outputting a newline.

my @array = split(/\t/, $line);

This is just a style point. We have a loop based on @{$array}, and a lexical within the loop named @array. With this sort of naming strategy you're not doing yourself any favors with respect to clarity. In particular, the inner lexical may be given a more descriptive name that avoids visual confusion with @{$array}.

...}

The omitted code paraphrased by "..." may contain something relevant here. The question may be, "How do you know chomp isn't working?" Whatever comes next in the loop might help us, or may not. But the point is that nothing contained within your example would explain why chomp isn't doing what you expect it to be doing.

My theory is that you may have one of the following issues:

I would start by wrapping that loop in the following code:

{ local $/ = ""; # Set paragraph mode foreach my $line ............. ....} }

In other words, localize the special variable $/, and set it to paragraph mode to see if that resolves your issue. It's an easy test, and while it may not be the problem, it only takes a few seconds to check so that it can be eliminated as an issue. In Paragraph Mode chomp will remove all trailing newlines, even if there's more than one.


Dave


In reply to Re: chomp - reference by davido
in thread chomp - reference by michaelp

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.