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

Re: [FWP] Interpreting a columnar text file.



>>>>> "RLS" == Randal L Schwartz <merlyn@stonehenge.com> writes:

>>>>> "Chaim" == Chaim Frenkel <chaimf@pobox.com> writes:
Chaim> I found this piece of code useful to generate an unpack format when I
Chaim> needed to translate a columnar text file.

Chaim> $fmt = "";
Chaim> while(<>) {chomp; $fmt |= $_};

Chaim> $fmt =~ y/ /X/c;
Chaim> $fmt =~ s/(X+\s+)/"A".(length($1)-1)."x"/ge;

RLS> It seems that "foo     bar" would be incorrectly translated to "A3xA3"
RLS> with this?  I don't think you want to collapse \s+ there.

Why? Doesn't (X+\s+) include the whitespace in the length?

Most columnar data that I've seen usually leave enough space for the
largest field + one extra for minimal readablity.

The mistakes that I noticed was the tail end wasn't converted,
it doesn't account for numeric data being right justified, and
that if a spurious whitespace shows up in the middle would confuse
it. But given enough data that shouldn't be a problem.

RLS> $fmt =~ s/\G(\s*)(\S+)/" x".length($1)." A".length($2)/ge;

Why the \G ?

Your solution excludes the whitespace from both sides, my quickie
adds the whitespace to the preceding column.

<chaim>
-- 
Chaim Frenkel					     Nonlinear Knowledge, Inc.
chaimf@pobox.com				               +1-718-236-0183

==== Want to unsubscribe from Fun With Perl?
==== Well, if you insist... Send mail with body "unsubscribe" to
==== fwp-request@technofile.org