Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > I'd like to create an array of unique values

Reply
Thread Tools

I'd like to create an array of unique values

 
 
Mr P
Guest
Posts: n/a
 
      05-09-2007
Sounds just like keys %h don't it.

Starting with

$_ =
"cat
man
dog
mouse
man
man
";

I want to end up with

[cat man dog mouse]

My approach is:

my @s = split /\n/;
# unique-afy it!
my %s;
@s{@s} = @s;
@s = keys %s;

My PREFERENCE is to not have to involve both a hash and an array, and
also to not have to include a non-native lib module.

Is there a syntax I can use to just work with the hash and populate
the keys-only directly from the split? I don't really care what the
values are. I don't even care if they are undef.


Thank-You, Peristas,

MP

 
Reply With Quote
 
 
 
 
Greg Bacon
Guest
Posts: n/a
 
      05-09-2007
In article <(E-Mail Removed) .com>,
Mr P <(E-Mail Removed)> wrote:

: Is there a syntax I can use to just work with the hash and populate
: the keys-only directly from the split? I don't really care what the
: values are. I don't even care if they are undef.

You can condense your code a little:

my %seen;
my @s = grep !$seen{$_}++, split /\n/;

Hope this helps,
Greg
--
When government forces people to help their neighbors, conscience
atrophies. When people are free to choose whether to help their
neighbors or not, conscience is strengthened.
-- Jacob G. Hornberger
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      05-09-2007
On May 9, 11:29 am, Mr P <(E-Mail Removed)> wrote:
> Sounds just like keys %h don't it.
>
> Starting with
>
> $_ =
> "cat
> man
> dog
> mouse
> man
> man
> ";
>
> I want to end up with
>
> [cat man dog mouse]
>
> My approach is:
>
> my @s = split /\n/;
> # unique-afy it!
> my %s;
> @s{@s} = @s;
> @s = keys %s;
>
> My PREFERENCE is to not have to involve both a hash and an array, and
> also to not have to include a non-native lib module.
>
> Is there a syntax I can use to just work with the hash and populate
> the keys-only directly from the split? I don't really care what the
> values are. I don't even care if they are undef.


I don't think you're going to get around using both a hash and an
array, without some serious mumbo-jumbo involving repeatedly looping
through the array as you're building it. That will be unreadable and
inefficient.

The "right" answer is:
my %h = map { $_ => 1 } split /\n/, $_;
my @s = keys %h;

If you're worried about having too many named variables, however, you
could sacrifice some readability by making the hash anonymous:

my @s = keys %{ {map { $_ => 1 } split /\n/, $_} };

Perhaps you should be asking yourself why you're making this
constraint, however.

Paul Lalli

 
Reply With Quote
 
Charlton Wilbur
Guest
Posts: n/a
 
      05-09-2007
>>>>> "P" == P <(E-Mail Removed)> writes:

P> my @s = split /\n/;
P> my %s;
P> @s{@s} = @s;
P> @s = keys %s;

P> My PREFERENCE is to not have to involve both a hash and an
P> array, and also to not have to include a non-native lib module.

Well, those are your two options.

You can use a hash and an array, or you can include a non-core module.

P> Is there a syntax I can use to just work with the hash and
P> populate the keys-only directly from the split? I don't really
P> care what the values are. I don't even care if they are undef.

my %s;
$s{$_}++ foreach (split /\n/);
keys %s;

Note that you're not really eliminating the array, though -- you're
just using it implicitly in the foreach loop.

Charlton




--
Charlton Wilbur
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      05-09-2007
Mr P wrote:
> Sounds just like keys %h don't it.
>
> Starting with
>
> $_ =
> "cat
> man
> dog
> mouse
> man
> man
> ";
>
> I want to end up with
>
> [cat man dog mouse]
>
> My approach is:
>
> my @s = split /\n/;
> # unique-afy it!
> my %s;
> @s{@s} = @s;
> @s = keys %s;
>
> My PREFERENCE is to not have to involve both a hash and an array, and
> also to not have to include a non-native lib module.
>
> Is there a syntax I can use to just work with the hash and populate
> the keys-only directly from the split? I don't really care what the
> values are. I don't even care if they are undef.


