Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re-dimensioning an HTML table with Perl ?

by TheDonald (Initiate)
on Apr 25, 2017 at 17:08 UTC ( #1188881=perlquestion: print w/replies, xml ) Need Help??

TheDonald has asked for the wisdom of the Perl Monks concerning the following question:

I'm staring down the abyss of something that I would rather not do manually. So it occurred to me that Perl is probably just the sort of thing for such a random requirement. What I need to do is "re-dimension" (what is the correct word ?) an HTML table. The table is currently 5x12, and I want to turn it into, say, 7x9. The thought of lifting and shifting dozens of cells by hand is filling me with dread ! I've no ideas where to start in terms of automating this, let alone how to code it (I've only recently joined the long road to Perl wisdom !).
  • Comment on Re-dimensioning an HTML table with Perl ?

Replies are listed 'Best First'.
Re: Re-dimensioning an HTML table with Perl ?
by FreeBeerReekingMonk (Deacon) on Apr 25, 2017 at 19:21 UTC
    You can fiddle with $COLUMS to change the table row-width

    use strict; use warnings; use XML::Simple; use Data::Dumper; # read __DATA__ file into $_ { $/ = undef; $_ = <DATA>; close DATA; } # use XML::Simple to read the table string my $ref = XMLin( $_ , NoAttr=>1 ); my $tr = $ref->{tr}; my @ARR; # flatten table structure into an array for my $row (keys @{$tr}){ my @X = values @{(values %{ $tr->[$row] })[0]}; push @ARR, @X; } # now we reformat the table my $COLUMNS = 2; my $counter = 0; my %S; for my $v (@ARR){ my $i=int($counter/$COLUMNS); my $j=int($counter % $COLUMNS); push @{$S{'tr'}->[$i]->{'td'}}, $v; ++$counter; } # We use XML::Simple to transform this stucture to an xml/html string my $xml = new XML::Simple (NoAttr=>1, RootName=>'table'); # convert Perl array ref into XML document my $new_table = $xml->XMLout(\%S); # now show the result print $new_table; __DATA__ <table style="width:100%"> <tr> <th>Firstname</th> <th>Lastname</th> <th>Age</th> </tr> <tr> <td>Jill</td> <td>Smith</td> <td>50</td> </tr> <tr> <td>Eve</td> <td>Jackson</td> <td>94</td> </tr> </table>
Re: Re-dimensioning an HTML table with Perl ?
by choroba (Archbishop) on Apr 26, 2017 at 09:10 UTC
    I usually use HTML::TableExtract to convert an HTML table into an array of arrays. You can then do whatever you like with the structure, serializing it back to HTML should be easy.
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Re-dimensioning an HTML table with Perl ?
by LanX (Archbishop) on Apr 25, 2017 at 17:36 UTC
    > "re-dimension" ... an HTML table.

    if it's only one table better do it manually!

    For a recurring task you'd need a proper HTML parser...

    If you really prefer to code it for just one table:

    • identify and cut the table by hand
    • try to split on each <tr to get the rows
    • and than the row on each <td to get the columns
    • create an AOA (array of arrays) representing your table
    • print a join of those rows and columns you need

    NOTE: This is fragile and only suitable for half-automatic use.

    I won't code this for you... do it manually.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      You remind me of that "How To Do It" sketch by Monty Python.

      "To play flute you simply hold down these keys, and blow here." xD

      "I won't code this for you."

      But you will expect XP for your "contribution." XP for xD

      "You're fired."
        Probably the best reply of the bunch. Unlike the others, if you're not going to answer the question, at least make an effort at an alternative answer. ;-)
Re: Re-dimensioning an HTML table with Perl ?
by shmem (Chancellor) on Apr 25, 2017 at 17:54 UTC
    The thought of lifting and shifting dozens of cells by hand is filling me with dread !

    Come on, these are just 5 dozen. About a quarter of an hour if you manage to lift and shift 4 cells per minute.

    To begin with programmatically, I'd describe first which cell from the origin table goes to which cell of the destination table, then write a program to do it, making some loops which iterate over the origin rows and cells. If something fails, I'd come back here and ask specifics, posting my code.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re: Re-dimensioning an HTML table with Perl ?
by talexb (Canon) on Apr 25, 2017 at 20:21 UTC

    Figure out how you'd do it manually, then have the computer do the same thing.

    I think I'd probably read the HTML table into Excel, then dump the result into a CSV. From there, I would get a Perl script to read the CSV and write out a new table. Presumably you want the first seven elements (1,1 to 1,5, 2,1 and 2,2) to go into the first row of the new table, and son on.

    But there are many ways to solve this problem. :) Let us know how it turns out!

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Re: Re-dimensioning an HTML table with Perl ?
by raymorris (Novice) on Apr 25, 2017 at 20:57 UTC

    Reading between the lines, it seems likely that what you have is a bunch of elements, not rows and columns of data (in other words, not really a table). using a table to format a collection of boxes is a very HTML 3.2, 1998ish way of doing HTML (and it wasn't the suggested method even back then). It seems somebody noticed that on their monitor, with the browser window whatever size they happen to have it at the moment, using their preferred font size, 7 items can fit in a row. Adjusting layout to fit different screens, different text sizes, etc is precisely what a web browser is for! It very likely shouldn't be a table with some predefined number of rows at all. Rather, it should be a sequence of boxes. Let the browser do its job and decide how many boxes fit in each row, on a particular display.

    To make each box the same width so they line up nicely you'd use some CSS like this:

    .gallery{ display:block; } .thumbnail{ display:inline-block; border:1px solid black; float:left; height:200px; width:100px; margin-left: 1em; }
Re: Re-dimensioning an HTML table with Perl ?
by trippledubs (Deacon) on Apr 26, 2017 at 01:55 UTC
Re: Re-dimensioning an HTML table with Perl ?
by mr_mischief (Monsignor) on Apr 25, 2017 at 21:46 UTC

    What tabular data do you have that can change its number of data points per item without having the data updated?

      Its a series of tables with images (e.g. sponsor logos). The point is I want to stretch it, i.e. expanding the width just brings up the stuff from the next line. Just like when you expand/contract a text window in Word or whatever.

        So it turns out it isn't a table at all (because the rows and columns have no semantic meaning) but actually a grid (a 2D representation of a linear series). In which case you should use the same method with Perl as you would without Perl: a responsive layout with appropriate CSS.

      If they had such data, they would have already posted it.
Re: Re-dimensioning an HTML table
by Anonymous Monk on Apr 25, 2017 at 23:26 UTC
    What? Just any html editor, there is no way you need to automate this task with a program
Re: Re-dimensioning an HTML table with Perl ?
by karthiknix (Sexton) on Apr 27, 2017 at 12:26 UTC

    you need to fix the table data such as rows and columns. Perl can be used to match these data and replace it where ever you want according to your dimension. Perl Regex is effective in doing this task.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1188881]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2020-02-21 10:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (94 votes). Check out past polls.

    Notices?