RE: Obtaining a slice of unique values from an array

2002-07-01 Thread Shishir K. Singh

Thanks Sudarshan, Felix!! This is one of the area that I never had to look. But as 
the saying goes...curiosity kills the crow...I am now all into it!!

Thanks again!!   

-Original Message-
From: Felix Geerinckx [mailto:[EMAIL PROTECTED]]
Sent: Monday, July 01, 2002 10:02 AM
To: [EMAIL PROTECTED]
Subject: RE: Obtaining a slice of unique values from an array


on Mon, 01 Jul 2002 13:45:30 GMT, [EMAIL PROTECTED] (Shishir 
K. Singh) wrote:

> This is the example "d" cited in perldoc -q duplicate
> d)  A way to do (b) without any loops or greps:

[line numbers added for reference]
 
> 01undef %saw;
> 02@saw{@in} = ();
> 03@out = sort keys %saw;  # remove sort if undesired
> 
> I am a bit confused about this example ?? 

You need to learn about hash-slices, e.g. from Uri Guttman's article 
at

<http://tlc.perlarchive.com/articles/perl/ug0001.shtml>

What's happening?
 line 01 makes sure you start with an empty hash
 line 02 creates a key-value pair in %saw foreach element of @in. The 
 value is undef (because of the '()' on the rhs, but who 
 cares ;-). The essential part here is that a hash cannot 
 have duplicate keys.
 line 03 puts the keys (which are unique by definition) in @out

-- 
felix




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: Obtaining a slice of unique values from an array

2002-07-01 Thread Felix Geerinckx

on Mon, 01 Jul 2002 13:45:30 GMT, [EMAIL PROTECTED] (Shishir 
K. Singh) wrote:

> This is the example "d" cited in perldoc -q duplicate
> d)  A way to do (b) without any loops or greps:

[line numbers added for reference]
 
> 01undef %saw;
> 02@saw{@in} = ();
> 03@out = sort keys %saw;  # remove sort if undesired
> 
> I am a bit confused about this example ?? 

You need to learn about hash-slices, e.g. from Uri Guttman's article 
at



What's happening?
 line 01 makes sure you start with an empty hash
 line 02 creates a key-value pair in %saw foreach element of @in. The 
 value is undef (because of the '()' on the rhs, but who 
 cares ;-). The essential part here is that a hash cannot 
 have duplicate keys.
 line 03 puts the keys (which are unique by definition) in @out

-- 
felix




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Obtaining a slice of unique values from an array

2002-07-01 Thread Sudarsan Raghavan

"Shishir K. Singh" wrote:

> >on Sun, 30 Jun 2002 12:08:23 GMT, Dan Fish wrote:
>
> > What is the most efficient (or at least AN efficient :-) way of
> > obtaining a slice from an array wherein the slice contains only
> > unique values found in the array?
>
> >See
> >   perldoc -q duplicate
>
> --
> >felix
> This is the example "d" cited in perldoc -q duplicate
> d)  A way to do (b) without any loops or greps:
>
> undef %saw;
> @saw{@in} = ();

This is called a hash slice, this is same as writing
foreach (@in) { $saw{$_} = undef }

>
> @out = sort keys %saw;  # remove sort if undesired
>
> I am a bit confused about this example ??
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: Obtaining a slice of unique values from an array

2002-07-01 Thread Shishir K. Singh


>on Sun, 30 Jun 2002 12:08:23 GMT, Dan Fish wrote:

> What is the most efficient (or at least AN efficient :-) way of
> obtaining a slice from an array wherein the slice contains only
> unique values found in the array?

>See
>   perldoc -q duplicate

-- 
>felix
This is the example "d" cited in perldoc -q duplicate
d)  A way to do (b) without any loops or greps:

undef %saw;
@saw{@in} = ();
@out = sort keys %saw;  # remove sort if undesired

I am a bit confused about this example ?? 

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Obtaining a slice of unique values from an array

2002-06-30 Thread Jeff 'japhy' Pinyan

On Jun 30, Dan Fish said:

>What is the most efficient (or at least AN efficient :-) way of obtaining a
>slice from an array wherein the slice contains only unique values found in
>the array?

>if @array = (1,3,5,5,3,5,2,1)  then @slice = (1,3,5,2)

This is FAQ.

  perldoc -q unique

  Found in /usr/local/lib/perl5/5.00502/pod/perlfaq4.pod
  How can I extract just the unique elements of an array?

-- 
Jeff "japhy" Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
 what does y/// stand for?   why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Obtaining a slice of unique values from an array

2002-06-30 Thread Felix Geerinckx

on Sun, 30 Jun 2002 12:08:23 GMT, Dan Fish wrote:

> What is the most efficient (or at least AN efficient :-) way of
> obtaining a slice from an array wherein the slice contains only
> unique values found in the array?

See
perldoc -q duplicate

-- 
felix

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]