# A newbie question about scanf

**URL:** <https://www.ruby-forum.com/t/a-newbie-question-about-scanf/95433>\
**Category:** Ruby\
**Created:** [June 1, 2006, 1:34am UTC](https://www.ruby-forum.com/t/a-newbie-question-about-scanf/95433 "2006-06-01T01:34:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kousen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e19b73/32.png) [@kousen](https://www.ruby-forum.com/u/kousen)\
**Post date:** [June 1, 2006, 1:34am UTC](https://www.ruby-forum.com/t/a-newbie-question-about-scanf/95433/1 "2006-06-01T01:34:21Z")

</div>

I tried to get user input by using scanf

a = scanf(’%d’)

that works, it’s all very nice, until i do this

while a \< 100  
and a is an array, so i can’t do that.

So i did this  
while a[0] \< 100…

which works… but is there a better way of doing it?

thanks in advance!

---

<div class="post-metadata">

**Author:** ![kousen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e19b73/32.png) [@kousen](https://www.ruby-forum.com/u/kousen)\
**Post date:** [June 1, 2006, 2:18am UTC](https://www.ruby-forum.com/t/a-newbie-question-about-scanf/95433/2 "2006-06-01T02:18:02Z")

</div>

Ken K. wrote:

> while a[0] \< 100…
> 
> which works… but is there a better way of doing it?

These should all work:

a, = scanf(’%d’)  
a,\* = scanf(’%d’) # Prettier  
a = \*scanf(’%d’) # Clearer?

However, if you really just want to convert a single  
string to an integer, use str.to\_i or Integer(str).

Hal

---

<div class="post-metadata">

**Author:** ![kousen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e19b73/32.png) [@kousen](https://www.ruby-forum.com/u/kousen)\
**Post date:** [June 1, 2006, 2:29am UTC](https://www.ruby-forum.com/t/a-newbie-question-about-scanf/95433/3 "2006-06-01T02:29:40Z")

</div>

Hal F. wrote:

> Ken K. wrote:
> 
> > while a[0] \< 100…
> > 
> > which works… but is there a better way of doing it?
> 
> These should all work:
> 
> a, = scanf(’%d’)  
> a,\* = scanf(’%d’) # Prettier  
> a = \*scanf(’%d’) # Clearer?
> 
> However, if you really just want to convert a single  
> string to an integer, use str.to\_i or Integer(str).
> 
> Hal

Hmmm… the \* thing…  
is it possible if you explain that in a bit more detail? Is that like  
pointers in C?

---

<div class="post-metadata">

**Author:** ![kousen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e19b73/32.png) [@kousen](https://www.ruby-forum.com/u/kousen)\
**Post date:** [June 1, 2006, 4:17am UTC](https://www.ruby-forum.com/t/a-newbie-question-about-scanf/95433/4 "2006-06-01T04:17:09Z")

</div>

As a fellow newbie, let me just say that this splat business is all very  
interesting, but I get cornfused one step earlier. You all seem to  
think it is quite ordinary that this scanf method returns an array.  
What’s up with that? If it gathers one line of input, why then, 'o why  
doesn’t it return a string object???

thanks,  
jp

Hal F. wrote:

> Ken K. wrote:
> 
> > is it possible if you explain that in a bit more detail? Is that like  
> > pointers in C?
> 
> No… it’s what David Alan Black calls the “unary unarray” operator,  
> also called the “splat” operator.
> 
> - on an array turns it into a simple list (sort of like removing  
> the brackets).
> 
> Actually… I think I misled you on the third example.  
> I don’t think it works. You’d still need more than one  
> item on the left side if you used it.
> 
> On the other side, it’s different.
> 
> ```
> x,* = array # means the same as
> x, = array # but with the * it looks better
> 
> ```
> 
> x gets assigned the first value.
> 
> There’s also:
> 
> a = scanf(’%d’)[0] # or  
> a = scanf(’%d’).first
> 
> Hal

---

<div class="post-metadata">

**Author:** ![kousen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e19b73/32.png) [@kousen](https://www.ruby-forum.com/u/kousen)\
**Post date:** [June 1, 2006, 2:51am UTC](https://www.ruby-forum.com/t/a-newbie-question-about-scanf/95433/5 "2006-06-01T02:51:17Z")

</div>

Ken K. wrote:

> is it possible if you explain that in a bit more detail? Is that like  
> pointers in C?

No… it’s what David Alan Black calls the “unary unarray” operator,  
also called the “splat” operator.

- on an array turns it into a simple list (sort of like removing  
the brackets).

Actually… I think I misled you on the third example.  
I don’t think it works. You’d still need more than one  
item on the left side if you used it.

On the other side, it’s different.

```
x,* = array # means the same as
x, = array # but with the * it looks better

```

x gets assigned the first value.

There’s also:

a = scanf(’%d’)[0] # or  
a = scanf(’%d’).first

Hal

---

<div class="post-metadata">

**Author:** ![kousen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e19b73/32.png) [@kousen](https://www.ruby-forum.com/u/kousen)\
**Post date:** [June 1, 2006, 5:35am UTC](https://www.ruby-forum.com/t/a-newbie-question-about-scanf/95433/6 "2006-06-01T05:35:04Z")

</div>

OH! Now I remember. Haven’t used it in forever. It uses a format  
string just like printf. The returned array would be it’s best attempt  
to pick out all of the pieces that the format string asked for. Makes  
sense now.

thanks,  
jp

Hal F. wrote:

> Jeff P. wrote:
> 
> > As a fellow newbie, let me just say that this splat business is all very  
> > interesting, but I get cornfused one step earlier. You all seem to  
> > think it is quite ordinary that this scanf method returns an array.  
> > What’s up with that? If it gathers one line of input, why then, 'o why  
> > doesn’t it return a string object???
> 
> scanf typically returns multiple objects – it reads a string and  
> returns strings, integers, floats, whatever.
> 
> The case where you’re only reading one item is the oddball  
> case. I guess it would be possible to just “return the item” –  
> but it’s more natural to return an array always.
> 
> Hal

---

<div class="post-metadata">

**Author:** ![kousen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e19b73/32.png) [@kousen](https://www.ruby-forum.com/u/kousen)\
**Post date:** [June 1, 2006, 5:00am UTC](https://www.ruby-forum.com/t/a-newbie-question-about-scanf/95433/7 "2006-06-01T05:00:28Z")

</div>

Jeff P. wrote:

> As a fellow newbie, let me just say that this splat business is all very  
> interesting, but I get cornfused one step earlier. You all seem to  
> think it is quite ordinary that this scanf method returns an array.  
> What’s up with that? If it gathers one line of input, why then, 'o why  
> doesn’t it return a string object???

scanf typically returns multiple objects – it reads a string and  
returns strings, integers, floats, whatever.

The case where you’re only reading one item is the oddball  
case. I guess it would be possible to just “return the item” –  
but it’s more natural to return an array always.

Hal
