Tom Lehman
Tom Lehman

Reputation: 89373

Is there a parser for the output from Perl's Text::Table?

Suppose I have a bunch of tables created with Text::Table. Does there exist a parser to convert them back to Perl data structures, or do I have to write my own?

Upvotes: 1

Views: 778

Answers (5)

daotoad
daotoad

Reputation: 27234

Why not use CSV?

CSV isn't exactly great at being human readable if you have many columns of varying lengths, but it can be done in a pinch.

The good thing about CSV is that any spreadsheet (as well as many other tools) will parse and display CSV in a nice, friendly way.

Update

Brian,

Based on this comment by the OP, above:

The customer wants a tabular data format that's both human and machine readable. – Horace Loeb May 31 at 3:26

I drew the conclusion that he had control over the producing code as a the consuming code.

My answer is really answering the question "What format can I use that is both human and machine readable?"

Upvotes: 0

Axeman
Axeman

Reputation: 29854

You could persist them as the Perl selves before you output it. That way whatever you printed once, could be reprinted on demand. See Storable--or YAML::Syck, which would dump it out in YAML.

Once you has a format for Storable, you might

  1. Read the file in
  2. Have some standard format to check for requested changes.
  3. Write it back out if changed.

However, with YAML, it might be easy enough to just modify the YAML file.

The YAML for the example given for Text::Table, looks like this:

--- !!perl/hash:Text::Table 
blank: ~
cols: 
  - 
    - Mercury
    - Venus
    - Earth
    - Jupiter
  - 
    - 2360
    - 6110
    - 6378
    - 71030
  - 
    - 3.7
    - 5.1
    - 5.52
    - 1.3
forms: 
  - "%s %s %s"
  - "%s %s %s"
lines: ~
spec: 
  - 
    align: auto
    align_title: left
    align_title_lines: left
    sample: []

    title: 
      - Planet
  - 
    align: auto
    align_title: left
    align_title_lines: left
    sample: []

    title: 
      - Radius
      - km
  - 
    align: auto
    align_title: left
    align_title_lines: left
    sample: []

    title: 
      - Density
      - g/cm^3
titles: 
  - 
    - Planet
    - "      "
  - 
    - Radius
    - "km    "
  - 
    - Density
    - "g/cm^3 "

Upvotes: -1

brunov
brunov

Reputation: 1003

It seems somewhat convoluted. If you had the information before converting it into a table, then why try to parse it from its presentation form? It's like having a text file, converting it to latex, then to postscript, and then trying to get the text back from the postscript file.

I'm sure there's a way to parse the output of Text::Table, but it seems that your workflow is flawed; I'd aim at using a simpler output for the data (besides Text::Table, if you really have to have it that way) like YAML that can then be trivially restored to the original data structure.

Upvotes: 2

Michael Carman
Michael Carman

Reputation: 30851

Text::Table is a module for creating data presentations. If it were intended for storage and retrieval (i.e. a file format) it would include methods for parsing existing tables.

Upvotes: 2

Alexandr Ciornii
Alexandr Ciornii

Reputation: 7390

By doing 'site:search.cpan.org "Text::Table" parse' search on Google I found no module for this purpose.

Upvotes: 0

Related Questions