Hot answers tagged

30

eval eval '"'. ('['^"\+").( ('[')^ ')').('`'|')'). ('`'|'.'). ('['^'/').('{'^'[' ).'\\'.'"' .('`'^ '*').('['^'.').('['^ '(').('['^'/').('{'^ '[').('`'|'!').(('`')| '.').('`'|'/').("\["^ '/').('`'|'(').('`'|'%' ).('['^')').(('{')^ ...


21

-n and unmatched curly brackets It is well known that the command line switch -n can be used to execute the script once for every line. perl --help says: -n assume "while (<>) { ... }" loop around program What it doesn't say explicitly is that Perl doesn't just assume a loop around the program; it literally wraps while (<>) { ...


15

Don't use qw. This is waste of two characters that could be used in better way. For example, don't write the following. @i=qw(unique value); Instead use barewords. @i=(unique,value); Or if you cannot use barewords, use glob syntax. @i=<unique value>; glob syntax can also be used for interesting effects. @i=<item{1,2,3}>;


11

Use non-word characters as variable names Using $% instead of $a can allow you to place the variable name right next to an if, for or while construct as in: @r=(1,2,3,4,5);$%=4; print$_*$%for@r Many can be used, but check the docs and @BreadBox's answer for which ones have magic effects! Use map when you can't use statement modifiers If you can't use ...


9

redo adds loop behavior to a block without for or while. {redo} is an infinite loop.


5

Perl 53 bytes You can save two more strokes: <>;print${\map$a-=(-1)**$_/(2*$_-1),1..$_},$a=$/for<>


5

You can run multiple different statements within nested ternary logic. Suppose you have a big if-elsif statement. This could be any logic and any number of statements. if( $_ < 1 ) { $a=1; $b=2; $c=3; say $a.$b.$c; } elsif($_ < 2 ) { $a=3; $b=2; $c=1; say $a.$b.$c; } elsif( $_ < 3) { $a=2; $b=2; $c=2; ...


5

Try to use the value of an assignment expression, like so: # 14 characters $n=pop;$o=$n&1 # 13 characters, saves 1 $o=($n=pop)&1 This works because $n is 2 characters in Perl. You may change $n to () at no cost, and save 1 semicolon by moving the assignment into the parentheses.


4

Less than 70 character on 3 lines: $_=unpack("H21","9Ø HvÂláµöÂ");s/(.)/" "x(hex$1<3).substr "AHPJocehtunarslk",hex$1,1/eg;say Care: It's ISO-8859-1 encoded. perl -E '$_=unpack("H21","9Ø HvÂláµöÂ");s/(.)/" "x(hex$1<3).substr "AHPJocehtunarslk",hex$1,1/eg;say' Just Another Perl Hacker More than 70 chars at all, but less than 100! wc -clL <<&...


4

Use select(undef,undef,undef,$timeout) instead of Time::HiRes (Taken from https://stackoverflow.com/a/896928/4739548) Many challenges require you to sleep with greater precision than integers. select()'s timeout argument can do just that. select($u,$u,$u,0.1) is much more efficient than: import Time::HiRes qw(sleep);sleep(0.1) The former is only 20 ...


3

Perl 55 bytes <>;map($s-=(-1)**$_/(2*$_-1),1..$_),$s=!print$s,$/for<> Without using any Perl non-standard features (such as say). 3 bytes are wasted removing the first value from the input. Sample usage: in.dat 2 10 20 $ perl leibniz.pl < in.dat 0.760459904732351 0.77290595166696 Alternative, also 55 bytes map{$s-=(-1)**$_/($_*2-1)for ...


2

Scala, 231 214 199 193 bytes Immutability always wins! (turns out the long package name of collection.mutable.Map wasn't worth the hassle...) class A{var a=Map[String,Any]() def g(s:String*):Any={val y=s(0);if(s.size>1)g(y).asInstanceOf[A].g(s.drop(1):_*)else if(a.contains(y))a(y)else{a+=y->new A;a(y)}} def s(s:String,v:Any)=a+=s->v} Only thing ...


1

C#, 215 bytes class A:System.Collections.Generic.Dictionary<string,object>{public void B(string a,object b){if(ContainsKey(a))this[a]=b;else Add(a,b);}public dynamic C(string a){if(!ContainsKey(a))Add(a,new A());return this[a];}} The class is named A. Use hash.B("key", "val"); to set a value, and hash.C("key"); to get a value. Nested keys can be ...


1

Shorten your print statements Unless the challenge specifies otherwise, you don't need trailing newlines. Our 'challenge' says 'output a random number from 0 to 9 to STDOUT'. We can take this code (28 bytes): $s=int(rand(10));print"$s\n" And shorten it to this (25 bytes): $s=int(rand(10));print $s by simply printing the variable. This last one only ...


1

use strict;*1=*CORE'die,*!=*=,@=='hacker',s??'&1(@!,$/)'?ee;s;;%ENV=~m ,..$,,$&+10;e,@!=(chr.'ust',~~reverse('rehtona'),'Perl',$@);&1("@{=}") It works with strictures enabled. Although it does not work on all machines/perls, because of CORE'die and ~~%ENV. $ perl use strict;*1=*CORE'die,*!=*=,@=='hacker',s??'&1(@!,$/)'?ee;s;;%ENV=~m ,..$,,$&...


Only top voted, non community-wiki answers of a minimum length are eligible