Okay, here's my attempt at this one, it should be slightly more efficient than using arrays, but you really should benchmark that...:
#!/usr/bin/perl -w use strict; unless ($ARGV[0] eq '-lc' || $ARGV[0] eq '-uc' ){ die "usage: lcuc [-uc | -lc] file\n"; } my $op = substr(shift @ARGV, 1, 2); while(<>){ no strict 'refs'; print &{$op}($_) } sub lc{ lc shift } sub uc{ uc shift }
TMTOWTDI Update (as suggested by tilly)
I'll admit that this one is cleaner than the above code. :)
<kbd>--#!/usr/bin/perl -w use strict; my %ops = ( 'lc' => sub {lc shift }, 'uc' => sub {uc shift }, ); my $case = shift @ARGV; unless (exists $ops{$case}){ die "usage: lcuc [", join(' | ', keys %ops), "] file\n"; } while(<>){ print $ops{$case}->($_); }
In reply to Re: More efficient munging if infile very large
by OeufMayo
in thread More efficient munging if infile very large
by ybiC
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |