Can You Store Individual Letters?

  • Thread starter Scott Andrechek
  • Start date
S

Scott Andrechek

I was wandering if it is possible to have user inputted words stored and
each letter as a seperate variable. I know i could just have each letter
inputted indvidually but thats time consuming on the users part. Here's
an example in case you don't know what i mean:
Ex.
USER: Tomato
COMP: Type a number to have a letter displayed
USER: 2
COMP: o
USER: 5
COMP: t


This is not the type of program I am looking to make, it's just an
example.


Thanks,
Scott
 
T

Tim Hunter

Scott said:
I was wandering if it is possible to have user inputted words stored and
each letter as a seperate variable. I know i could just have each letter
inputted indvidually but thats time consuming on the users part. Here's
an example in case you don't know what i mean:
Ex.
USER: Tomato
COMP: Type a number to have a letter displayed
USER: 2
COMP: o
USER: 5
COMP: t

Check out String#split

irb(main):005:0> x = "tomato"
=> "tomato"
irb(main):006:0> y = x.split ''
=> ["t", "o", "m", "a", "t", "o"]
irb(main):007:0> y[0]
=> "t"
irb(main):008:0> y[5]
=> "o"
 
R

Rob Biedenharn

Scott said:
I was wandering if it is possible to have user inputted words
stored and
each letter as a seperate variable. I know i could just have each
letter
inputted indvidually but thats time consuming on the users part.
Here's
an example in case you don't know what i mean:
Ex.
USER: Tomato
COMP: Type a number to have a letter displayed
USER: 2
COMP: o
USER: 5
COMP: t

Check out String#split

irb(main):005:0> x = "tomato"
=> "tomato"
irb(main):006:0> y = x.split ''
=> ["t", "o", "m", "a", "t", "o"]
irb(main):007:0> y[0]
=> "t"
irb(main):008:0> y[5]
=> "o"


And if you unshift a value (like nil) into the first element, then you
can use 1-based positions:

irb> word = "Tomato".split('')
=> ["T", "o", "m", "a", "t", "o"]
irb> word.unshift nil
=> [nil, "T", "o", "m", "a", "t", "o"]
irb> word[2]
=> "o"
irb> word[5]
=> "t"


-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top