1 / 7
Feb 2006

print while($_=<>)!=42

Any golfers want to show me how it's really done?

  • created

    Feb '06
  • last reply

    Sep '16
  • 6

    replies

  • 1.5k

    views

  • 6

    users

A few other versions:

/42/..exit,print for<>

print while($_=<>)-42

#!perl -p
/42/&&exit
1 year later

(join '',<>)=~/42/ && print $`

Not the best idea in theory, but apparently the input is small enough that this runs in 0.00 s.

4 months later

Using just /42/ is not accurate, because it matches anything that contains 42 (e.g. 142).

If output to STDERR doesn't matter, then these work:

#!/usr/bin/perl -p
$_-42||&
#!/usr/bin/perl -p
/^42$/&&&
5 years later
4 years later

I submitted my answer but it failed to recognize it as a correct answer, despite it saying it was correct at ideaone.com. I assume it may be because of the perl version I am using (5.10) versus the ones available in the drop down (5.20 and 6). If anyone can take a look and see if there is anything I am missing, it would be greatly appreciated.

my @numbers = ("1","2","88","42","99");

foreach (@numbers){
if ($_ !=42) {
print $_."\n";
}
else {
exit;
}
}

Update:
I was able to correct my answer by clarifying where stdin was coming from.

my @numbers = ;