in reply to Trying to print out only unique array values-
Some of the ways you were using trim() and putting elements into an array were off. Once cleared up, the code above should hopefully be a little easier to follow. Also, later versions of List::Util contain a uniq function. Use that instead. Hope that helps.#!/usr/bin/env perl use strict; use warnings; use List::Util 1.45 qw(uniq); my $char_at=10; my $num_chars=50; open my $fh, '<:encoding(UTF-8)', $ARGV[0] or die "Could not open '$ARGV[0]' $!"; my @trnAccounts; while (my $line = <$fh>) { 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 (0..$#unique_accounts) { print $i, ":", $unique_accounts[$i], "\n"; } print "total: ", scalar(@trnAccounts), "\n"; print "uniq : ", scalar(@unique_accounts), "\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Trying to print out only unique array values-
by rickman1 (Novice) on Aug 16, 2016 at 19:23 UTC | |
by Marshall (Canon) on Aug 16, 2016 at 22:22 UTC | |
by genio (Beadle) on Aug 16, 2016 at 22:34 UTC | |
by rickman1 (Novice) on Aug 17, 2016 at 16:12 UTC |