[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Randomly Sorting Strings

woodyee

2/11/2007 7:23:00 AM

Hi! I'm trying to write a program that will print out a different
string each time it's run from a list of strings.

For example:

example = ['one', 'two', 'three']
# sort them somehow
# 'two' prints out as the output, for example
# run it again and a different string prints out

Make sense? I've tried srand & rand but can't do this with strings. Do
I need to forget the arrays and try something else? I've looked at the
Pickaxe book and Ruby Cookbook but can't find anything. Thanks!

5 Answers

David and Sharon Phillips

2/11/2007 7:47:00 AM

0

try

example= %w[one two three cat dog cow table]
puts example[rand(example.size)]

Cheers,
Dave

On 11/02/2007, at 6:25 PM, woodyee wrote:

> Hi! I'm trying to write a program that will print out a different
> string each time it's run from a list of strings.
>
> For example:
>
> example = ['one', 'two', 'three']
> # sort them somehow
> # 'two' prints out as the output, for example
> # run it again and a different string prints out
>
> Make sense? I've tried srand & rand but can't do this with strings. Do
> I need to forget the arrays and try something else? I've looked at the
> Pickaxe book and Ruby Cookbook but can't find anything. Thanks!
>
>


Stefan Rusterholz

2/11/2007 8:09:00 AM

0

Sharon Phillips wrote:
> try
>
> example= %w[one two three cat dog cow table]
> puts example[rand(example.size)]
>
> Cheers,
> Dave

Another example would be:

example= %w[one two three cat dog cow table].sort_by { rand }
example.each { |string|
puts string
}

that will print all in random order

my regards

--
Posted via http://www.ruby-....

Stefan Rusterholz

2/11/2007 11:15:00 AM

0

Robert Dober wrote:
> On 2/11/07, Stefan Rusterholz <apeiros@gmx.net> wrote:
>> Another example would be:
>> --
>> Posted via http://www.ruby-....
>>
>> This is not really what you want to do but I cannot post more for now,
> this is a Ruby Quiz Spoiler!!!
> Robert

I apologize. I didn't read the current quiz.
Does that mean I have to check back with the current quiz when somebody
asks for help in here?

my regards

--
Posted via http://www.ruby-....

dblack

2/11/2007 12:57:00 PM

0

James Gray

2/11/2007 7:54:00 PM

0

On Feb 11, 2007, at 6:59 AM, Robert Dober wrote:

> I personally chose to try to avoid to post any material too much
> related to
> the Quiz and furthermore I point it out to others who might not be
> interested in the Quiz.

Overlap happens. No big deal. It just means I chose well for the
quiz topic and it will be helpful in the future. ;)

James Edward Gray II