# What am I doing wrong! Arrays

**URL:** <https://rubytalk.org/t/what-am-i-doing-wrong-arrays/47412>\
**Category:** ruby-talk\
**Created:** [30 June 2008 05:09 UTC](https://rubytalk.org/t/what-am-i-doing-wrong-arrays/47412 "2008-06-30T05:09:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![12345678\_123456789](https://avatars.discourse-cdn.com/v4/letter/1/3ec8ea/32.png) [@12345678\_123456789](https://rubytalk.org/u/12345678_123456789)\
**Post date:** [30 June 2008 05:09 UTC](https://rubytalk.org/t/what-am-i-doing-wrong-arrays/47412/1 "2008-06-30T05:09:04Z")

</div>

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

&nbsp;&nbsp;array = title.split

&nbsp;&nbsp;if(array.length == "12")  
&nbsp;&nbsp;&nbsp;&nbsp;puts array[0]  
&nbsp;&nbsp;elsif(array.length == "13")  
&nbsp;&nbsp;&nbsp;&nbsp;puts array[1].to\_s + " " + array[2].to\_s  
&nbsp;&nbsp;elsif(array.length == "14")  
&nbsp;&nbsp;&nbsp;&nbsp;puts array[1].to\_s + " " + array[2].to\_s + " " + array[3].to\_s  
&nbsp;&nbsp;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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Daniel\_Sheppard](https://avatars.discourse-cdn.com/v4/letter/d/db5fbb/32.png) [@Daniel\_Sheppard](https://rubytalk.org/u/Daniel_Sheppard)\
**Post date:** [30 June 2008 05:11 UTC](https://rubytalk.org/t/what-am-i-doing-wrong-arrays/47412/2 "2008-06-30T05:11:47Z")

</div>

> &nbsp;&nbsp;title = "word word1 word2 word3 word4 word5 word6 word7 word8 word9  
> word10 word11 word12 word13"
> 
> &nbsp;&nbsp;array = title.split
> 
> &nbsp;&nbsp;if(array.length == "12")  
> &nbsp;&nbsp;&nbsp;&nbsp;puts array[0]  
> &nbsp;&nbsp;elsif(array.length == "13")  
> &nbsp;&nbsp;&nbsp;&nbsp;puts array[1].to\_s + " " + array[2].to\_s  
> &nbsp;&nbsp;elsif(array.length == "14")  
> &nbsp;&nbsp;&nbsp;&nbsp;puts array[1].to\_s + " " + array[2].to\_s + " " + array[3].to\_s  
> &nbsp;&nbsp;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:** ![krusty.ar](https://avatars.discourse-cdn.com/v4/letter/k/f14d63/32.png) [@krusty.ar](https://rubytalk.org/u/krusty.ar)\
**Post date:** [30 June 2008 12:17 UTC](https://rubytalk.org/t/what-am-i-doing-wrong-arrays/47412/3 "2008-06-30T12:17:17Z")

</div>

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

Lucas.

> **···**
>
> On Jun 30, 2:09 am, Tj Superfly \<nonstickg...@verizon.net\> wrote:
> 
> > 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?

---

<div class="post-metadata">

**Author:** ![12345678\_123456789](https://avatars.discourse-cdn.com/v4/letter/1/3ec8ea/32.png) [@12345678\_123456789](https://rubytalk.org/u/12345678_123456789)\
**Post date:** [30 June 2008 05:14 UTC](https://rubytalk.org/t/what-am-i-doing-wrong-arrays/47412/4 "2008-06-30T05:14:57Z")

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Thomas\_Peklak](https://avatars.discourse-cdn.com/v4/letter/t/ecc23a/32.png) [@Thomas\_Peklak](https://rubytalk.org/u/Thomas_Peklak)\
**Post date:** [30 June 2008 06:27 UTC](https://rubytalk.org/t/what-am-i-doing-wrong-arrays/47412/5 "2008-06-30T06:27:19Z")

</div>

Just do it this way and it should work:

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

---

<div class="post-metadata">

**Author:** ![Sebastian\_Hungereck1](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastian\_Hungereck1](https://rubytalk.org/u/Sebastian_Hungereck1)\
**Post date:** [30 June 2008 08:27 UTC](https://rubytalk.org/t/what-am-i-doing-wrong-arrays/47412/6 "2008-06-30T08:27:45Z")

</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

> **···**
>
> --  
> Jabber: sepp2k@jabber.org  
> ICQ: 205544826

---

<div class="post-metadata">

**Author:** ![Florian\_Gilcher](https://avatars.discourse-cdn.com/v4/letter/f/57b2e6/32.png) [@Florian\_Gilcher](https://rubytalk.org/u/Florian_Gilcher)\
**Post date:** [30 June 2008 13:25 UTC](https://rubytalk.org/t/what-am-i-doing-wrong-arrays/47412/7 "2008-06-30T13:25:33Z")

</div>

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 Gilcher

> **···**
>
> On Jun 30, 2008, at 10:27 AM, Sebastian Hungerecker wrote:
> 
> > 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  
> > --  
> > Jabber: sepp2k@jabber.org  
> > ICQ: 205544826

---

<div class="post-metadata">

**Author:** ![Dave\_Bass](https://avatars.discourse-cdn.com/v4/letter/d/f6c823/32.png) [@Dave\_Bass](https://rubytalk.org/u/Dave_Bass)\
**Post date:** [30 June 2008 21:49 UTC](https://rubytalk.org/t/what-am-i-doing-wrong-arrays/47412/8 "2008-06-30T21:49:12Z")

</div>

Florian Gilcher 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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
