[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

ruby equiv of perl pos

snacktime

8/12/2006 8:33:00 PM

Or to be more exact, how would I do the following in ruby?

while ($string =~ /\0/g) {
$new_string .= sprintf '\%o', (pos $string) - 1;
}

14 Answers

gga

8/12/2006 10:04:00 PM

0

snacktime ha escrito:

> Or to be more exact, how would I do the following in ruby?
>
> while ($string =~ /\0/g) {
> $new_string .= sprintf '\%o', (pos $string) - 1;
> }

#!/usr/bin/env ruby

new_string = ''
string = "\0\0\0asda\0\0\0sdasd"

pos = 0
while pos = string.index( /\0/, pos )
match = Regexp.last_match
pos += match.size
new_string << '\%o' % (pos - 1)
end

puts new_string

James Gray

8/12/2006 10:11:00 PM

0

$ ri -T IO#pos
----------------------------------------------------------------- IO#pos
ios.pos => integer
ios.tell => integer
------------------------------------------------------------------------
Returns the current offset (in bytes) of ios.

f = File.new("testfile")
f.pos #=> 0
f.gets #=> "This is line one\n"
f.pos #=> 17


James Edward Gray II

Robin Stocker

8/12/2006 10:15:00 PM

0

snacktime wrote:
> Or to be more exact, how would I do the following in ruby?
>
> while ($string =~ /\0/g) {
> $new_string .= sprintf '\%o', (pos $string) - 1;
> }

Another idea:

string = "one\0two\0threeeeeee\0"
result = ''
string.scan(/\0/) do
result << '\%o' % $`.length
end
puts result #=> \3\7\22

Robin

gga

8/12/2006 10:46:00 PM

0


> Another idea:
>
> string = "one\0two\0threeeeeee\0"
> result = ''
> string.scan(/\0/) do
> result << '\%o' % $`.length
> end

This is simpler to write, but will be slower on longer strings.

#!/usr/bin/env ruby

@string = "\0\0\0asda\0\0\0sdasd" * 5000

def test_a
result = ''
pos = 0
while pos = @string.index( /\0/, pos )
match = Regexp.last_match
pos += match.size
result << '\%o' % (pos - 1)
end
return result
end


def test_b
result = ''
@string.scan(/\0/) do
result << '\%o' % $`.length
end
return result
end

require 'benchmark'

Benchmark.benchmark() { |x|
x.report('a:') { test_a }
x.report('b:') { test_b }
}

Robin Stocker

8/12/2006 11:05:00 PM

0

gga wrote:
> This is simpler to write, but will be slower on longer strings.

You're right, it's much slower! I didn't think about speed while writing
it, thanks for pointing it out.

Another question: Why do you use Regexp.last_match? Maybe to have a more
general solution? The following seems to be simpler and faster:

def test_b
result = ''
pos = 0
while pos = @string.index("\0", pos)
result << '\%o' % pos
pos += 1
end
return result
end

snacktime

8/12/2006 11:39:00 PM

0

Thanks guys. I'm having to write a module to set parity on strings.
I'll post it when I'm done, maybe someone can help clean it
up/refactor it a bit and put it on rubyforge or something. Not sure
how much demand there is for something like this.

Logan Capaldo

8/13/2006 6:44:00 PM

0


On Aug 12, 2006, at 4:33 PM, snacktime wrote:

> Or to be more exact, how would I do the following in ruby?
>
> while ($string =~ /\0/g) {
> $new_string .= sprintf '\%o', (pos $string) - 1;
> }
>

if perldoc -f pos is describing what pos does in this case,

new_string = ""
while pos = string =~ /\0/g
new_string << "%o" % (pos - 1)
end


gga

8/13/2006 7:32:00 PM

0


Robin Stocker ha escrito:

> Another question: Why do you use Regexp.last_match? Maybe to have a more
> general solution?

Yes. That way you can use any sort of regexp, not just a single
character match.

gga

8/13/2006 7:37:00 PM

0


Logan Capaldo ha escrito:

> if perldoc -f pos is describing what pos does in this case,
>
> new_string = ""
> while pos = string =~ /\0/g
> new_string << "%o" % (pos - 1)
> end

This won't work. Regexp in ruby does not support Perl's /g option.
You need to use String.index() with a position modifier as I showed
before or gsub in case of a global regexp replacement.

Logan Capaldo

8/13/2006 7:51:00 PM

0


On Aug 13, 2006, at 3:40 PM, gga wrote:

>
> Logan Capaldo ha escrito:
>
>> if perldoc -f pos is describing what pos does in this case,
>>
>> new_string = ""
>> while pos = string =~ /\0/g
>> new_string << "%o" % (pos - 1)
>> end
>
> This won't work. Regexp in ruby does not support Perl's /g option.
> You need to use String.index() with a position modifier as I showed
> before or gsub in case of a global regexp replacement.
>
>

Oops, out of practice with my perl. Forgot that /g with a while loop
was how you did #scan { } in perl.

How about:

new_string = ""
string.scan(/\0/) do |m|
new_string = "%o" % $~.offset(0).first
end