perl5++

hex on 2007-03-14T15:58:00

And #perl++ too.

15:30 < hex> golf suggestions plz: $foo = 1 if ($bar ne 'foonly' || $baz ne 'foonly' || $honk ne 'foonly');
...
15:36 < claes> hex: working on job security?
15:37 < hex> naw, just avoiding a pile of ||s.
15:37 < claes> ah
15:37 < hex> I went for my $foo = 0; foreach ($bar, $baz, $honk) { $foo = 1 if $_ ne 'foonly'; }
15:37 < claes> hex: use Perl6::Junction
15:37 < hex> claes: :D
15:38 < mauke> (grep $_ eq 'foonly', $bar, $baz, $honk) == 3 or $foo = 1;
15:38 < avar> my $foo; sub { $_ ne "foonly" && $foo = 1 for @_; 0 }->(@vars);
15:40 < kane[work]> we *so* need an 'in' statement
15:40 < mauke> "$foo $bar $baz" eq "foonly" x 3 or $foo = 1;
15:40 < kane[work]> $foo++ if 'foonly' in ($foo,$bar,$baz);
15:41 < avar> kane[work]: it's in the next release of perl
15:41 < kane[work]> of course it is
15:41 < kane[work]> everything is in perl6 :)
15:41 < clintp> And a pony.
15:41 < avar> if ("foonly" ~~ @vars) { $foo = 1 }
15:41 < avar> I mean 5.10
15:41 < kane[work]> ah
15:41 < kane[work]> perl5++
15:41 < clintp> perl5++
15:41 < kane[work]> perl5++ # exists
15:43 < mauke> "@{ +{foonly => 1} }{$foo, $bar, $baz}" eq "1 1 1" or $foo = 1;

TIMTOWTDI: It's a lifestyle choice.

Apparently ~~ is the smart match operator. (Thanks to mauke for bringing it to my attention, and to clintp for the link.) I'm looking forward to 5.10....

Postscript:

17:08 < broquaint> To add to hex's golf conversation because I'm too lazy to post to use.perl: my $foo = 'foonly' !~ /^(?:$bar|$baz|$honk)$/;


More (better?) ways to do it

Aristotle on 2007-03-14T20:22:37

<hex> I went for my $foo = 0; foreach ($bar, $baz, $honk) { $foo = 1 if $_ ne 'foonly'; }

Is that really what you mean? With it, the value of $foo depends only on $honk; the values of $bar and $baz don’t come into play at all, even though you test these variables.

Also,

<avar> if ("foonly" ~~ @vars) { $foo = 1 }

I think that could be just $foo = "foonly" ~~ @vars;.

But the solution I’d use is

$foo = 1 if grep $_ ne 'foonly', $bar, $baz, $honk;

I don’t consider that obfuscated either; “if grep” is a well-known idiom.

Although these days, I’d have List::MoreUtils installed anyway, in which case it’s just:

$foo = any { $_ ne 'foonly' } $bar, $baz, $honk;

Re:More (better?) ways to do it

hex on 2007-03-14T21:49:25

Is that really what you mean? With it, the value of $foo depends only on $honk; the values of $bar and $baz don’t come into play at all, even though you test these variables.

Are you sure?

#!/usr/bin/perl

my $foo = 0;

my $bar = 'foo';
my $baz = 'foonly';
my $honk = 'foonly';

foreach ($bar, $baz, $honk) {
  $foo = 1 if $_ ne 'foonly';
}

print "$foo\n";
That produces "1" for me, as I expected.

The "if grep" isn't an idiom I'd previously heard. I'll bear it in mind in future.

Re:More (better?) ways to do it

Aristotle on 2007-03-14T23:36:22

Are you sure?

Err, d’oh. Somehow I read the code as if you were resetting the variable on each iteration, which is clearly not the case. My bad.