3 Replies - 19717 Views - Last Post: 30 July 2007 - 07:40 AM Rate Topic: -----

#1 chris_drappier  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 10-April 07

comparing each element of an array automatically

Posted 26 July 2007 - 10:36 AM

I have this
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:

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

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 :)

Is This A Good Question/Topic? 0
  • +

Replies To: comparing each element of an array automatically

#2 The Neoracle  Icon User is offline

  • Check, check, 1, 2.
  • member icon

Reputation: 21
  • View blog
  • Posts: 4,097
  • Joined: 30-March 01

Re: comparing each element of an array automatically

Posted 29 July 2007 - 10:33 PM

View Postchris_drappier, on 26 Jul, 2007 - 01:36 PM, said:

I have this
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:

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

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 :)



I'd just do:

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.
Was This Post Helpful? 0
  • +
  • -

#3 chris_drappier  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 10-April 07

Re: comparing each element of an array automatically

Posted 30 July 2007 - 07:20 AM

Nevermind DIC, I figured it out myself

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 :)
Was This Post Helpful? 0
  • +
  • -

#4 chris_drappier  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 10-April 07

Re: comparing each element of an array automatically

Posted 30 July 2007 - 07:40 AM

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:

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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1