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:
- $/ is set to something other than newline.
- Your test to see if newline is being chomped could be flawed, and if that's the case, we might see the flaw in the omitted code that comprises the remainder of your loop.
- You may have more than one trailing newline, with $/ set to "\n" instead of "".
- There may be some other whitespace or non-printing character between two newline characters. chomp, in paragraph mode, will remove all trailing newlines. But if you have a trailing "\n\t\n", only the last "\n is removed, since the first one isn't "trailing" (there's a tab after it.
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.
|