in reply to Re^2: Trying to print out only unique array values-
in thread Trying to print out only unique array values-
This code has some issues that I see:
First, my $output = $line; will never be executed. And even if it is, it will do absolutely nothing. You cannot declare a "my" variable conditionally and use it elsewhere. Use it immediately or not at all. So this whole "if" clause is "nonsense".while (my $line = <$fh>) { my $val = substr($line, $char_at, $num_chars); if ($val eq 'H' or $val eq 'T') { my $output = $line; }else { chomp $line; next unless $line && length($line) >= $char_at; push @trnAccounts, substr($line, $char_at, $num_chars); } }
Will the condition if ($val eq 'H' or $val eq 'T') ever be satisfied? I think not. $val looks like it is a string of 50 characters, starting at $char_at. This will never equal a single character comparison. Some regex might work, but single character, I think not. You have not chomped the line endings, and this line ending in $val will prevent the "match".
In the "else" clause, the appears to be confusion. next unless $line, will always work! Right up front, while (my $line = <$fh>) says that $line is true otherwise the loop doesn't proceed. push @trnAccounts, substr($line, $char_at, $num_chars);. Well that substr is just $val.
I suggest that you have another "go" at this. Generate say 10 example lines, show your code to process those lines and how it fails. Keep simplifying the example until you cannot reproduce the problem any more. Make it as simple as possible. This process may help you discover your own problem.
|
|---|