Output A File w/ Line Numbers?

J

John Joyce

I'd like to read a file and output its contents (just to terminal is
fine for simplicity) with line numbers.
Is there a Ruby library method that handles this like unix cat?
Of course we can write a looping one-liner, but it seems to me this
function must already exist.

love,
John Joyce
 
W

William James

I'd like to read a file and output its contents (just to terminal is
fine for simplicity) with line numbers.
Is there a Ruby library method that handles this like unix cat?
Of course we can write a looping one-liner, but it seems to me this
function must already exist.

love,
John Joyce

ruby -ne 'BEGIN{$n=0}; print "#{$n+=1} "; print' junk
 
C

come

another way, but maybe less "ruby way" and more "perl way" :

ruby -ne 'print "#{$.} : #{$_}"' file
 
R

Robert Klemme

I'd like to read a file and output its contents (just to terminal is
fine for simplicity) with line numbers.
Is there a Ruby library method that handles this like unix cat?
Of course we can write a looping one-liner, but it seems to me this
function must already exist.

11:09:44 [~]: ruby -ne 'print $., " ",$_' foo.txt
1 aaa
2 bbbb
11:09:58 [~]: cat foo.txt
aaa
bbbb
11:10:01 [~]: cat -n foo.txt
1 aaa
2 bbbb
11:10:04 [~]:

If you want it more integrated in a script:

11:11:00 [~]: ruby -e 'ARGF.each {|line| print ARGF.lineno, " ", line}'
foo.txt
1 aaa
2 bbbb
11:11:03 [~]:

This also works with arbitrary IO's.

Kind regards

robert
 
R

Robert Dober

I'd like to read a file and output its contents (just to terminal is
fine for simplicity) with line numbers.
Is there a Ruby library method that handles this like unix cat?
Of course we can write a looping one-liner, but it seems to me this
function must already exist.

love,
John Joyce
ARGF.readlines.each_with_index{
| line, idx |
puts "%3d %s" % [ idx.succ, line ]
}
HTH
Robert
 
R

Robert Klemme

love,
John Joyce
ARGF.readlines.each_with_index{
| line, idx |
puts "%3d %s" % [ idx.succ, line ]
}
HTH
Robert

But Robert's solution is nicer, I did not know about ARGF.lineno, and
have overlooked his solution, sorry!

:) Thank you! Btw, you can also do ARGF.each_with_index so you do not
have to read the whole file into mem before printing it.

Kind regards

robert
 
J

John Joyce

love,
John Joyce


ARGF.readlines.each_with_index{
| line, idx |
puts "%3d %s" % [ idx.succ, line ]
}
HTH
Robert
But Robert's solution is nicer, I did not know about ARGF.lineno, and
have overlooked his solution, sorry!

:) Thank you! Btw, you can also do ARGF.each_with_index so you
do not have to read the whole file into mem before printing it.

Kind regards

robert
This .each_with_index seems to be an invaluable method... I'd like to
know more about it. Can anyone give a little more detail on how
each_with_index works? The pickaxe covered it too briefly for my
feeble mind, but clearly it's got legs.
 
R

Robert Klemme

love,
John Joyce


ARGF.readlines.each_with_index{
| line, idx |
puts "%3d %s" % [ idx.succ, line ]
}
HTH
Robert
But Robert's solution is nicer, I did not know about ARGF.lineno, and
have overlooked his solution, sorry!

:) Thank you! Btw, you can also do ARGF.each_with_index so you do
not have to read the whole file into mem before printing it.

Kind regards

robert
This .each_with_index seems to be an invaluable method... I'd like to
know more about it. Can anyone give a little more detail on how
each_with_index works? The pickaxe covered it too briefly for my feeble
mind, but clearly it's got legs.

There's not much to it. It's defined in module Enumerable and yields
the current element plus a count to the block. You can imagine it
implemented like this but of course it's written in C:

module Enumerable
def ewi
c=0
each {|x| yield x, c; c+=1}
end
end

Kind regards

robert
 
J

John Joyce

On 28.04.2007 09:06, Robert Dober wrote:
love,
John Joyce


ARGF.readlines.each_with_index{
| line, idx |
puts "%3d %s" % [ idx.succ, line ]
}
HTH
Robert
But Robert's solution is nicer, I did not know about
ARGF.lineno, and
have overlooked his solution, sorry!

