swestrup: (Default)
[personal profile] swestrup
I'm kinda new to PHP, and I'm wondering what is the best way to perform this operation:

I have three variables, $a, $b and $c. Each contains either a single character, or nothing.

What's the best way to test that no two defined values are the same?

Heck, I'm not even sure what the best way to do this would be in Perl, allthough I have a few ideas involving hashes.

Date: 2006-02-22 07:09 pm (UTC)
From: [identity profile] sps.livejournal.com
Yeah, in Perl, you can do something perverted but cute like

sub coll(@) {my %a; @a{@_}=(); @_ == keys %a}

or more sensibly if less tersely

sub coll(@) {my %a; for my $a (@_) {return undef if $a{$a}++}; 1}

Date: 2006-02-22 07:26 pm (UTC)
From: [identity profile] sps.livejournal.com
...Though for only three items, $a ne $b && $a ne $c && $b ne $c is likely fastest.

Date: 2006-02-22 09:18 pm (UTC)
From: [identity profile] sps.livejournal.com
Oh, sorry.

sub coll(@) {my @a = grep $_, @_; %a; @a{@a}=(); @a == keys %a}

or

sub coll(@) {my %a; for my $a (@_) {return undef if $a && $a{$a}++}; 1}

—but of course, that's still just Perl.

January 2017

S M T W T F S
1234567
891011121314
15161718192021
22232425262728
293031    

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Feb. 13th, 2026 05:14 pm
Powered by Dreamwidth Studios