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

Join 86,382 Programmers. There are 1,371 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

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



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
+Quote Post


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


Check, check, 1, 2.

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

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
+Quote Post

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


New D.I.C Head

*
Joined: 10 Apr, 2007
Posts: 34

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
+Quote Post

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


New D.I.C Head

*
Joined: 10 Apr, 2007
Posts: 34

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
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 5/17/08 03:39AM

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