fixed width text files 
Author Message
 fixed width text files

I have a text file with approximately 400 rows.  Each row has 31
columns, and each column has a different width.  I'm a newbie so if
this elementary question frustrates anyone I apologize.  But, how can
I read from this file and set the width for each individual column so
I can reference them?

TIA,
Jeff



Sun, 08 Oct 2000 03:00:00 GMT  
 fixed width text files

: I have a text file with approximately 400 rows.  Each row has 31
: columns, and each column has a different width.  I'm a newbie so if
: this elementary question frustrates anyone I apologize.  But, how can
: I read from this file and set the width for each individual column so
: I can reference them?

I assume that when you say "each column has a different width",
you mean a different width between the columns, not between rows.
(i.e. the first column is always 5 chars, the second is 6, etc.)

If I understand your question correctly, you might try something
like this:

  open(F, $filename) || die "Can't open $filename";
  while $line (<F>) {

  }

Replace the ellpisis with the rest of the pertinent widths.
Now $data[0] has the first column's data, $data[1] has the
second, etc. within the while loop.

This handy-dandy use of the match operator is documented
in the perlop manpage.  To quote:

    If used in a context that requires a list value, a
    pattern match returns a list consisting of the
    subexpressions matched by the parentheses in the
    pattern, i.e., ($1, $2, $3...).  ...

G'luck.

[ cc'd ]



Sun, 08 Oct 2000 03:00:00 GMT  
 fixed width text files

Quote:

> I have a text file with approximately 400 rows.  Each row has 31
> columns, and each column has a different width.

Ok.

Quote:
>  I'm a newbie so if
> this elementary question frustrates anyone I apologize.

No particularly.

Quote:
>  But, how can
> I read from this file and set the width for each individual column so
> I can reference them?

I'm not exactly sure what you mean by getting the width, but
length($yourVariable);

Will tell you how many characters are in $yourVariable.

Quote:
> TIA,
> Jeff

How are the fields divided?  Comma-delimited?  Double colon (::)?
Space?  Tab?

All very relevant information.

Maybe a few sample lines of the input file, and what you've tried (aka a
code snippet) would be appropriate?

For help without providing the above, please post to
comp.lang.perl.psychic

No one here (at least not me) is psychic.  We're willing to help those
who (at least attempt to) help themselves.

HTH.

Dave

--
"Security through obscurity is no security at all."
                -comp.lang.perl.misc newsgroup posting

----------------------------------------------------------------------


----------------------------------------------------------------------



Sun, 08 Oct 2000 03:00:00 GMT  
 fixed width text files

Thanks, I've provided some more info below.  The reply just below
your's has the following suggestion.

open(F, $filename) || die "Can't open $filename";
  while $line (<F>) {

  }

I haven't tried it yet but it's heading the right direction.

On Wed, 22 Apr 1998 13:11:29 -0500, Dave Barnett

Quote:


>> I have a text file with approximately 400 rows.  Each row has 31
>> columns, and each column has a different width.
>Ok.

>>  I'm a newbie so if
>> this elementary question frustrates anyone I apologize.
>No particularly.

>>  But, how can
>> I read from this file and set the width for each individual column so
>> I can reference them?

>I'm not exactly sure what you mean by getting the width, but
>length($yourVariable);

By width I mean length,  field(0) has a length of 24, field(1) has a
length of 8, field(10) has a length of 2

Quote:

>Will tell you how many characters are in $yourVariable.

>> TIA,
>> Jeff
>How are the fields divided?  Comma-delimited?  Double colon (::)?
>Space?  Tab?

The fields are not delimited.  This file is coming from an outside
source.  It looks to me that they are feeding the data from a table
straight into a fixed width text file.
Quote:

>All very relevant information.

>Maybe a few sample lines of the input file, and what you've tried (aka a
>code snippet) would be appropriate?

OK

01155800281             1998022319980309199801221998021802700000748KWH
00000083211998030501ELECTRIC       DUKEP28272B DUKEP0002DUKE POWER
P O BOX 70516                 C{*filter*}TE    NC28272
19980310637245CAR-64102-5136-074-003CAR641025136074003600 N. SUMMIT
AVE             C{*filter*}TE     NC0000000000617244

This is one line of input data.  This is deceiving.  The spaces do not
represent a delimiter but unfilled spaces for that particular field.
I have a data file layout that describes the field type, start/stop
for the field, length of field.

- Show quoted text -

Quote:

>For help without providing the above, please post to
>comp.lang.perl.psychic

>No one here (at least not me) is psychic.  We're willing to help those
>who (at least attempt to) help themselves.

>HTH.

>Dave

>--
>"Security through obscurity is no security at all."
>            -comp.lang.perl.misc newsgroup posting

>----------------------------------------------------------------------


>----------------------------------------------------------------------



Mon, 09 Oct 2000 03:00:00 GMT  
 fixed width text files

Quote:

>Thanks, I've provided some more info below.  The reply just below
>your's has the following suggestion.

>open(F, $filename) || die "Can't open $filename";
>  while $line (<F>) {

>  }

>I haven't tried it yet but it's heading the right direction.

(snip)

Both substr() and unpack() are mentioned as solutions in PerlFAQ4, section "How
do I extract selected columns from a string?".

If you've got a big file, you'll find these functions a bit faster for grabbing
fixed-width fields. Regexes are marvelous things, but they're not the ideal
solution to every problem.

Example:

open(F, $filename) or die "Can't open $filename: $!\n";
while (<F>) {
        $field1 = substr($_,0,24);
        $field2 = substr($_,24,8);
        .
        .
        .

Quote:
}

--
                               Jeffrey R. Drumm, Systems Integration Specialist
                       Maine Medical Center - Medical Information Systems Group

"Broken? Hell no! Uniquely implemented!" - me


Mon, 09 Oct 2000 03:00:00 GMT  
 fixed width text files

Quote:


> : I have a text file with approximately 400 rows.  Each row has 31
> : columns, and each column has a different width.  I'm a newbie so if
> : this elementary question frustrates anyone I apologize.  But, how can
> : I read from this file and set the width for each individual column so
> : I can reference them?

> I assume that when you say "each column has a different width",
> you mean a different width between the columns, not between rows.
> (i.e. the first column is always 5 chars, the second is 6, etc.)

> If I understand your question correctly, you might try something
> like this:

>   open(F, $filename) || die "Can't open $filename";
>   while $line (<F>) {

>   }

I would use split() ... but that's only me!!

--
Ala Qumsieh             |  No .. not just another
ASIC Design Engineer    |  Perl Hacker!!!!!
Matrox Graphics Inc.    |
Montreal, Quebec        |  (Not yet!)



Tue, 24 Oct 2000 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Tk::Text text width with non-fixed fonts

2. Fixed width, but varying # of fields - better way?

3. Fix Width...

4. FIXED with text files

5. Database (tie) from fixed format text file?

6. width of text in canvas

7. Tk::Dialog "text width" Please Ignore

8. Tk::Dialog "text width"

9. Problem with converting DOS text files to UNIX text files

10. Proposed Win95 Fixes for Text::MetaText's metapage utility

11. Getting width and height on graphic files

12. MAX FILE WIDTH TOOL

 

 
Powered by phpBB® Forum Software