Extracting lines from an address

I have an address stored in mysql as a text field.
I’m using ActiveRecord to access the field.
But how do I get each line of the address in to their own strings?

eg.

Address = “12, HouseName
Street
City
Country”

–>

Address_line1 = “12, HouseName”
Address_line2 = “Street”
Address_line3 = “City”
Address_line4 = “Country”

Many thanks, K.

there might be a cleaner way of doing it, but this will work:

assign database entry to a variable

address = “12, HouseName
Street
City
Country”

make an array from the string, dividing each line by line breaks

lines = address.split(“\n”)

each line is an element in the array, strip takes whitespace out

address_line1 = lines[0].strip
address_line2 = lines[1].strip
address_line3 = lines[2].strip
address_line4 = lines[3].strip

“Kris L.” [email protected] wrote in message
news:[email protected]

sorry, i should be more careful with the definition of strip() in this
comment:

strip takes whitespace out

more correcly, strip “Returns a copy of the string with leading and
trailing
whitespace removed.”

luke

Kris L. wrote:

–>

Address_line1 = “12, HouseName”
Address_line2 = “Street”
Address_line3 = “City”
Address_line4 = “Country”

address = “12, HouseName
Street
City
Country”

address_lines = address.split( /\n\s*/ )

puts address_lines[0]

address_line1, address_line2, address_line3, address_line4 =
address.split( /\n\s*/ )

puts address_line4

I have an address stored in mysql as a text field.
I’m using ActiveRecord to access the field.
But how do I get each line of the address in to their own strings?

address = "12, HouseName

puts address_line4

This method has a couple of minor shortcomings - it does not eliminate
all
unnecessary whitespace (specifically, it does not look at whitespace at
the beginning of the address field - if there were any, nor does it look
at trailing whitespace).

In that case, may I suggest the following solution?
(for readability, I have shortened Address_line1-4 to al1-4…

address = " 12, HouseName
            Street
            City
            Country "

# btw. you should never assume you'll just receive the 4 lines
# you were expecting - this is why I added in the *remains which
# will receive the extra lines, if any.
al1,al2,al3,al4,*remains =address.split("\n").map { |l| l.strip }

puts "A : '#{address}'"
puts "A1: '#{al1}'"
puts "A2: '#{al2}'"	# yes - for this example, I'm not checking
puts "A3: '#{al3}'"	# whether I get LESS than the 4 lines I
puts "A4: '#{al4}'"	# wanted...

if remains != []
  puts "oops... there are more than just the expected 4 lines"
  remains.each do |r|
    puts "  trailing line: #{r}"
  end
end

Running this will print:

A : ' 12, HouseName
            Street
            City
            Country '
A1: '12, HouseName'
A2: 'Street'
A3: 'City'
A4: 'Country'

Benedikt

ALLIANCE, n. In international politics, the union of two thieves who
have their hands so deeply inserted in each other’s pockets that
they cannot separately plunder a third.
(Ambrose Bierce, The Devil’s Dictionary)

Hi –

On Thu, 17 Nov 2005, Kris L. wrote:

I have an address stored in mysql as a text field.
I’m using ActiveRecord to access the field.
But how do I get each line of the address in to their own strings?

eg.

Address = "12, HouseName

You probably want a local variable there, rather than a constant.

      Street
      City
      Country"

–>

Address_line1 = “12, HouseName”
Address_line2 = “Street”
Address_line3 = “City”
Address_line4 = “Country”

This is actually a case where the sometimes odd-seeming way that
String#map works (as well as #each and #to_a) can work in your favor:

house,street,city,country = address.map {|line| line.strip }

David

Kris L. wrote:

–>

Address_line1 = “12, HouseName”
Address_line2 = “Street”
Address_line3 = “City”
Address_line4 = “Country”

adr = “12, HouseName
Street
City
Country”
=> “12, HouseName\n Street\n City\n
Country”

adr.to_a.each {|s| s.strip! }
=> [“12, HouseName”, “Street”, “City”, “Country”]

Kind regards

robert

Benedikt H. wrote:

Address_line2 = “Street”
puts address_lines[0]
at trailing whitespace).

btw. you should never assume you’ll just receive the 4 lines

you were expecting - this is why I added in the *remains which

will receive the extra lines, if any.

al1,al2,al3,al4,*remains =address.split("\n").map { |l| l.strip }

I can’t help but commend your work. However, it has the minor
shortcoming that the split is not needed:

al1,al2,al3,al4,*remains =address.map { |l| l.strip }

Thanks for the replies, very useful.
As always there appears to many ways of doing the same thing in ruby :slight_smile:

Cheers K.