Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > ruby equiv of perl pos

Reply
Thread Tools

ruby equiv of perl pos

 
 
snacktime
Guest
Posts: n/a
 
      08-12-2006
Or to be more exact, how would I do the following in ruby?

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

 
Reply With Quote
 
 
 
 
gga
Guest
Posts: n/a
 
      08-12-2006
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

 
Reply With Quote
 
 
 
 
James Edward Gray II
Guest
Posts: n/a
 
      08-12-2006
$ 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

 
Reply With Quote
 
Robin Stocker
Guest
Posts: n/a
 
      08-12-2006
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

 
Reply With Quote
 
gga
Guest
Posts: n/a
 
      08-12-2006

> 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 }
}

 
Reply With Quote
 
Robin Stocker
Guest
Posts: n/a
 
      08-12-2006
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

 
Reply With Quote
 
snacktime
Guest
Posts: n/a
 
      08-12-2006
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.

 
Reply With Quote
 
Logan Capaldo
Guest
Posts: n/a
 
      08-13-2006

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


 
Reply With Quote
 
gga
Guest
Posts: n/a
 
      08-13-2006

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.

 
Reply With Quote
 
gga
Guest
Posts: n/a
 
      08-13-2006

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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bit check using function check(var,pos) ((var)&(1<<pos)) prati C Programming 0 10-27-2012 05:25 PM
java regex equiv to perl !~ kevinm3574 Java 7 08-03-2005 04:04 PM
perl bug with pos() and placing matches in an array? t13_e@yahoo.com Perl Misc 6 03-17-2005 04:55 AM
Ruby equiv of Perl's HTML::Template module? ChrisO Ruby 8 09-15-2004 10:28 PM
perl pos function equivalent in ruby? John W. Long Ruby 4 04-13-2004 04:01 PM



Advertisments