Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

hex (3272)

hex
  (email not shown publicly)
http://downlode.org/

Perl, RDF and wiki hacker, London, UK. This is my former Perl blog; I now write at Earle's Notebook [downlode.org].

Journal of hex (3272)

Wednesday March 14, 2007
10:58 AM

perl5++

[ #32684 ]
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)$/;

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • <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 tha

    • 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.

      • 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.