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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |