Robert Citek
12/20/2007 3:28:00 PM
On Dec 20, 2007 2:40 AM, Robert Klemme <shortcutter@googlemail.com> wrote:
> 2007/12/20, Robert Citek <robert.citek@gmail.com>:
> > Found it: indices, which is deprecated in favor of values_at.
> >
> > $ echo $(seq 1 10) | tee /dev/stderr | ruby -lane 'print
> > $F.values_at(1,2,3,8,2,2).join(" -- ") '
> > 1 2 3 4 5 6 7 8 9 10
> > 2 -- 3 -- 4 -- 9 -- 3 -- 3
>
> Why not
>
> $ ruby -e 'a=(1..10).to_a; $stderr.puts(a.join(" ")); puts
> a.values_at(1,2,3,8,2,2).join(" -- ")'
> 1 2 3 4 5 6 7 8 9 10
> 2 -- 3 -- 4 -- 9 -- 3 -- 3
>
> ?
First, thank you for the example. Made me wonder about the .to_a
method and discover that 1..10 works differently in ruby than in perl:
$ perl -le 'print 1..10'
12345678910
$ ruby -le 'print 1..10'
1..10
To answer your question: because 'echo $(seq 1 10)' represents the
abstract "any delimited input data" model, which could be multi-line,
thus the -lane options. Also, because I tend to build up my
quick-and-dirty scripts from the command line: first head the file,
then filter/modify the data stream, repeat quickly. So the example
above is merely one in a series of commands:
$ seq 1 10
$ echo $(seq 1 10)
$ echo $(seq 1 10) | tee /dev/stderr
$ echo $(seq 1 10) | tee /dev/stderr | ruby -lane 'print $F'
$ echo $(seq 1 10) | tee /dev/stderr | ruby -lane 'print $F.join(" -- ") '
$ echo $(seq 1 10) | tee /dev/stderr | ruby -lane 'print $F(1,2).join(" -- ") '
$ echo $(seq 1 10) | tee /dev/stderr | ruby -lane 'print
$F[1,2,3].join(" -- ") '
$ echo $(seq 1 10) | tee /dev/stderr | ruby -lane 'print
$F.values_at(1,2).join(" -- ") '
$ echo $(seq 1 10) | tee /dev/stderr | ruby -lane 'print
$F.values_at(1,2,3).join(" -- ") '
$ echo $(seq 1 10) | tee /dev/stderr | ruby -lane 'print
$F.values_at(1..5).join(" -- ") '
$ echo $(seq 1 10) | tee /dev/stderr | ruby -lane 'print
$F.values_at(1,2,3,8,2,2).join(" -- ") ' # posted example
$ head /etc/passwd | tee /dev/stderr | ruby -F: -lane 'print
$F.values_at(1,2,3,8,2,2).join(" -- ") '
$ head /etc/passwd | tee /dev/stderr | ruby -F: -lane 'print
$F.values_at(0,2..5).join(" -- ") '
$ head /etc/passwd | tee /dev/stderr | ruby -F: -lane 'print
$F.values_at(0,2..5).join(" -- ") if $F[2] >= 1000 '
$ head /etc/passwd | tee /dev/stderr | ruby -F: -lane 'print
$F.values_at(0,2..5).join(" -- ") if $F[2].to_i >= 1000 '
$ cat /etc/passwd | tee /dev/stderr | ruby -F: -lane 'print
$F.values_at(0,2..5).join(" -- ") if $F[2].to_i >= 1000 '
Regards,
- Robert