On Thu, 13 Feb 2003 01:29:02 +0900, Warren Brown wrote:

> Tim,
> 
>> Although I think I understand what's going on here, it leaves me
>> wondering what would be a better way to do what I need to do, if there
>> is a better way.
>>
>> The input file contains three tab-separated fields. The second field
>> needs to be able to contain an arbitrary number of newlines (which will
>> be realized as actual newlines, so to speak, when this field is later
>> printed out).
> 
>     The basic problem here is that you have a two-level data structure
>     that
> you want to represent.  The first level is three fields separated by the
> string "\t", and the second level is zero or more sub-fields separated by
> '\n' in the second field.  Your decision to use '\n' as a separator led
> you to a small confusion when you tried to display the second field.  As
> others have pointed out, this can be achieved by simply replacing the
> two-character string '\n' with the single character string "\n" (note the
> difference in quotes).  Also note that the string '\n' is really just a
> sub-field delimiter, and could actually be any string (or character) other
> than your field separator ("\t") that is guaranteed not to be part of a
> sub-field.
> 
>     I would think that it would be useful for you to have this two-level
> data structure represented in your code.  For instance:
> 
> irb(main):001:0> line = "field 1\tfield 2.1\\nfield 2.2\\nfield 2.3\tfield
> 3"
> => "field 1\tfield 2.1\\nfield 2.2\\nfield 2.3\tfield 3"
> 
> irb(main):002:0> fields = line.split(/\t/) => ["field 1", "field
> 2.1\\nfield 2.2\\nfield 2.3", "field 3"]
> 
> irb(main):003:0> fields[1] = fields[1].split(/\\n/) => ["field 2.1",
> "field 2.2", "field 2.3"]
> 
> irb(main):004:0> fields
> => ["field 1", ["field 2.1", "field 2.2", "field 2.3"], "field 3"]
> 
>     You now have access to the sub-fields (e.g. fields[1].length,
> fields[1][0], etc.) and can easily join them back together for display:
> 
> irb(main):005:0> puts fields[1].join("\n") field 2.1
> field 2.2
> field 2.3
> => nil
> 
>     Hope this helps!

Thanks, Warren. I thought about doing this as well.

However, while it works, conceptually this isn't what I want to do. The
second field IS a single field; it's just that when I later print it out,
it needs to spread over multiple lines. Embedding the newlines in the
original value seems to me to be the simplest way to achieve this, but it
turns out that hoops must be jumped through in order to get those newlines
to do their job.

-- 
Tim Kynerd   Sundbyberg (sméÔtan i storstan), Sweden   tim / tram.nu
Sunrise in Stockholm today:  7:39
Sunset in Stockholm today:  16:24
My rail transit photos at http://www.kynerd.nu