Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

strictvars's scratchpad

by strictvars (Sexton)
on Apr 07, 2005 at 20:14 UTC ( #445866=scratchpad: print w/replies, xml ) Need Help??

Idioms:
@hash{@array} = @array; for (<*.*>) { $totalsize += -s } @char_num{'A'..'Z'} = 1..26; print sort <> @div5 = grep { not $_ % 5} @n; print map "$_: $h{$_}\n", sort keys %h;
PERL one-liners
# run contents of "my_file" as a program perl my_file # run debugger "stand-alone" perl -d -e 42 # run program, but with warnings perl -w my_file # run program under debugger perl -d my_file # just check syntax, with warnings perl -wc my_file # useful at end of "find foo -print" perl -nle unlink # simplest one-liner program perl -e 'print "hello world!\n"' # add first and penultimate columns perl -lane 'print $F[0] + $F[-2]' # just lines 15 to 17 perl -ne 'print if 15 .. 17' *.pod # in-place edit of *.c files changing all foo to bar perl -p -i.bak -e 's/\bfoo\b/bar/g' *.c # command-line that prints the first 50 lines (cheaply) perl -pe 'exit if $. > 50' f1 f2 f3 ... # delete first 10 lines perl -i.old -ne 'print unless 1 .. 10' foo.txt # change all the isolated oldvar occurrences to newvar perl -i.old -pe 's{\boldvar\b}{newvar}g' *.[chy] # command-line that reverses the whole file by lines perl -e 'print reverse <>' file1 file2 file3 .... # find palindromes perl -lne 'print if $_ eq reverse' /usr/dict/words # command-line that reverse all the bytes in a file perl -0777e 'print scalar reverse <>' f1 f2 f3 ... # command-line that reverses the whole file by paragraphs perl -00 -e 'print reverse <>' file1 file2 file3 .... # increment all numbers found in these files perl i.tiny -pe 's/(\d+)/ 1 + $1 /ge' file1 file2 .... # command-line that shows each line with its characters backwards perl -nle 'print scalar reverse $_' file1 file2 file3 .... # delete all but lines beween START and END perl -i.old -ne 'print unless /^START$/ .. /^END$/' foo.txt # binary edit (careful!) perl -i.bak -pe 's/Mozilla/Slopoke/g' /usr/local/bin/netscape # look for dup words perl -0777 -ne 'print "$.: doubled $_\n" while /\b(\w+)\b\s+\b\1\b/ +gi' # command-line that prints the last 50 lines (expensively) perl -e 'lines = <>; print @@lines[ $#lines .. $#lines-50' f1 f2 f3 + ...
printf examples
print ":1234567890:\n"; printf ":%7s:\n", "abcdefghij"; # min width, string is longer printf ":%7s:\n", "abc"; # min width, string is shorter ri +ght align printf ":%-7s:\n", "abc"; # min width, string is shorter le +ft align print "---\n"; printf ":%.7s:\n", "abc"; # max width, string is shorter printf ":%.7s:\n", "abcdefghij"; # max width, string is longer print "---\n"; printf ":%7.7s:\n", "abcdefghij"; # min.max and they are the same printf ":%7.7s:\n", "abc"; # min.max and they are the same print "---\n"; printf ":%3d:\n", 3; # min width printf ":%0.3d:\n", 3; # min width with 0 padding
Results:
:1234567890: :abcdefghij: : abc: :abc : --- :abc: :abcdefg: --- :abcdefg: : abc: --- : 3: :003:
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2022-08-10 04:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?