Dude you rock! For the most part I modified your example so that it almost works as expected. I had to blow away the UTF-8 encoding part due to errors. Then I modified the '$i (1..$#unique_accounts)' so output would not be zero based. Then I included some added conditions so that it would not process header & trailer records. So output is:
1:#########
2:#########
3:#########
4:#########
5:#########
total: 370659
uniq : 6
Sorry but I cannot show actual values. Notice there are 5 unique values (which is correct) yet the 'uniq' counter reads 6. Also total lines processed equals 370659, it should only equal 370657. It is obviously processing header & trailer records. Here is your example with my changes:
use strict; use warnings; use List::MoreUtils qw(uniq); my $char_at=10; my $num_chars=50; open my $fh, $ARGV[0] #open my $fh, '<:encoding(UTF-8)', $ARGV[0] or die "Could not open '$ARGV[0]' $!"; my @trnAccounts; 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); } } my @unique_accounts = uniq(@trnAccounts); for my $i (1..$#unique_accounts) { print $i, ":", $unique_accounts[$i], "\n"; } print "total: ", scalar(@trnAccounts), "\n"; print "uniq : ", scalar(@unique_accounts), "\n";
In reply to Re^2: Trying to print out only unique array values-
by rickman1
in thread Trying to print out only unique array values-
by rickman1
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |