ruby equiv of perl pos

Discussion in 'Ruby' started by snacktime, Aug 12, 2006.

  1. snacktime

    snacktime Guest

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

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

  2. snacktime

    gga Guest

    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
     
    gga, Aug 12, 2006
    #2
    1. Advertising

  3. $ 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
     
    James Edward Gray II, Aug 12, 2006
    #3
  4. 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
     
    Robin Stocker, Aug 12, 2006
    #4
  5. snacktime

    gga Guest


    > 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 }
    }
     
    gga, Aug 12, 2006
    #5
  6. 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
     
    Robin Stocker, Aug 13, 2006
    #6
  7. snacktime

    snacktime Guest

    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.
     
    snacktime, Aug 13, 2006
    #7
  8. 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
     
    Logan Capaldo, Aug 13, 2006
    #8
  9. snacktime

    gga Guest

    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, Aug 13, 2006
    #9
  10. snacktime

    gga Guest

    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.
     
    gga, Aug 13, 2006
    #10
  11. 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
     
    Logan Capaldo, Aug 13, 2006
    #11
  12. snacktime

    gga Guest

    Logan Capaldo ha escrito:

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


    That should work okay (well, it should be new_string << ..., thou).
    I'll admit I was unaware of MatchData's offset method. That's cool.
    Using string.scan() should be only slightly slower than the
    while/index() loop do to the inherent yield in scan {}.
    If you are comming from Perl, I would also recommend to stick with
    Regexp.last_match instead of $~ for clarity. I did 5+ years of Perl
    but I'll be damned if I remember what all the $symbol vars stand for
    without looking them up.
     
    gga, Aug 14, 2006
    #12
  13. Re: [good] Re: ruby equiv of perl pos

    On Mon, 14 Aug 2006 14:20:10 +0900, you wrote:

    >Logan Capaldo ha escrito:
    >
    >> How about:
    >>
    >> new_string =3D ""
    >> string.scan(/\0/) do |m|
    >> new_string =3D "%o" % $~.offset(0).first
    >> end

    >
    >That should work okay (well, it should be new_string << ..., thou).
    >I'll admit I was unaware of MatchData's offset method. That's cool.
    >Using string.scan() should be only slightly slower than the
    >while/index() loop do to the inherent yield in scan {}.
    >If you are comming from Perl, I would also recommend to stick with
    >Regexp.last_match instead of $~ for clarity. I did 5+ years of Perl
    >but I'll be damned if I remember what all the $symbol vars stand for
    >without looking them up.
    >


    http://home.cogeco.ca/~tsummerfelt1
    ROVING SWARM: http://groups.yahoo.com/group/roving_swarm/
    telnet://ventedspleen.dyndns.org
     
    tony summerfelt, Aug 14, 2006
    #13
  14. tony summerfelt, Aug 14, 2006
    #14
  15. =20
    tony summerfelt wrote:
    > i noticed that all the ruby solutions presented were longer than the
    > original perl code. i found the perl code more readable... which is
    > unusual for most perl-ruby comparisons :)


    Well, I wouldn't consider it unusual for the code to be longer when you
    are trying to duplicate the functionality of a keyword of another
    language that doesn't exist in Ruby!

    Perhaps if String#scan made MatchData objects available as requested
    here: http://www.rcrchive.net/rcr/show/276 the Ruby solution would be
    cleaner.

    - Mark.
     
    Thomas, Mark - BLS CTR, Aug 17, 2006
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kevinm3574

    java regex equiv to perl !~

    kevinm3574, Aug 2, 2005, in forum: Java
    Replies:
    7
    Views:
    1,288
    kevinm3574
    Aug 3, 2005
  2. John W. Long

    perl pos function equivalent in ruby?

    John W. Long, Apr 8, 2004, in forum: Ruby
    Replies:
    4
    Views:
    189
    Robert Klemme
    Apr 13, 2004
  3. ChrisO
    Replies:
    8
    Views:
    171
    ChrisO
    Sep 15, 2004
  4. Replies:
    6
    Views:
    133
    Fabian Pilkowski
    Mar 17, 2005
  5. prati
    Replies:
    0
    Views:
    501
    prati
    Oct 27, 2012
Loading...

Share This Page