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 _


In reply to Re: array manipulation by ZlR
in thread array manipulation by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.