rindex with array of arrays

Discussion in 'Ruby' started by STEPHEN BECKER I V, Sep 28, 2004.

  1. Should rindex work for an array of arrays? I am starting the playfair
    cypher, you get a key and append the alphabet to it then remove dups
    and change J to I, then make that in to a matrix .

    mat[0].rindex("b") works right
    but not
    mat.rindex("b")
    Do i need a string match type of thing?


    ####code
    def removedup(a) # i want to change a to an array then use uniq
    b=('a'..'z').to_a
    a<<b.to_s
    a.gsub!("j","i")
    a.gsub!(/\s/, '')
    loc=0
    while loc<a.size

    for i in loc+1 .. a.size
    a='' if a[loc]==a
    end
    loc+=1
    end

    return a
    end

    def makematrix(a)
    count = 0
    mx = Array.new(5)
    for i in 0 .. (5 - 1)
    row = Array.new(5, 0)
    for j in 0 .. (5 - 1)
    row[j] = a[count].chr #make a matrix of the letters in a
    count += 1
    end
    mx = row
    end
    return mx
    end

    print "Enter a key: "
    a=gets.downcase

    print removedup(a)
    tmat=makematrix(a)
    print tmat.rindex("x") # will this work?
    ####code
     
    STEPHEN BECKER I V, Sep 28, 2004
    #1
    1. Advertising

  2. Hi --

    On Tue, 28 Sep 2004, STEPHEN BECKER I V wrote:

    > Should rindex work for an array of arrays? I am starting the playfair
    > cypher, you get a key and append the alphabet to it then remove dups
    > and change J to I, then make that in to a matrix .
    >
    > mat[0].rindex("b") works right
    > but not
    > mat.rindex("b")


    If mat is an array of arrays, then none of its elements are "b", so
    there will be no match there.

    > Do i need a string match type of thing?


    Maybe, but have you looked at the Matrix class? I'm not sure, but it
    might have the kind of search mechanism you need.

    > ####code
    > def removedup(a) # i want to change a to an array then use uniq
    > b=('a'..'z').to_a
    > a<<b.to_s
    > a.gsub!("j","i")
    > a.gsub!(/\s/, '')
    > loc=0
    > while loc<a.size
    >
    > for i in loc+1 .. a.size
    > a='' if a[loc]==a
    > end
    > loc+=1
    > end
    >
    > return a
    > end
    >
    > def makematrix(a)
    > count = 0
    > mx = Array.new(5)
    > for i in 0 .. (5 - 1)
    > row = Array.new(5, 0)
    > for j in 0 .. (5 - 1)
    > row[j] = a[count].chr #make a matrix of the letters in a
    > count += 1
    > end
    > mx = row
    > end
    > return mx
    > end
    >
    > print "Enter a key: "
    > a=gets.downcase
    >
    > print removedup(a)
    > tmat=makematrix(a)
    > print tmat.rindex("x") # will this work?
    > ####code


    Slightly shorter version of your code (I think it does the same
    thing), just for fun :)

    def removedup(a)
    (a.split(//).concat(('a'..'z').to_a) - ["j"]).uniq.join
    end

    def makematrix(a)
    a.scan(/.{5}/).map {|row| row.split(//)}
    end

    print "Enter a key: "
    a=gets.downcase.chomp

    r = removedup(a)
    puts r
    tmat=makematrix(r)
    p tmat

    I haven't used Matrix, but again, you might want to look at that too.


    David

    --
    David A. Black
     
    David A. Black, Sep 28, 2004
    #2
    1. Advertising

  3. One day i will program in a ruby way. I am searching the CHM file that
    comes with the windows installer they only thing that has matrix is
    "Math mode (fraction and matrix support is available)." under the IRB.
    I am going to delete the CHM file and just use the
    http://www.ruby-doc.org/stdlib/ pages. But the matrix class does not
    seem to be useful for what i want. It would be good for linear
    algebra. Thank you again for your help.

    Stephen


    On Tue, 28 Sep 2004 23:59:21 +0900, David A. Black <> wrote:
    > Hi --
    >
    > On Tue, 28 Sep 2004, STEPHEN BECKER I V wrote:
    >
    > > Should rindex work for an array of arrays? I am starting the playfair
    > > cypher, you get a key and append the alphabet to it then remove dups
    > > and change J to I, then make that in to a matrix .
    > >
    > > mat[0].rindex("b") works right
    > > but not
    > > mat.rindex("b")

    >
    > If mat is an array of arrays, then none of its elements are "b", so
    > there will be no match there.
    >
    > > Do i need a string match type of thing?

    >
    > Maybe, but have you looked at the Matrix class? I'm not sure, but it
    > might have the kind of search mechanism you need.
    >
    > > ####code
    > > def removedup(a) # i want to change a to an array then use uniq
    > > b=('a'..'z').to_a
    > > a<<b.to_s
    > > a.gsub!("j","i")
    > > a.gsub!(/\s/, '')
    > > loc=0
    > > while loc<a.size
    > >
    > > for i in loc+1 .. a.size
    > > a='' if a[loc]==a
    > > end
    > > loc+=1
    > > end
    > >
    > > return a
    > > end
    > >
    > > def makematrix(a)
    > > count = 0
    > > mx = Array.new(5)
    > > for i in 0 .. (5 - 1)
    > > row = Array.new(5, 0)
    > > for j in 0 .. (5 - 1)
    > > row[j] = a[count].chr #make a matrix of the letters in a
    > > count += 1
    > > end
    > > mx = row
    > > end
    > > return mx
    > > end
    > >
    > > print "Enter a key: "
    > > a=gets.downcase
    > >
    > > print removedup(a)
    > > tmat=makematrix(a)
    > > print tmat.rindex("x") # will this work?
    > > ####code

    >
    > Slightly shorter version of your code (I think it does the same
    > thing), just for fun :)
    >
    > def removedup(a)
    > (a.split(//).concat(('a'..'z').to_a) - ["j"]).uniq.join
    > end
    >
    > def makematrix(a)
    > a.scan(/.{5}/).map {|row| row.split(//)}
    > end
    >
    > print "Enter a key: "
    > a=gets.downcase.chomp
    >
    > r = removedup(a)
    > puts r
    > tmat=makematrix(r)
    > p tmat
    >
    > I haven't used Matrix, but again, you might want to look at that too.
    >
    > David
    >
    > --
    > David A. Black
    >
    >
    >
     
    STEPHEN BECKER I V, Sep 28, 2004
    #3
  4. (a.split(//).concat(('a'..'z').to_a) - ["j"]).uniq.join
    does not take care of white space .
    split(/\s*/) does :) I dont know why but i kept tring slice and was
    over looking split. Thank you again
     
    STEPHEN BECKER I V, Sep 29, 2004
    #4
    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. Philipp
    Replies:
    21
    Views:
    1,140
    Philipp
    Jan 20, 2009
  2. Hadmut Danisch

    Array::index and rindex operator

    Hadmut Danisch, Jul 5, 2004, in forum: Ruby
    Replies:
    22
    Views:
    302
    David A. Black
    Jul 8, 2004
  3. trans.  (T. Onoma)

    rindex with regexp

    trans. (T. Onoma), Dec 28, 2004, in forum: Ruby
    Replies:
    2
    Views:
    130
    trans. (T. Onoma)
    Dec 28, 2004
  4. Cec Tre
    Replies:
    3
    Views:
    196
    Robert Klemme
    Mar 19, 2010
  5. Paul.Lee.1971

    Rindex used to find end of a string

    Paul.Lee.1971, Feb 28, 2007, in forum: Perl Misc
    Replies:
    4
    Views:
    115
    Dr.Ruud
    Mar 11, 2007
Loading...

Share This Page