[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

trying to create an array with AA-ZZ for dutch postal codes

bookie988

5/6/2008 1:36:00 PM

the array postcodes should be filled with strings starting looking
like

["AA",AB",AC"..."BA","BB","BC" to "ZZ"] 26*26 entries.

i've come up with the following to create it,

postcodes = Array.new

# create postal codes array

W = String.new()
X = String.new()

(A..Z).each do |W| {

puts W
(A..Z).each do |X| {
postcodes.push("#{W}#{X}")

}
}

puts(postcodes)

this code produces

hpricot-test.rb:20: syntax error, unexpected tCONSTANT, expecting kDO
or '{' or '('
hpricot-test.rb:22: odd number list for Hash
hpricot-test.rb:25: syntax error, unexpected '}', expecting kEND
hpricot-test.rb:57: syntax error, unexpected $end, expecting kEND

what am i doing wrong, is there an easier (rubier) way?
6 Answers

-j b-

5/6/2008 1:45:00 PM

0

unknown wrote:
> the array postcodes should be filled with strings starting looking
> like
>
> ["AA",AB",AC"..."BA","BB","BC" to "ZZ"] 26*26 entries.
>
...
>
> what am i doing wrong, is there an easier (rubier) way?

("AA".."ZZ").to_a
--
Posted via http://www.ruby-....

Harry Kakueki

5/6/2008 1:49:00 PM

0

On Tue, May 6, 2008 at 10:40 PM, <bookie988@gmail.com> wrote:
> the array postcodes should be filled with strings starting looking
> like
>
> ["AA",AB",AC"..."BA","BB","BC" to "ZZ"] 26*26 entries.
>
> i've come up with the following to create it,
>
> postcodes = Array.new
>
> # create postal codes array
>
> W = String.new()
> X = String.new()
>
> (A..Z).each do |W| {
>
> puts W
> (A..Z).each do |X| {
> postcodes.push("#{W}#{X}")
>
> }
> }
>
> puts(postcodes)
>
> this code produces
>
> hpricot-test.rb:20: syntax error, unexpected tCONSTANT, expecting kDO
> or '{' or '('
> hpricot-test.rb:22: odd number list for Hash
> hpricot-test.rb:25: syntax error, unexpected '}', expecting kEND
> hpricot-test.rb:57: syntax error, unexpected $end, expecting kEND
>
> what am i doing wrong, is there an easier (rubier) way?
>
>

("AA".."ZZ").each {|x| p x}

Harry
--
A Look into Japanese Ruby List in English
http://www.kakueki.com/ruby...

Farrel Lifson

5/6/2008 1:50:00 PM

0

postcodes = "AA".."ZZ".to_a

Farrel

2008/5/6 <bookie988@gmail.com>:
> the array postcodes should be filled with strings starting looking
> like
>
> ["AA",AB",AC"..."BA","BB","BC" to "ZZ"] 26*26 entries.
>
> i've come up with the following to create it,
>
> postcodes = Array.new
>
> # create postal codes array
>
> W = String.new()
> X = String.new()
>
> (A..Z).each do |W| {
>
> puts W
> (A..Z).each do |X| {
> postcodes.push("#{W}#{X}")
>
> }
> }
>
> puts(postcodes)
>
> this code produces
>
> hpricot-test.rb:20: syntax error, unexpected tCONSTANT, expecting kDO
> or '{' or '('
> hpricot-test.rb:22: odd number list for Hash
> hpricot-test.rb:25: syntax error, unexpected '}', expecting kEND
> hpricot-test.rb:57: syntax error, unexpected $end, expecting kEND
>
> what am i doing wrong, is there an easier (rubier) way?
>
>

Peter Hickman

5/6/2008 1:55:00 PM

0

bookie988@gmail.com wrote:
> the array postcodes should be filled with strings starting looking
> like
>
> ["AA",AB",AC"..."BA","BB","BC" to "ZZ"] 26*26 entries.
>
> i've come up with the following to create it,
>
> postcodes = Array.new
>
> # create postal codes array
>
> W = String.new()
> X = String.new()
>
> (A..Z).each do |W| {
>
> puts W
> (A..Z).each do |X| {
> postcodes.push("#{W}#{X}")
>
> }
> }
>
> puts(postcodes)
>
> this code produces
>
> hpricot-test.rb:20: syntax error, unexpected tCONSTANT, expecting kDO
> or '{' or '('
> hpricot-test.rb:22: odd number list for Hash
> hpricot-test.rb:25: syntax error, unexpected '}', expecting kEND
> hpricot-test.rb:57: syntax error, unexpected $end, expecting kEND
>
> what am i doing wrong, is there an easier (rubier) way?
>
>
>
Sorry but there are many errors here: First some code that works:

postcodes = Array.new

('A'..'Z').each do |x|
('A'..'Z').each do |y|
postcodes << "#{x}#{y}"
end
end

puts(postcodes)

Note that the A..Z is now 'A'..'Z'. This is because A is a reference to
a constant called A which you haven't defined but 'A' is a upper case
letter.

When doing a xxx.each you can do it either way:

('A'..'Z').each do |x|
puts x
end

or

('A'..'Z').each {|x|
puts x
}

You seem to be mixing both.

Your use of W and X as variables. Firstly upper case letters are
constants so you can not use them as looping variables. Infact you do
not need to predeclare the variables at all.

bookie988

5/6/2008 2:00:00 PM

0

On May 6, 3:54 pm, Peter Hickman <pe...@semantico.com> wrote:
> bookie...@gmail.com wrote:
> > the array postcodes should be filled with strings starting looking
> > like
>
> > ["AA",AB",AC"..."BA","BB","BC" to "ZZ"] 26*26 entries.
>
> > i've come up with the following to create it,
>
> > postcodes = Array.new
>
> > # create postal codes array
>
> > W = String.new()
> > X = String.new()
>
> > (A..Z).each do |W| {
>
> > puts W
> > (A..Z).each do |X| {
> > postcodes.push("#{W}#{X}")
>
> > }
> > }
>
> > puts(postcodes)
>
> > this code produces
>
> > hpricot-test.rb:20: syntax error, unexpected tCONSTANT, expecting kDO
> > or '{' or '('
> > hpricot-test.rb:22: odd number list for Hash
> > hpricot-test.rb:25: syntax error, unexpected '}', expecting kEND
> > hpricot-test.rb:57: syntax error, unexpected $end, expecting kEND
>
> > what am i doing wrong, is there an easier (rubier) way?
>
> Sorry but there are many errors here: First some code that works:
>
> postcodes = Array.new
>
> ('A'..'Z').each do |x|
> ('A'..'Z').each do |y|
> postcodes << "#{x}#{y}"
> end
> end
>
> puts(postcodes)
>
> Note that the A..Z is now 'A'..'Z'. This is because A is a reference to
> a constant called A which you haven't defined but 'A' is a upper case
> letter.
>
> When doing a xxx.each you can do it either way:
>
> ('A'..'Z').each do |x|
> puts x
> end
>
> or
>
> ('A'..'Z').each {|x|
> puts x
>
> }
>
> You seem to be mixing both.
>
> Your use of W and X as variables. Firstly upper case letters are
> constants so you can not use them as looping variables. Infact you do
> not need to predeclare the variables at all.

thanx

Sandro Paganotti

5/6/2008 2:10:00 PM

0

I'm having fun with arrays; here's my version:

((a = ('A'..'Z').to_a) * (b=a.length)).zip(a.collect{|e|
[e]*b}.flatten).collect{|e| e.join}.join("\n")

On Tue, May 6, 2008 at 2:05 PM, aleister <bookie988@gmail.com> wrote:
> On May 6, 3:54 pm, Peter Hickman <pe...@semantico.com> wrote:
>
>
> > bookie...@gmail.com wrote:
> > > the array postcodes should be filled with strings starting looking
> > > like
> >
> > > ["AA",AB",AC"..."BA","BB","BC" to "ZZ"] 26*26 entries.
> >
> > > i've come up with the following to create it,
> >
> > > postcodes = Array.new
> >
> > > # create postal codes array
> >
> > > W = String.new()
> > > X = String.new()
> >
> > > (A..Z).each do |W| {
> >
> > > puts W
> > > (A..Z).each do |X| {
> > > postcodes.push("#{W}#{X}")
> >
> > > }
> > > }
> >
> > > puts(postcodes)
> >
> > > this code produces
> >
> > > hpricot-test.rb:20: syntax error, unexpected tCONSTANT, expecting kDO
> > > or '{' or '('
> > > hpricot-test.rb:22: odd number list for Hash
> > > hpricot-test.rb:25: syntax error, unexpected '}', expecting kEND
> > > hpricot-test.rb:57: syntax error, unexpected $end, expecting kEND
> >
> > > what am i doing wrong, is there an easier (rubier) way?
> >
> > Sorry but there are many errors here: First some code that works:
> >
> > postcodes = Array.new
> >
> > ('A'..'Z').each do |x|
> > ('A'..'Z').each do |y|
> > postcodes << "#{x}#{y}"
> > end
> > end
> >
> > puts(postcodes)
> >
> > Note that the A..Z is now 'A'..'Z'. This is because A is a reference to
> > a constant called A which you haven't defined but 'A' is a upper case
> > letter.
> >
> > When doing a xxx.each you can do it either way:
> >
> > ('A'..'Z').each do |x|
> > puts x
> > end
> >
> > or
> >
> > ('A'..'Z').each {|x|
> > puts x
> >
> > }
> >
> > You seem to be mixing both.
> >
> > Your use of W and X as variables. Firstly upper case letters are
> > constants so you can not use them as looping variables. Infact you do
> > not need to predeclare the variables at all.
>
> thanx
>
>



--
Go outside! The graphics are amazing!