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

I have an array @a, in which i have 3 rows in the $a[0], 2 rows in $a[1] and $a[2] as shown below.

<th><rowspan=2>Target protein</th> <th><colspan=2>Purified protein concentration</th> <th><rowspan=3>Activity</th> <th>Mg/liter culture</th> <th>Mg/g dry cell weight</th> <th>Mg/liter culture</th> <th>Mg/g dry cell weight</th>

What i want to do is, if $a[0] has rowspan=2 then we have to place one empty <th></th> in the corresponding place of $a[1], likewise if rowspan=3 the we have to place empty <th></th> in $a[0] and $a[1] in corresponding place.

output: <th><rowspan=2>Target protein</th> <th><colspan=2>Purified protein concentration</th> <th><rowspan=3>Activity</th> <th></th> <th>Mg/liter culture</th> <th><rowspan=2>Mg/g dry cell weight</th> <th></th> <th>Mg/liter culture</th> <th>Mg/g dry cell weight</th> <th></th> <th></th>

I tried by splitting newline characters and by using the index value of the newarray i assigned the empty cell in the next $a[1]. But i could not able to get the correct solution. Is there any other way to do this.

20050426 Janitored by Corion: Fixed formatting and inline <th> tags

Replies are listed 'Best First'.
Re: array manipulation
by ZlR (Chaplain) on Apr 26, 2005 at 10:11 UTC
    Hello,

    This really depends on what you have as an input and want to output, and what rules you apply to do it .
    I'm afraid the rules you have given do not create the output you want since $a[0] appears unmodified in the output.

    To do what something approaching what you asked, i used an array of array . It takes your @a and applies these rules :

    if $a[0] has rowspan=2 then we have to place one empty <th></th> in th +e corresponding place of $a[1] if rowspan=3 the we have to place empty <th></th> in $a[1] and $a[2] i +n corresponding place.

    I understand "corresponding place" as "at the begining of the same element number but in the other arrays" .

    #!perl ; use strict ; use warnings ; my @a ; my @b ; my @c ; my @splitted ; my $i ; my $j ; my $aref ; $a[0] = "<th><rowspan=2>Target protein</th><th><colspan=2>Purified pro +tein concentration</th><th><rowspan=3>Activity</th>" ; $a[1] = "<th>Mg/liter culture</th><th>Mg/g dry cell weight</th>" ; $a[2] = "<th>Mg/liter culture</th><th>Mg/g dry cell weight</th>" ; # First build an array of array foreach $i (0..$#a) { @splitted = $a[$i] =~ m#(<th>.*?</th>)#g ; $b[$i][$_] = shift @splitted for (0..$#splitted ) } # Apply rules $aref = $b[0]; for $j ( 0 .. $#{$aref} ) { if ( $b[0][$j] =~ m/rowspan=(\d)/ && $1 == 2 ) { $b[1][$j] = "<th></th>" . $b[1][$j] ; } elsif ( $b[0][$j] =~ m/rowspan=(\d)/ && $1 == 3 ) { $b[1][$j] = "<th></th>" . $b[1][$j] ; $b[2][$j] = "<th></th>" . $b[2][$j] ; } } # Merge into a simple array for $i ( 0 .. $#b) { $aref = $b[$i]; $c[$i] = join "", @$aref ; } # Format and print result foreach (@c) {$_ =~ s#</th>#</th>\n#g; print $_, "\n\n" ; }

    This will output (with some warnings ):

    <th><rowspan=2>Target protein</th> <th><colspan=2>Purified protein concentration</th> <th><rowspan=3>Activity</th> <th></th> <th>Mg/liter culture</th> <th>Mg/g dry cell weight</th> <th></th> <th>Mg/liter culture</th> <th>Mg/g dry cell weight</th> <th></th>

    So if you play around with Array of Array you might get to something matching exactly your needs .

    Hope this helps,

    zlr _

Re: array manipulation
by Jaap (Curate) on Apr 26, 2005 at 09:50 UTC
    Are you trying to convert the HTML to an array or do you have an array and are you trying to output HTML?

    Please show us the code you have so far too.
Re: array manipulation
by Cody Pendant (Prior) on Apr 26, 2005 at 10:36 UTC
    First of all, that can't be your actual HTML, surely? There's no such thing as a <rowspan>tag. More to the point, I'm not sure if it's possible to figure out what was the intention from the HTML you've given us. There's more than one possible correct answer to the problem as far as I can see. Is each of those blocks of code a table row? It doesn't make sense. Tell us the big picture, please. What are you trying to do, where does the code come from, etc.


    ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
    =~y~b-v~a-z~s; print