From: Joel VanderWerf Date: 2004-09-16T09:39:02+09:00 Subject: Re: Ex-Perl coders: Howz it feel to convert to Ruby? ChrisO wrote: > This is where I'm looking to get with Ruby and I still reach for Perl > because I don't know Ruby as well. It happened today at work. I needed > to extract some server names from a piece of Perl code (text) that > looked like this: > > { SERVER => "servername1", SHARE = $Default }, > { SERVER => "servername2", SHARE = $Default }, > { SERVER => "servername3", SHARE = $Default }, > { SERVER => "servername4", SHARE = $Default }, > { SERVER => "servername5", SHARE = $Default }, > { SERVER => "servername6", SHARE = $Default }, > { SERVER => "servername7", SHARE = $Default }, > { SERVER => "servername8", SHARE = $Default }, > > (The server names were more unique than the contrived names above.) In > Perl it was: > > perl -e 'while (<>) { /"(\w*)"/; print "$1\n" }' file-snippet.txt > > In Ruby? I didn't have the time to figure it out. Which is the > agonizing point. Later, I did the following in Ruby to satisfactory > effect: > > ruby -e 'ARGF.each { |line| line.scan( /"(\w*)"/ ) { |frag| puts frag } > }' file-snippet.txt > > Perhaps someone can refine this while we're at it. It's hard not to > notice that the Perl was more concise. Perhaps my ignorance can be made > right...? Can this be shortened in Ruby? Surely... Golf.shoes = "on" $ ruby -n -e 'puts $1 if /"(\w+)"/' file-snippet.txt servername1 servername2 servername3 servername4 servername5 servername6 servername7 servername8