[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

getting the last word in each line

Joe Van Dyk

9/12/2006 9:58:00 PM

Is there a prettier way to read a file and return the last word in each line?

Here's my go:

File.read(some_file).to_a.map { |l| line.split.last }

What if I wanted to search the lines (based on a regex) and return the
last word in each line that satisfied the search criteria?

Joe

6 Answers

Vincent Fourmond

9/12/2006 10:19:00 PM

0


Hello !

> File.read(some_file).to_a.map { |l| line.split.last }
>
> What if I wanted to search the lines (based on a regex) and return the
> last word in each line that satisfied the search criteria?

What about

File.read(some_file).to_a.map { |l| line.split.select {|v| v =~
/regex/}.last }

Vince


Ben Bleything

9/12/2006 10:24:00 PM

0

On Wed, Sep 13, 2006, Joe Van Dyk wrote:
> Is there a prettier way to read a file and return the last word in each
> line?

Well, there's #readlines...

File.readlines('somefile').map {|f| f.split.last}

...which is a little nicer.

> What if I wanted to search the lines (based on a regex) and return the
> last word in each line that satisfied the search criteria?

File.readlines('somefile').map do |f|
next unless f =~ /regex/
f.split.last
end

But you'll have nils everywhere that a line was next'ed over...

File.readlines('somefile').reject{|f| f !~ /regex/}.map {|f| f.split.last}

Someone else might have an elegant solution, but these will work :)

Ben

dominique.plante@gmail.com

9/12/2006 10:26:00 PM

0

in irb:
>>r = /\w+$/
=> /\w+$/
>>s = "bad boy"
=> "bad boy"
>>md = s.match(r)
=> #<MatchData:0x2cae00c>
>>md[0]
=> "boy"

Joe Van Dyk wrote:
> Is there a prettier way to read a file and return the last word in each
> line?
>
> Here's my go:
>
> File.read(some_file).to_a.map { |l| line.split.last }
>
> What if I wanted to search the lines (based on a regex) and return the
> last word in each line that satisfied the search criteria?
>
> Joe


--
Posted via http://www.ruby-....

Joel VanderWerf

9/12/2006 10:33:00 PM

0

Joe Van Dyk wrote:
> File.read(some_file).to_a.map { |l| line.split.last }

line[/(\w+)\s*$/, 1]

might be faster than line.split.last. Worth trying anway.
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Nobuyoshi Nakada

9/12/2006 11:24:00 PM

0

Hi,

At Wed, 13 Sep 2006 06:57:47 +0900,
Joe Van Dyk wrote in [ruby-talk:214152]:
> File.read(some_file).to_a.map { |l| line.split.last }

$ ruby -e 'p open(ARGV[0]){|f|f.grep(/(\w+)\s*$/){$1}}' version.h
["190", "20060912", "1", "9", "0", "2006", "9", "12"]

--
Nobu Nakada

Logan Capaldo

9/12/2006 11:33:00 PM

0

On Wed, Sep 13, 2006 at 07:18:44AM +0900, Vincent Fourmond wrote:
> What about
>
> File.read(some_file).to_a.map { |l| line.split.select {|v| v =~
> /regex/}.last }
That call to_a is unecessary. File.read(some_file).map { ... } will work
just as well.
>
> Vince
>