Shiping Wang wrote:
At 02:04 PM 10/18/2006, Johnson, Reginald (GTI) wrote:
I am trying to understand this sort and uniq code that a came across in
the archive. This works, but I thought the %uniq would have the sort
and uniqed values. What is needed if I didn't want to print the values
out immediatedly but put them in an array or hash?
#!/usr/bin/perl
use strict;
use warnings;
my @holdArr =qw(apple pear lemon bananna apple pear);
my %uniq;
map{$uniq{$_}=1 and print "$_\n" unless $uniq{$_}
[EMAIL PROTECTED];
--------------------------------------------------------
How about:
[EMAIL PROTECTED];
Never use map when you mean foreach:
$uniq{$_} = 1 foreach @holdArr;
or if you want to use map:
%uniq = map { $_ => 1 } @holdArr;
for array:
my @uniqArr = keys%uniq;
print join "\t", @uniqArr, "\n";
That will output a tab character after the last array element. Try either
print join("\t", @uniqArr), "\n";
or
{
local $" = "\t";
print "@uniqArr\n";
}
HTH,
Rob
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>