[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

How to mimic Perl's `s///' in Ruby?

Jos Backus

2/9/2005 8:10:00 PM

Given Perl's

$_ = "123 foo";

s/^(\d+)\s+//;
$pid = $1;

print "$_\n$pid\n";

all I can come up with is

line = "123 foo"

pid = nil
line.sub!(/^(\d+)\s+/) {pid = $1; ''}

puts line, pid

Is there a better way perhaps?

--
Jos Backus _/ _/_/_/ Sunnyvale, CA
_/ _/ _/
_/ _/_/_/
_/ _/ _/ _/
jos at catnook.com _/_/ _/_/_/ require 'std/disclaimer'


17 Answers

James Gray

2/9/2005 8:26:00 PM

0

On Feb 9, 2005, at 2:10 PM, Jos Backus wrote:

> Given Perl's
>
> $_ = "123 foo";
>
> s/^(\d+)\s+//;
> $pid = $1;
>
> print "$_\n$pid\n";
>
> all I can come up with is
>
> line = "123 foo"
>
> pid = nil
> line.sub!(/^(\d+)\s+/) {pid = $1; ''}
>
> puts line, pid
>
> Is there a better way perhaps?

I don't think I understand the question. Ruby supports replacement
with and empty string and even $1, just like Perl.

line.sub!(/^((\d+)\s+/, "")
puts "#{line}\n#{$1}"

Hope that helps.

James Edward Gray II



djberg96

2/9/2005 8:29:00 PM

0

Jos Backus wrote:
> Given Perl's
>
> $_ = "123 foo";
>
> s/^(\d+)\s+//;
> $pid = $1;
>
> print "$_\n$pid\n";
>
> all I can come up with is
>
> line = "123 foo"
>
> pid = nil
> line.sub!(/^(\d+)\s+/) {pid = $1; ''}
>
> puts line, pid
>
> Is there a better way perhaps?

Use split over a regex when you can:

pid, line = "123 foo".split

puts pid
puts line

Also keep in mind that 'pid' is currently still a string, not a number
(if that matters to you).

Regards,

Dan

Glenn Parker

2/9/2005 8:53:00 PM

0

Jos Backus wrote:
> Given Perl's
>
> $_ = "123 foo";
>
> s/^(\d+)\s+//;
> $pid = $1;
>
> print "$_\n$pid\n";
>
> all I can come up with is
>
> line = "123 foo"
>
> pid = nil
> line.sub!(/^(\d+)\s+/) {pid = $1; ''}
>
> puts line, pid

Closer to the original Perl:

line = "123 foo"
line.sub!(/^(\d+)\s+/, '')
pid = $1
puts line, pid

The Pickaxe doc for String#sub states the MatchData $-variables will be
available using the block form of String#sub!, but it doesn't bother to
remind you that they are available in the non-block form as well.

--
Glenn Parker | glenn.parker-AT-comcast.net | <http://www.tetrafoi...


Thomas E Enebo

2/9/2005 9:06:00 PM

0

On Thu, 10 Feb 2005, Glenn Parker defenestrated me:

> Jos Backus wrote:
> >Given Perl's
> >
> > $_ = "123 foo";
> >
> > s/^(\d+)\s+//;
> > $pid = $1;
> >
> > print "$_\n$pid\n";
>
> Closer to the original Perl:
>
> line = "123 foo"
> line.sub!(/^(\d+)\s+/, '')
> pid = $1
> puts line, pid

Even a little closer to the original perl...Though I am sure you are
not asking how to get ruby syntax closer to Perls :)

alias :s :sub

$_ = "123 foo";

s /^(\d+)\s+/, '';
$pid = $1;

print "#$_\n#$pid\n";

-Tom

--
+ http://www.tc.umn.... +---- mailto:enebo@acm.org ----+
| Thomas E Enebo, Protagonist | "A word is worth a thousand |
| | pictures" -Bruce Tognazzini |


Florian Gross

2/9/2005 9:24:00 PM

0

Glenn Parker wrote:

> Closer to the original Perl:
>
> line = "123 foo"
> line.sub!(/^(\d+)\s+/, '')
> pid = $1
> puts line, pid

line = "123 foo"
pid = line.slice!(/\d+/, 1).to_i
puts line, pid

..slice!() is the in-place version of [] that will delete the matched
stuff as well as returning it. Note that you can use Regexps with [] and
even []=.

str = "hello world"
str[/.(.)/, 1] # => "e"
str[/\s(.+)/, 1] = "bar"
str # => "hello bar"

Jos Backus

2/9/2005 10:01:00 PM

0

On Thu, Feb 10, 2005 at 05:52:34AM +0900, Glenn Parker wrote:
> The Pickaxe doc for String#sub states the MatchData $-variables will be
> available using the block form of String#sub!, but it doesn't bother to
> remind you that they are available in the non-block form as well.

Duh, for some reason I didn't realize that $1 etc. are available _outside_ the
block in the first place as they are global.

Thanks for all your responses people, enlightening as always.

--
Jos Backus _/ _/_/_/ Sunnyvale, CA
_/ _/ _/
_/ _/_/_/
_/ _/ _/ _/
jos at catnook.com _/_/ _/_/_/ require 'std/disclaimer'


Jos Backus

2/9/2005 10:03:00 PM

0

On Thu, Feb 10, 2005 at 05:25:52AM +0900, James Edward Gray II wrote:
> I don't think I understand the question. Ruby supports replacement
> with and empty string and even $1, just like Perl.

Somehow I didn't realize the significance of the $ in $1.

> line.sub!(/^((\d+)\s+/, "")
> puts "#{line}\n#{$1}"
>
> Hope that helps.

Sure does, thanks.

--
Jos Backus _/ _/_/_/ Sunnyvale, CA
_/ _/ _/
_/ _/_/_/
_/ _/ _/ _/
jos at catnook.com _/_/ _/_/_/ require 'std/disclaimer'


Jos Backus

2/9/2005 10:44:00 PM

0

On Thu, Feb 10, 2005 at 06:25:06AM +0900, Florian Gross wrote:
[snip]
> str = "hello world"
> str[/.(.)/, 1] # => "e"
> str[/\s(.+)/, 1] = "bar"
> str # => "hello bar"

and

$1 # => "world"

Isn't it too bad a non-match raises an IndexError instead of returning nil?

irb(main):006:0> if str[/(hello)/, 1] = ""; puts "match", $1; end
match
hello
=> nil
irb(main):007:0> if str[/(hallo)/, 1] = ""; puts "match", $1; end
IndexError: regexp not matched
from (irb):7:in `[]='
from (irb):7
irb(main):008:0>

--
Jos Backus _/ _/_/_/ Sunnyvale, CA
_/ _/ _/
_/ _/_/_/
_/ _/ _/ _/
jos at catnook.com _/_/ _/_/_/ require 'std/disclaimer'


Robert Klemme

2/9/2005 10:51:00 PM

0


"Jos Backus" <jos@catnook.com> schrieb im Newsbeitrag
news:20050209201021.GA47301@lizzy.catnook.com...
> Given Perl's
>
> $_ = "123 foo";
>
> s/^(\d+)\s+//;
> $pid = $1;
>
> print "$_\n$pid\n";
>
> all I can come up with is
>
> line = "123 foo"
>
> pid = nil
> line.sub!(/^(\d+)\s+/) {pid = $1; ''}
>
> puts line, pid
>
> Is there a better way perhaps?

Others have shown ways to mimic Perl - even so far as to include the
trailing semicolons. I don't know why you want to mimic Perl, but here's
how I'd do it in Ruby:

if /^(\d+)\s+(.*)$/ =~ str
pid, name = $1, $2
else
# no match
end

Or, if you expect it to always match:

raise "String error: #{str}" unless /^(\d+)\s+(.*)$/ =~ str
pid, name = $1, $2

Kind regards

robert

Wybo Dekker

2/9/2005 11:33:00 PM

0