$ perl -le'
$_ =
"cat
man
dog
mouse
man
man
";

my @s = do {
my %unique;
grep !$unique{$_}++, split /\n/;
};

print for @s;
'
cat
man
dog
mouse



John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
 
Reply With Quote
 
Mirco Wahab
Guest
Posts: n/a
 
      05-09-2007
Mr P wrote:
> Starting with
> $_ =
> "cat
> man
> ...
> [cat man dog mouse]
>
> My approach is:
> ...
> My PREFERENCE is to not have to involve both a hash and an array, and
> also to not have to include a non-native lib module.
>
> Is there a syntax I can use to just work with the hash and populate
> the keys-only directly from the split? I don't really care what the
> values are. I don't even care if they are undef.



The closest I could come up with is
a simple match against a code assertion:

....

my %h;
$_ ='
cat
man
dog
mouse
man
man
';

() = /(\w+)(?{$h{$1}=undef})/g;

print " [@{[keys %h]}]";

....

but I couldn't get your correct order

Regards

M.
 
Reply With Quote
 
Mirco Wahab
Guest
Posts: n/a
 
      05-09-2007
Mirco Wahab wrote:
> print " [@{[keys %h]}]";


Stupid, because the hash slice *does*
already provide list context, so a simple:


@h{/(\w+)/g} = undef;

print " [@{[keys %h]}]";


would be all what's necessary.
(did too much php lately

Regards

M.


 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      05-09-2007
Mr P wrote:
> Starting with
> $_ =
> "cat
> man
> dog
> mouse
> man
> man
> ";
>
> I want to end up with
>
> [cat man dog mouse]
>
> My approach is:
>
> my @s = split /\n/;
> # unique-afy it!
> my %s;
> @s{@s} = @s;


You can split() directly into a hash slice without reading the words into an
array first:

@s{split/\n/} = undef;

> @s = keys %s;


But that doesn't save you from still using keys() to get the list of unique
words.

jue


 
Reply With Quote
 
Jorge
Guest
Posts: n/a
 
      05-09-2007
On May 9, 8:29 am, Mr P <(E-Mail Removed)> wrote:
> Sounds just like keys %h don't it.
>
> Starting with
>
> $_ =
> "cat
> man
> dog
> mouse
> man
> man
> ";
>
> I want to end up with
>
> [cat man dog mouse]
>
> My approach is:
>
> my @s = split /\n/;
> # unique-afy it!
> my %s;
> @s{@s} = @s;
> @s = keys %s;
>
> My PREFERENCE is to not have to involve both a hash and an array, and
> also to not have to include a non-native lib module.
>
> Is there a syntax I can use to just work with the hash and populate
> the keys-only directly from the split? I don't really care what the
> values are. I don't even care if they are undef.
>
> Thank-You, Peristas,
>
> MP


using arrays only --

my @arr = qw(cat man dog mouse man man);

my $prev = 'none';

my @sorted_arr = sort(@arr);

my @uniq_arr = grep($_ ne $prev && ($prev = $_), @sorted_arr);

foreach my $uniq(@uniq_arr){
print $uniq, "\n";
}

cat
dog
man
mouse

 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      05-10-2007
On May 9, 5:28 pm, Mirco Wahab <(E-Mail Removed)> wrote:

> @h{/(\w+)/g} = undef;


That is a list assignment so the RHS evaluates to a list containing a
single undef element.

Although that would work (as would _any_ list) it would be more
idiomatic to write:

@h{/(\w+)/g} = ();

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a unique method in python to unique a list? Token Type Python 9 09-09-2012 02:13 PM
list question... unique values in all possible unique spots ToshiBoy Python 6 08-12-2008 05:01 AM
Need help randomly selecting unique values from an array Paul Ruby 3 08-30-2007 05:25 PM
How to create an array of unique random numbers for an online quiz alanbe Javascript 15 06-03-2005 11:37 AM
JDO Question : Is it possible to get unique values like DISTINCT in SQL? Klixx0r Java 1 12-08-2004 10:34 AM



Advertisments