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

**URL:** <https://www.ruby-forum.com/t/trying-to-create-an-array-with-aa-zz-for-dutch-postal-codes/136811>\
**Category:** Ruby\
**Created:** [May 6, 2008, 3:41pm UTC](https://www.ruby-forum.com/t/trying-to-create-an-array-with-aa-zz-for-dutch-postal-codes/136811 "2008-05-06T15:41:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 6, 2008, 3:41pm UTC](https://www.ruby-forum.com/t/trying-to-create-an-array-with-aa-zz-for-dutch-postal-codes/136811/1 "2008-05-06T15:41:13Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 6, 2008, 3:44pm UTC](https://www.ruby-forum.com/t/trying-to-create-an-array-with-aa-zz-for-dutch-postal-codes/136811/2 "2008-05-06T15:44:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 6, 2008, 3:50pm UTC](https://www.ruby-forum.com/t/trying-to-create-an-array-with-aa-zz-for-dutch-postal-codes/136811/3 "2008-05-06T15:50:11Z")

</div>

On Tue, May 6, 2008 at 10:40 PM, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> }
> 
> ```
> 
> what am i doing wrong, is there an easier (rubier) way?

(“AA”…“ZZ”).each {|x| p x}

Harry

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 6, 2008, 3:51pm UTC](https://www.ruby-forum.com/t/trying-to-create-an-array-with-aa-zz-for-dutch-postal-codes/136811/4 "2008-05-06T15:51:21Z")

</div>

postcodes = “AA”…“ZZ”.to\_a

Farrel

2008/5/6 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 6, 2008, 3:55pm UTC](https://www.ruby-forum.com/t/trying-to-create-an-array-with-aa-zz-for-dutch-postal-codes/136811/5 "2008-05-06T15:55:31Z")

</div>

removed\_email\_address@domain.invalid wrote:

> ```
> }
> 
> ```
> 
> 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.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 6, 2008, 4:05pm UTC](https://www.ruby-forum.com/t/trying-to-create-an-array-with-aa-zz-for-dutch-postal-codes/136811/6 "2008-05-06T16:05:38Z")

</div>

On May 6, 3:54 pm, Peter H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > # create postal codes array
> > 
> > ```
> > }
> > 
> > ```
> > 
> > hpricot-test.rb:57: syntax error, unexpected $end, expecting kEND  
> > end  
> > (‘A’…‘Z’).each do |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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 6, 2008, 4:11pm UTC](https://www.ruby-forum.com/t/trying-to-create-an-array-with-aa-zz-for-dutch-postal-codes/136811/7 "2008-05-06T16:11:31Z")

</div>

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")
