Re: Getting keys of mappe @list = qw/foo bar foo baz/;d hash?
Tony Bowden <[EMAIL PROTECTED]> writes: > OK, my brain has fried. > > How can I do this: > > my @list = qw/foo bar foo baz/; > my %hash = map { $_ => 1 } @men; > my @sort = sort keys %hash; > print "We have @sort\n"; # foo bar baz > > without the %hash? > > i.e. something akin to: my @sort = sort keys map { $_ => 1 } @list; > > I've tried so many variations that my brain is refusing to tell me the > correct answer. my $last; do {print $_ unless $_ eq $last; $last = $_} for sort @list Dunno if it's quicker than the cunning tricks with map, but hey, it works. -- Piers
Re: Getting keys of mappe @list = qw/foo bar foo baz/;d hash?
On Wed, Jan 03, 2001 at 11:16:06AM +, Piers Cawley wrote: > > How can I do this: > > my @list = qw/foo bar foo baz/; > > my %hash = map { $_ => 1 } @men; > > my @sort = sort keys %hash; > > print "We have @sort\n"; # foo bar baz > > without the %hash? > my $last; > do {print $_ unless $_ eq $last; $last = $_} for sort @list > Dunno if it's quicker than the cunning tricks with map, but hey, it > works. Ah, but my question was badly phrased. I needed to end up with @sort, not a printed list ... The result of this was heading towards another couple of maps ;) I had found the solution long before this turned up though (stupidly forgot to update my .muttrc for the new list address, so I had to go through moderation again ...) - just a matter of doubling those curly brackets (no need for all those +s etc...) Thanks, Tony -- - Tony Bowden | Belfast, NI | [EMAIL PROTECTED] | www.tmtm.com | www.blackstar.co.uk you move in waves like the midnight blues you vector of this weird dis-ease -
Re: Getting keys of mappe @list = qw/foo bar foo baz/;d hash?
Tony Bowden <[EMAIL PROTECTED]> writes: > On Wed, Jan 03, 2001 at 11:16:06AM +, Piers Cawley wrote: > > > How can I do this: > > > my @list = qw/foo bar foo baz/; > > > my %hash = map { $_ => 1 } @men; > > > my @sort = sort keys %hash; > > > print "We have @sort\n"; # foo bar baz > > > without the %hash? > > > my $last; > > do {print $_ unless $_ eq $last; $last = $_} for sort @list > > Dunno if it's quicker than the cunning tricks with map, but hey, it > > works. > > Ah, but my question was badly phrased. I needed to end up with @sort, not > a printed list ... Ah: do {push(@sorted, $_) unless $_ eq $last; $last = $_} for sort @list; Can't be bothered to benchmark it... -- Piers
Re: Getting keys of mappe @list = qw/foo bar foo baz/;d hash?
On 4 Jan 2001, Piers Cawley wrote: > Tony Bowden <[EMAIL PROTECTED]> writes: > > > On Wed, Jan 03, 2001 at 11:16:06AM +, Piers Cawley wrote: > > > > How can I do this: > > > > my @list = qw/foo bar foo baz/; > > > > my %hash = map { $_ => 1 } @men; > > > > my @sort = sort keys %hash; > > > > print "We have @sort\n"; # foo bar baz > > > > without the %hash? > > > > > my $last; > > > do {print $_ unless $_ eq $last; $last = $_} for sort @list > > > Dunno if it's quicker than the cunning tricks with map, but hey, it > > > works. > > > > Ah, but my question was badly phrased. I needed to end up with @sort, not > > a printed list ... > > Ah: > > do {push(@sorted, $_) unless $_ eq $last; $last = $_} for sort @list; > > Can't be bothered to benchmark it... This would be very slow for the dutch national flag, as it sorts the large list rather than the small one. S. -- Shevek I am the Borg. sub AUTOLOAD { ($s=$AUTOLOAD)=~s/.*:://; eval qq{ *$AUTOLOAD=$s ?sub {$s*&{$s-1}} :sub {1}; }; goto &$AUTOLOAD; } print &{'4'};
Re: Getting keys of mappe @list = qw/foo bar foo baz/;d hash?
Shevek <[EMAIL PROTECTED]> writes: > On 4 Jan 2001, Piers Cawley wrote: > > > Tony Bowden <[EMAIL PROTECTED]> writes: > > > > > On Wed, Jan 03, 2001 at 11:16:06AM +, Piers Cawley wrote: > > > > > How can I do this: > > > > > my @list = qw/foo bar foo baz/; > > > > > my %hash = map { $_ => 1 } @men; > > > > > my @sort = sort keys %hash; > > > > > print "We have @sort\n"; # foo bar baz > > > > > without the %hash? > > > > > > > my $last; > > > > do {print $_ unless $_ eq $last; $last = $_} for sort @list > > > > Dunno if it's quicker than the cunning tricks with map, but hey, it > > > > works. > > > > > > Ah, but my question was badly phrased. I needed to end up with @sort, not > > > a printed list ... > > > > Ah: > > > > do {push(@sorted, $_) unless $_ eq $last; $last = $_} for sort @list; > > > > Can't be bothered to benchmark it... > > This would be very slow for the dutch national flag, as it sorts the large > list rather than the small one. Point. It's all data dependent isn't it? -- Piers