# What am I doing wrong! Arrays

**URL:** <https://www.ruby-forum.com/t/what-am-i-doing-wrong-arrays/141730>\
**Category:** Ruby\
**Created:** [June 30, 2008, 7:11am UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong-arrays/141730 "2008-06-30T07:11:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![superfly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@superfly](https://www.ruby-forum.com/u/superfly)\
**Post date:** [June 30, 2008, 7:11am UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong-arrays/141730/1 "2008-06-30T07:11:46Z")

</div>

title = “word word1 word2 word3 word4 word5 word6 word7 word8 word9  
word10 word11 word12 word13”

array = title.split

if(array.length == “12”)  
puts array[0]  
elsif(array.length == “13”)  
puts array[1].to\_s + " " + array[2].to\_s  
elsif(array.length == “14”)  
puts array[1].to\_s + " " + array[2].to\_s + " " + array[3].to\_s  
end

My output should be “word1 word2” but I’m getting nothing.

What am I overlooking? I’ve tried everything I know, can anyone help?

Regards,

TJ

---

<div class="post-metadata">

**Author:** ![superfly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@superfly](https://www.ruby-forum.com/u/superfly)\
**Post date:** [June 30, 2008, 7:15am UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong-arrays/141730/2 "2008-06-30T07:15:12Z")

</div>

> ```
> puts array[1].to_s + " " + array[2].to_s + " " + array[3].to_s
> 
> ```
> 
> end
> 
> My output should be “word1 word2” but I’m getting nothing.
> 
> What am I overlooking? I’ve tried everything I know, can anyone help?

12 == “12” is going to be false.

Dan.

---

<div class="post-metadata">

**Author:** ![superfly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@superfly](https://www.ruby-forum.com/u/superfly)\
**Post date:** [June 30, 2008, 7:17am UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong-arrays/141730/3 "2008-06-30T07:17:40Z")

</div>

I thought of that too, but when I added the line:

twelve = “12” and then changed it to array.length == twelve it does the  
same thing.

I’m still really confused at such a simple task. D:

---

<div class="post-metadata">

**Author:** ![superfly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@superfly](https://www.ruby-forum.com/u/superfly)\
**Post date:** [June 30, 2008, 8:30am UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong-arrays/141730/4 "2008-06-30T08:30:40Z")

</div>

Just do it this way and it should work:

if(array.length == 12)  
puts array[0]  
elsif(array.length == 13)  
puts array[1].to\_s + " " + array[2].to\_s  
elsif(array.length == 14)  
puts array[1].to\_s + " " + array[2].to\_s + " " + array[3].to\_s  
end

---

<div class="post-metadata">

**Author:** ![superfly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@superfly](https://www.ruby-forum.com/u/superfly)\
**Post date:** [June 30, 2008, 10:31am UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong-arrays/141730/5 "2008-06-30T10:31:20Z")

</div>

Tj Superfly wrote:

> twelve = “12” and then changed it to array.length == twelve it does the  
> same thing.

You’re still compating array.length to “12”, but Array#length returns  
an integer, not a string. “12” is a string. array.length will return  
12 and as Daniel said 12 == “12” is false. Whether you assign “12” to  
a variable first or not.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![superfly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@superfly](https://www.ruby-forum.com/u/superfly)\
**Post date:** [June 30, 2008, 3:08pm UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong-arrays/141730/6 "2008-06-30T15:08:16Z")

</div>

On Jun 30, 2:09 am, Tj Superfly [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> puts array[1].to_s + " " + array[2].to_s + " " + array[3].to_s
> 
> ```
> 
> end
> 
> My output should be “word1 word2” but I’m getting nothing.
> 
> What am I overlooking? I’ve tried everything I know, can anyone help?

The previous mails have nailed the problem, but your array length is  
14, so the output should be “wiord1 word2 word3”

Lucas.

---

<div class="post-metadata">

**Author:** ![superfly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@superfly](https://www.ruby-forum.com/u/superfly)\
**Post date:** [June 30, 2008, 11:51pm UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong-arrays/141730/7 "2008-06-30T23:51:59Z")

</div>

Florian G. wrote:

> That a typical PHP behaviour, because PHP casts Strings to Ints before  
> comparing.

Similar thing with Perl. Perl scalars can be numbers or strings,  
depending on the context. Sometimes you have to force them to be one or  
the other; e.g. adding 0 to a scalar makes it more number-like;  
appending an empty string “” makes it more string-like. But generally it  
behaves in a sensible way.

Comparing a string to an integer is a typical gotcha for us ex-Perlers.

Dave

---

<div class="post-metadata">

**Author:** ![superfly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@superfly](https://www.ruby-forum.com/u/superfly)\
**Post date:** [June 30, 2008, 3:28pm UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong-arrays/141730/8 "2008-06-30T15:28:50Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Are you a PHP programmer by trade?

That a typical PHP behaviour, because PHP casts Strings to Ints before  
comparing. This can be confusing. (Although they sell it as “good  
usability”) Ruby does not know implicit casts and thus 11 != “11”.  
Ruby is not as lax as PHP when it comes to types.

(Fun fact. Did you know that 11 == “11abcde”? Ask PHP. Second FF: Did  
you know that a variable containing 0 or “0” is empty()?)

Regards,  
Florian G.

On Jun 30, 2008, at 10:27 AM, Sebastian H. wrote:

> ## HTH, Sebastian
> 
> Jabber: removed\_email\_address@domain.invalid  
> ICQ: 205544826

-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.8 (Darwin)

iEYEARECAAYFAkho32gACgkQJA/zY0IIRZYvewCeLtepLFf1XWzb9tQMsNFWozzR  
g6oAoLGQhz4CyFJfmN1VWBRdeBlSZlFx  
=zsUH  
-----END PGP SIGNATURE-----
