Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #4203 > unrolled thread
| Started by | Kevin <darkintent@gmail.com> |
|---|---|
| First post | 2011-05-10 15:56 -0500 |
| Last post | 2011-05-16 21:47 -0500 |
| Articles | 8 — 5 participants |
Back to article view | Back to comp.lang.ruby
Generate random string matching specific pattern and length Kevin <darkintent@gmail.com> - 2011-05-10 15:56 -0500
Re: Generate random string matching specific pattern and length Adam Prescott <adam@aprescott.com> - 2011-05-10 16:12 -0500
Re: Generate random string matching specific pattern and length 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-10 16:20 -0500
Re: Generate random string matching specific pattern and length Adam Prescott <adam@aprescott.com> - 2011-05-10 16:24 -0500
Re: Generate random string matching specific pattern and length Adam Prescott <adam@aprescott.com> - 2011-05-10 20:41 -0500
Re: Generate random string matching specific pattern and length Robert Klemme <shortcutter@googlemail.com> - 2011-05-11 02:11 -0500
Re: Generate random string matching specific pattern and length Chris Hulan <chris.hulan@gmail.com> - 2011-05-11 07:50 -0700
Re: Generate random string matching specific pattern and length Kevin <darkintent@gmail.com> - 2011-05-16 21:47 -0500
| From | Kevin <darkintent@gmail.com> |
|---|---|
| Date | 2011-05-10 15:56 -0500 |
| Subject | Generate random string matching specific pattern and length |
| Message-ID | <BANLkTimBHzkQmhhBAb7EEX7WjXc+xzfURQ@mail.gmail.com> |
[Note: parts of this message were removed to make it a legal post.]
I'm trying to generate a random set of strings to fill a database with that
match the following pattern: /^([A-Z]|[a-z]|-)+$/
The array I'm trying to create must contain strings that look like
"A12-34-4567" and be exactly ten characters long I have seen code on the
internet for generating random strings but I don't have internet at home at
the moment to really study examples like the one here:
http://coderrr.wordpress.com/2008/10/28/ruby-golf-generating-random-strings
I'm guessing that something like map { if /^([A-Z]|[a-z]|-)+$/ &
char.len==10
strings<< current_matching_value
}
would achieve the right solution. Any pointers would be greatly
appreciated.
[toc] | [next] | [standalone]
| From | Adam Prescott <adam@aprescott.com> |
|---|---|
| Date | 2011-05-10 16:12 -0500 |
| Message-ID | <BANLkTinoFJhRF8zq_AAaPkUx5OHKyakcGA@mail.gmail.com> |
| In reply to | #4203 |
[Note: parts of this message were removed to make it a legal post.]
On Tue, May 10, 2011 at 9:56 PM, Kevin <darkintent@gmail.com> wrote:
> I'm trying to generate a random set of strings to fill a database with that
> match the following pattern: /^([A-Z]|[a-z]|-)+$/
>
> The array I'm trying to create must contain strings that look like
> "A12-34-4567" and be exactly ten characters long
How's this?
characters = ("A".."Z").to_a + ("a".."z").to_a # there is a shortcut to this
in 1.9, but it escapes me.
characters << "-"
10.times.map { a << b.sample }
Sure there's a quicker way.
By the way, /[A-Z|[a-z]|-/ is equivalent to /[A-Za-z-]/.
[toc] | [prev] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-05-10 16:20 -0500 |
| Message-ID | <ff11e0b873337727b724da400a5e5354@ruby-forum.com> |
| In reply to | #4205 |
Adam Prescott wrote in post #997841:
> On Tue, May 10, 2011 at 9:56 PM, Kevin <darkintent@gmail.com> wrote:
>
>> I'm trying to generate a random set of strings to fill a database with that
>> match the following pattern: /^([A-Z]|[a-z]|-)+$/
>>
>> The array I'm trying to create must contain strings that look like
>> "A12-34-4567" and be exactly ten characters long
>
>
> How's this?
>
> characters = ("A".."Z").to_a + ("a".."z").to_a # there is a shortcut to
> this
> in 1.9, but it escapes me.
> characters << "-"
>
> 10.times.map { a << b.sample }
>
> Sure there's a quicker way.
>
> By the way, /[A-Z|[a-z]|-/ is equivalent to /[A-Za-z-]/.
Of course with all your misnamed/non-existent variables, that code won't
run. Atrocious.
--
Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | Adam Prescott <adam@aprescott.com> |
|---|---|
| Date | 2011-05-10 16:24 -0500 |
| Message-ID | <BANLkTikcMR94qLAezPjbPv9dwXF8Wmj-hw@mail.gmail.com> |
| In reply to | #4207 |
[Note: parts of this message were removed to make it a legal post.] On Tue, May 10, 2011 at 10:20 PM, 7stud -- <bbxx789_05ss@yahoo.com> wrote: > Of course with all your misnamed/non-existent variables, that code won't > run. Atrocious. > Thanks for that. I'd renamed them between checking it in irb and paste. Badly, as you politely noted. It's been a long day.
[toc] | [prev] | [next] | [standalone]
| From | Adam Prescott <adam@aprescott.com> |
|---|---|
| Date | 2011-05-10 20:41 -0500 |
| Message-ID | <BANLkTimFnGhsk1-L=_Wz-R6vQBMd3qJGHA@mail.gmail.com> |
| In reply to | #4205 |
[Note: parts of this message were removed to make it a legal post.]
On Tue, May 10, 2011 at 10:12 PM, Adam Prescott <adam@aprescott.com> wrote:
> characters = ("A".."Z").to_a + ("a".."z").to_a # there is a shortcut to
> this in 1.9, but it escapes me.
> characters << "-"
>
> 10.times.map { a << b.sample }
>
To rectify this abomination, it comes from two ways:
a = []
# this was the b:
characters = ("A".."Z").to_a + ("a".."z").to_a
characters << "-"
10.times { a << characters.sample }
a.join("")
or
characters = ("A".."Z").to_a + ("a".."z").to_a
characters << "-"
10.times.map { characters.sample }.join("")
[toc] | [prev] | [next] | [standalone]
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Date | 2011-05-11 02:11 -0500 |
| Message-ID | <BANLkTinmSVicq3SRWTvq=+Qzwj8LYD8H8A@mail.gmail.com> |
| In reply to | #4203 |
On Tue, May 10, 2011 at 10:56 PM, Kevin <darkintent@gmail.com> wrote:
> I'm trying to generate a random set of strings to fill a database with that
> match the following pattern: /^([A-Z]|[a-z]|-)+$/
>
> The array I'm trying to create must contain strings that look like
> "A12-34-4567" and be exactly ten characters long I have seen code on the
That's 11 characters btw.
> internet for generating random strings but I don't have internet at home at
> the moment to really study examples like the one here:
> http://coderrr.wordpress.com/2008/10/28/ruby-golf-generating-random-strings
Just a hint: that does not work on 1.9 because ?a does not return a
Fixnum any more.
> I'm guessing that something like map { if /^([A-Z]|[a-z]|-)+$/ &
> char.len==10
> strings<< current_matching_value
> }
>
> would achieve the right solution. Any pointers would be greatly
> appreciated.
Often there is not _the_ right solution (-> TIMTOWTDI). So far nobody
seems to have suggested good old sprintf:
BASE = 'A'.getbyte 0
DELTA = 'Z'.getbyte(0) - BASE
10.times do
s = sprintf '%s%02d-%02d-%04d',
(BASE + rand(DELTA)).chr,
rand(100),
rand(100),
rand(10000)
puts s, s.length
end
Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
[toc] | [prev] | [next] | [standalone]
| From | Chris Hulan <chris.hulan@gmail.com> |
|---|---|
| Date | 2011-05-11 07:50 -0700 |
| Message-ID | <b907ce51-50fd-45ff-aa3e-c1fcf991ec99@x1g2000yqb.googlegroups.com> |
| In reply to | #4203 |
On May 10, 4:56 pm, Kevin <darkint...@gmail.com> wrote: ... > I'm trying to generate a random set of strings to fill a database with that > match the following pattern: /^([A-Z]|[a-z]|-)+$/ ... This gem might fit your need: https://github.com/benburkert/randexp cheers
[toc] | [prev] | [next] | [standalone]
| From | Kevin <darkintent@gmail.com> |
|---|---|
| Date | 2011-05-16 21:47 -0500 |
| Message-ID | <BANLkTi=_mW-026rx+rs8ejXCejEC1S==bg@mail.gmail.com> |
| In reply to | #4266 |
Thank you everyone. @Chad: Yes I do need the dashes in the string. I will probably incorporate your other suggestion with respect to letting the regex check the overall length as well @Chris: That definitely looks like it might do the trick. While writing my original message I had wondered if such a thing existed, and what do you know, it did.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.ruby
csiph-web