:) Thank you! Btw, you can also do ARGF.each_with_index so you
do not have to read the whole file into mem before printing it.

Kind regards

robert
This .each_with_index seems to be an invaluable method... I'd like
to know more about it. Can anyone give a little more detail on how
each_with_index works? The pickaxe covered it too briefly for my
feeble mind, but clearly it's got legs.

There's not much to it. It's defined in module Enumerable and
yields the current element plus a count to the block. You can
imagine it implemented like this but of course it's written in C:

module Enumerable
def ewi
c=0
each {|x| yield x, c; c+=1}
end
end

Kind regards

robert
So let me see if I got this close to straight:
each_with_index is like each in that it alows you to iterate through
the array, but additionally hands over a counter that is also
incremented?! If this is the deal, then I'm gonna have to go to rehab
because of that method. How useful it will be. I think for the last
week I've been wanting exactly that in more places than I can think
of. Where I had been building iterating blocks with some externally
initialized counter. I knew I'd missed something golden. Hadn't
looked into the Enumerator lib yet. Been forging through the File an
IO stuff.
 
M

Martin DeMello

So let me see if I got this close to straight:
each_with_index is like each in that it alows you to iterate through
the array, but additionally hands over a counter that is also
incremented?! If this is the deal, then I'm gonna have to go to rehab
because of that method. How useful it will be. I think for the last
week I've been wanting exactly that in more places than I can think
of. Where I had been building iterating blocks with some externally
initialized counter. I knew I'd missed something golden. Hadn't
looked into the Enumerator lib yet. Been forging through the File an
IO stuff.

Wait till you feel the need for map_with_index :)

martin
 
J

James Edward Gray II

So let me see if I got this close to straight:
each_with_index is like each in that it alows you to iterate
through the array, but additionally hands over a counter that is
also incremented?!
Right.

If this is the deal, then I'm gonna have to go to rehab because of
that method. How useful it will be. I think for the last week I've
been wanting exactly that in more places than I can think of.

Just be careful. In my experience the need for and index is often a
hint that you haven't yet found the Ruby way to approach the
problem. That's not always true, but often.
Where I had been building iterating blocks with some externally
initialized counter.

See the standard generator library for doing this without a counter.

James Edward Gray II
 
J

James Edward Gray II

See the standard generator library for doing this without a counter.
You just made up that name right?
Nope:
require "generator" => true
enum = %w[one two three] => ["one", "two", "three"]
gen = Generator.new(enum)
=> #<Generator:0x14be12c @cont_endp=nil, @cont_yield=#<Continuation:
0x14be050>, cont_nextnil, queue["one"], block#<Proc:0x014c3640@/usr/
local/lib/ruby/1.8/generator.rb:71>, index0=> false

James Edward Gray II
 
J

John Joyce

See the standard generator library for doing this without a counter.
You just made up that name right?
Nope:
require "generator" => true
enum = %w[one two three] => ["one", "two", "three"]
gen = Generator.new(enum)
=> #<Generator:0x14be12c @cont_endp=nil, @cont_yield=#<Continuation:
0x14be050>, cont_nextnil, queue["one"], block#<Proc:0x014c3640@/usr/
local/lib/ruby/1.8/generator.rb:71>, index0=> false

James Edward Gray II
Sure enough it's in the Ruby Standard LIbrary, among the things the
pickaxe glances over at the end....
I'll have to dig through that. Very interesting, but could lean
towards more C++/Java-ish looking code perhaps.
Nonetheless, 1000 thanks to all.
 
R

Robert Klemme

So let me see if I got this close to straight:
each_with_index is like each in that it alows you to iterate through the
array, but additionally hands over a counter that is also incremented?!
Correct.

If this is the deal, then I'm gonna have to go to rehab because of that
method. How useful it will be. I think for the last week I've been
wanting exactly that in more places than I can think of. Where I had
been building iterating blocks with some externally initialized counter.
I knew I'd missed something golden. Hadn't looked into the Enumerator
lib yet. Been forging through the File an IO stuff.

:))

I suggest you take a very close look at Enumerable because that is one
of the most often used pieces of Ruby and it's at the core of all the
nice iterating functionality.

Then look into Enumerator which brings this functionality to all sorts
of objects (if they provide any iterating method).

Finally, check out #inject - my personal favorite.

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,241
Messages
2,571,219
Members
47,850
Latest member
StewartTha

Latest Threads

Top