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>
| [reply] [d/l] |
Re: Re-dimensioning an HTML table with Perl ?
by choroba (Cardinal) 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,
| [reply] [d/l] |
Re: Re-dimensioning an HTML table with Perl ?
by LanX (Saint) 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.
| [reply] [d/l] [select] |
|
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
| [reply] |
|
| [reply] |
|
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. ;-)
| [reply] |
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'
| [reply] |
Re: Re-dimensioning an HTML table with Perl ?
by talexb (Chancellor) 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.
| [reply] |
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;
}
| [reply] [d/l] |
Re: Re-dimensioning an HTML table with Perl ?
by trippledubs (Deacon) on Apr 26, 2017 at 01:55 UTC
|
| [reply] |
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?
| [reply] |
|
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.
| [reply] |
|
| [reply] |
|
If they had such data, they would have already posted it.
| [reply] |
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 | [reply] |
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.
| [reply] |