[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Columns



Nico Rozendal <Nico.Rozendaal@NP.UNIMAAS.NL> wrote:

>I have a (text) file with the following format:
>
>123420010110010010101  (and so on this way => )
>234531111101100101010
>678940000010010111010
>340151010101110101010
>and so on...
>
>Now let's say the first four characters are a PIN number,
>the following two characters the subject's age, and so on.
>How can I read such a file split in (fixed) columns ?
>I would like to insert tabs at the end of each field, creating
>a tab-delimited text file.

See the Llama book Ch. 17 under Fixed-length Random Access Databases.

Try something like this:

my ($pin, $age) = unpack ("A4 A2", <>);
print ($pin, "\t", $age, "\n");

If you just want to insert tabs (and not process the data) then try:

print (join ("\t", unpack ("A4 A2", <>)), "\n");

>Thanks for your help, and a happy new year !

A happy new year to everyone else, too!

Michael Hoffman <grouse@eden.com>