for loop

Discussion in 'Ruby' started by Martin Durai, Nov 21, 2007.

  1. Martin Durai

    Martin Durai Guest

    Consider the following for loop in 'C' or 'c++' or 'java'

    for (i=namespaceEnd - 1; i >= 0; i--)

    Please help me with code to do the same functionality in ruby
    --
    Posted via http://www.ruby-forum.com/.
    Martin Durai, Nov 21, 2007
    #1
    1. Advertising

  2. Martin Durai

    Carl Guest

    Martin Durai <> writes:

    > Consider the following for loop in 'C' or 'c++' or 'java'
    >
    > for (i=namespaceEnd - 1; i >= 0; i--)
    >
    > Please help me with code to do the same functionality in ruby
    > --
    > Posted via http://www.ruby-forum.com/.


    How about this:

    (namespace_end - 1).downto(0) { |x| puts x }

    Hope that helps,
    Carl.
    Carl, Nov 21, 2007
    #2
    1. Advertising

  3. Martin Durai

    Martin Durai Guest

    Thank you carl, iam very new to this language

    sorry carl could you help with this code fully

    for( int i = namespaceEnd -1; i >= 0; i--) {
    if( prefix.equals( namespacePrefix[ i ] ) ) {
    return namespaceUri[ i ];
    }


    thank you in advance

    --
    Posted via http://www.ruby-forum.com/.
    Martin Durai, Nov 21, 2007
    #3
  4. Martin Durai

    Todd Burch Guest

    Martin Durai wrote:
    > Thank you carl, iam very new to this language
    >
    > sorry carl could you help with this code fully
    >
    > for( int i = namespaceEnd -1; i >= 0; i--) {
    > if( prefix.equals( namespacePrefix[ i ] ) ) {
    > return namespaceUri[ i ];
    > }
    >
    >
    > thank you in advance


    (namespace_end - 1).downto(0) { |x|
    return namespaceUri[x] if prefix==namespacePrefix[x] }

    Todd
    --
    Posted via http://www.ruby-forum.com/.
    Todd Burch, Nov 21, 2007
    #4
  5. Martin Durai

    Carl Guest

    Martin Durai <> writes:

    > Thank you carl, iam very new to this language
    >
    > sorry carl could you help with this code fully
    >
    > for( int i = namespaceEnd -1; i >= 0; i--) {
    > if( prefix.equals( namespacePrefix[ i ] ) ) {
    > return namespaceUri[ i ];
    > }
    >
    >
    > thank you in advance
    >


    I suspect you're looking for the element in the namespaceUri
    array which is at the position determined by looking up the
    position of 'prefix' in the namespacePrefix array (how very
    unsettling). Assuming namespaceEnd is actually the count of
    elements in the namespacePrefix array, would this work?

    your_value = (i = namespace_prefix.index(prefix)) ?
    namespace_uri :
    nil

    Where 'your_value' will now contain the namespace_uri value,
    or nil if it was not found in namespace_prefix

    If i've misunderstood your question, post back with the values
    of prefix, namespacePrefix, namespaceUri and namespaceEnd, and
    what you expect to get out of it and I'll see if I can't help.
    Carl, Nov 21, 2007
    #5
  6. Martin Durai

    Carl Guest

    Todd Burch <> writes:

    > Martin Durai wrote:
    >> Thank you carl, iam very new to this language
    >>
    >> sorry carl could you help with this code fully
    >>
    >> for( int i = namespaceEnd -1; i >= 0; i--) {
    >> if( prefix.equals( namespacePrefix[ i ] ) ) {
    >> return namespaceUri[ i ];
    >> }
    >>
    >>
    >> thank you in advance

    >
    > (namespace_end - 1).downto(0) { |x|
    > return namespaceUri[x] if prefix==namespacePrefix[x] }
    >
    > Todd


    I'm getting a 'LocalJumpError: unexpected return' error when
    I try to return from within a downto block, is this supported
    in your version?

    Carl.
    Carl, Nov 21, 2007
    #6
  7. Martin Durai wrote:
    > Consider the following for loop in 'C' or 'c++' or 'java'
    >
    > for (i=namespaceEnd - 1; i >= 0; i--)
    >
    > Please help me with code to do the same functionality in ruby


    ruby does not really have a for loop as such but there are various ways
    to get the job done. You saw the upto approach. Here is another:


    namespaceEnd.times do
    # your code in here
    end

    now, this works for 0..namespace - 1 automatically. If you need to run
    in reverse, can use the downto or a calculation.

    namespaceEnd.times do |my_var|
    some_array[namespaceEnd - my_var] = some_val
    end

    but that is clunky. If you are iterating through an array you can use
    the each loop:

    my_arr = ["aaa", "bbb", "ccc", "ddd"]
    my_arr.each_with_index {|str, idx| puts "#{idx}. #{str}"}

    =>
    0. aaa
    1. bbb
    2. ccc
    3. ddd

    If you want only the values, use my_arr.each
    If you want only the index position, use my_arr.each_index

    HTH
    --
    Posted via http://www.ruby-forum.com/.
    Lloyd Linklater, Nov 21, 2007
    #7
  8. Martin Durai

    Paul McMahon Guest

    On Wed, 21 Nov 2007 13:39:58 +0900, Martin Durai <=
    > =


    wrote:

    > Thank you carl, iam very new to this language
    >
    > sorry carl could you help with this code fully
    >
    > for( int i =3D namespaceEnd -1; i >=3D 0; i--) {
    > if( prefix.equals( namespacePrefix[ i ] ) ) {
    > return namespaceUri[ i ];
    > }
    >
    >
    > thank you in advance
    >


    You might consider using an array of namespace objects instead of parall=
    el =

    arrays

    class Namespace
    attr_reader :prefix, :uri
    def initialize(prefix, uri)
    @prefix =3D prefix
    @uri =3D uri
    end
    end

    namespaces =3D [ Namespace.new("prefix", "uri"), ...]

    then your code becomes something like:

    matching_namespace =3D namespaces.find {|namespace| namespace.prefix =3D=
    =3D =

    prefix)
    return matching_namespace ? matching_namespace.uri : nil
    Paul McMahon, Nov 21, 2007
    #8
  9. Martin Durai

    Carl Guest

    Paul McMahon <> writes:
    >
    > You might consider using an array of namespace objects instead of parallel
    > arrays
    >
    > class Namespace
    > attr_reader :prefix, :uri
    > def initialize(prefix, uri)
    > @prefix = prefix
    > @uri = uri
    > end
    > end
    >
    > namespaces = [ Namespace.new("prefix", "uri"), ...]
    >
    > then your code becomes something like:
    >
    > matching_namespace = namespaces.find {|namespace| namespace.prefix ==
    > prefix)
    > return matching_namespace ? matching_namespace.uri : nil


    Good point.
    I suspect in this case a plain old Hash might do wonders to simplify
    the problem...

    Carl.
    Carl, Nov 21, 2007
    #9
  10. Martin Durai

    Martin Durai Guest

    Sorry the code is running well in my system. I didnt got any error. I
    have checked the code with my applications.

    --
    Posted via http://www.ruby-forum.com/.
    Martin Durai, Nov 21, 2007
    #10
  11. On 21.11.2007 04:59, Martin Durai wrote:
    > Consider the following for loop in 'C' or 'c++' or 'java'
    >
    > for (i=namespaceEnd - 1; i >= 0; i--)
    >
    > Please help me with code to do the same functionality in ruby


    Did you actually read a tutorial or book about the language? If not,
    it's probably easier to do that vs. trying to cover all these basic
    questions via newsgroup...

    Cheers

    robert
    Robert Klemme, Nov 21, 2007
    #11
  12. "Martin Durai" <> wrote in message
    news:...
    > Consider the following for loop in 'C' or 'c++' or 'java'
    >
    > for (i=namespaceEnd - 1; i >= 0; i--)
    >
    > Please help me with code to do the same functionality in ruby


    I'm surprised no one's suggested the obvious:


    namespace.reverse_each do |i|
    # Do something with i
    end


    ...this assumes you don't actually need an index, which is usually the
    case. It also assumes that namespaceEnd is the end of a container called
    "namespace," or some such...

    If you need the index, instead of the array, you'll have to do something
    wild and zany, like:


    namespace.reverse.each_index do |i|
    # Do something with i
    end


    ...does this help?
    Just Another Victim of the Ambient Morality, Nov 21, 2007
    #12
    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. =?Utf-8?B?VGltOjouLg==?=

    Loop the loop...

    =?Utf-8?B?VGltOjouLg==?=, Feb 16, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,380
    Karl Seguin
    Feb 16, 2005
  2. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,230
    Tim Slattery
    Mar 30, 2005
  3. -
    Replies:
    12
    Views:
    691
    Remon van Vliet
    Jun 15, 2005
  4. Byte
    Replies:
    4
    Views:
    414
  5. Isaac Won
    Replies:
    9
    Views:
    372
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page