Welcome to Dream.In.Code
Getting Help is Easy!

Join 108,956 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,541 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



comparing each element of an array automatically

 
Reply to this topicStart new topic

comparing each element of an array automatically

chris_drappier
post 26 Jul, 2007 - 10:36 AM
Post #1


New D.I.C Head

*
Joined: 10 Apr, 2007
Posts: 34


My Contributions


I have this
CODE

s1 = "this is a typing test"
s2 = gets.chomp

s1s = s1.split(//)
s2s = s2.split(//)


I want to compare each element of each array to the corresponding element in the other array like so:

CODE

s1s[0] == s2s[0]
s2s[1] == s2s[1]
s1s[2] == s2s[2]
and so on...


this has to happen automatically though because s1.length will be variable based on the specific string that's in there. I tried to iterate through the array with

CODE

s2s.each{
    |i|
    a = 0
    i == s1s[a]    
    a + 1
}


but all the values return false even if s2 == s1. How can I maek this work?

plz gimme some ruby magic here thx smile.gif
User is offlineProfile CardPM

Go to the top of the page


The Neoracle
post 29 Jul, 2007 - 10:33 PM
Post #2


Check, check, 1, 2.

Group Icon
Joined: 30 Mar, 2001
Posts: 4,069


My Contributions


QUOTE(chris_drappier @ 26 Jul, 2007 - 01:36 PM) *

I have this
CODE

s1 = "this is a typing test"
s2 = gets.chomp

s1s = s1.split(//)
s2s = s2.split(//)


I want to compare each element of each array to the corresponding element in the other array like so:

CODE

s1s[0] == s2s[0]
s2s[1] == s2s[1]
s1s[2] == s2s[2]
and so on...


this has to happen automatically though because s1.length will be variable based on the specific string that's in there. I tried to iterate through the array with

CODE

s2s.each{
    |i|
    a = 0
    i == s1s[a]    
    a + 1
}


but all the values return false even if s2 == s1. How can I maek this work?

plz gimme some ruby magic here thx smile.gif



I'd just do:

CODE

foo = s1s & s2s  (& creates an array from the matching values)
foo.length


I don't really get what your trying to do.

Maybe if you expand what on how your using this, it would make more sense.
User is offlineProfile CardPM

Go to the top of the page

chris_drappier
post 30 Jul, 2007 - 07:20 AM
Post #3


New D.I.C Head

*
Joined: 10 Apr, 2007
Posts: 34


My Contributions


Nevermind DIC, I figured it out myself

CODE

s1 = "this is a typing test"
s2 = "this is a typing test"
s1s = s1.split(//)
s2s = s2.split(//)
a = 0
s2s.each{
    |i|
    puts i == s1s[a]
    a += 1
}


this compares each element of s1s to the corresponding element of s2s for a true or false value. Thx4lookin though smile.gif
User is offlineProfile CardPM

Go to the top of the page

chris_drappier
post 30 Jul, 2007 - 07:40 AM
Post #4


New D.I.C Head

*
Joined: 10 Apr, 2007
Posts: 34


My Contributions


It's a typing test, and I this is the part that checks for correctness. the very first part. it just checks to see if each letter is the same as the corresponding letter. here's the test a little cleaned up:

CODE

s1 = "this is a typing test"
puts s1
s2 = gets.chomp
s1s = s1.split(//)
s2s = s2.split(//)
a = 0
if s1s ==s2s
    print "100% correct!\n"
else
    s2s.each{
        |i|
        b = i == s1s[a]
        puts b
        a += 1
    }
end

copypaste this to a file and run it on your command line, you'll see what it does
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 9/4/08 09:24PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month