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

Dear Masters,
How can I parse this text:
__DATA__ Pm 0.2707 Minimum match (10/15) Top 3 motifs Wid Score1 Segment Con Deg Mtf 1 15 1.700 10 CGATATCGATTCGTT CGATATYGAWTSSTT Final Motif 1: Wid 15 Score1 1.700 Segment 10 A C G T Con rCon Deg rDeg 1 7 81 4 7 C G C G 2 23 4 66 7 G C G C 3 69 19 4 7 A T A T 4 7 4 4 84 T A T A 5 84 4 4 7 A T A T >2522963-2522975-reverse St b99 CGATATCGATGGATT >2522963-2522975-reverse St f145 CGATATCGAATCGCT >2522963-2522975-reverse St b155 CGATATTAATTCCTT >2522963-2522975-reverse St f156 CGCTATTGAATGCGT >2523043-2523055-reverse St b88 CAATAGCGATTCGAT Mtf 2 15 1.666 12 AATGAATCAATATCG AAKSAWTYRATATCG Final Motif 2: Wid 15 Score1 1.666 Segment 12 A C G T Con rCon Deg rDeg 1 73 4 17 6 A T A T 2 60 17 17 6 A T A T 3 6 17 30 46 T A K M 4 6 44 44 6 C G S S 5 46 17 17 20 A T A T >2522963-2522975-reverse St b81 ACGCATTCAATAGCG >2522963-2522975-reverse St f82 AAGGAATTAATATCG >2522963-2522975-reverse St b92 AGCGATTCGATATCG >2522963-2522975-reverse St b106 GATGGATTTATATCT >2522963-2522975-reverse St f138 AATCCATCGATATCG Mtf 3 15 1.656 10 TCGATATCGAATGGC TCGMTATCGAWTSGC Final Motif 3: Wid 15 Score1 1.656 Segment 10 A C G T Con rCon Deg rDeg 1 7 4 4 84 T A T A 2 7 81 4 7 C G C G 3 23 4 66 7 G C G C 4 54 35 4 7 A T M K 5 7 4 4 84 T A T A 6 84 4 4 7 A T A T 7 7 4 19 69 T A T A 8 23 50 4 23 C G C G 9 23 4 66 7 G C G C 10 84 4 4 7 A T A T 11 54 4 4 38 A T W W 12 23 4 19 54 T A T A 13 7 35 50 7 G C S S 14 23 19 50 7 G C G C 15 23 35 19 23 C G C G >2522963-2522975-reverse St b87 TCAATAGCGATTCGA >2522963-2522975-reverse St b98 TCGATATCGATGGAT >2522963-2522975-reverse St f144 TCGATATCGAATCGC >2522963-2522975-reverse St f155 TCGCTATTGAATGCG >2522963-2522975-reverse St f204 TCGCTATAAAAAGGC Total time 0:0:0.
So that it return this AoA:
$VAR = [ ['CGATATYGAWTSSTT', '>2522963-2522975-reverse St f145 CGATATCGAATCGCT', '>2522963-2522975-reverse St f156 CGCTATTGAATGCGT',], ['AAKSAWTYRATATCG', '>2522963-2522975-reverse St f82 AAGGAATTAATATCG', '>2522963-2522975-reverse St f138 AATCCATCGATATCG',], ['TCGMTATCGAWTSGC', '>2522963-2522975-reverse St f144 TCGATATCGAATCGC', '>2522963-2522975-reverse St f155 TCGCTATTGAATGCG', '>2522963-2522975-reverse St f204 TCGCTATAAAAAGGC',] ];
Note that we: I am totally lost right from the beginning:
use warnings; use strict; use Data::Dumper; use Carp; my @aoa; my @temp; while (<DATA>) { chomp; if (/^Mtf (\d+) (.*) ([A-Z]+)$/) { # stuck here print "$_ \n"; push @temp, ($3); } elsif (/^>(\w+) St (b|f)\d+/) { # not sure how to capture only "f" + here print "$_\n"; } push @aoa, [@temp]; } #print Dumper \@aoa ;


---
neversaint and everlastingly indebted.......

Replies are listed 'Best First'.
Re: Selective Parsing Problem
by shmem (Chancellor) on Jun 06, 2007 at 15:49 UTC
    I'd do something like
    #!/usr/bin/perl use Data::Dumper; $Data::Dumper::Indent = 1; my $ary = []; my $i = -1; while (<DATA>) { chomp; s/\r//; /^Mtf.*\s(\S+)$/ and push @{ $ary->[++$i] }, $1; # next array /\s+St f\d+/ and push @{ $ary->[$i] }, $_; } print Dumper($ary); __DATA__ - your data here -

    Output:

    $VAR1 = [ [ 'CGATATYGAWTSSTT', '>2522963-2522975-reverse St f145 CGATATCGAATCGCT', '>2522963-2522975-reverse St f156 CGCTATTGAATGCGT' ], [ 'AAKSAWTYRATATCG', '>2522963-2522975-reverse St f82 AAGGAATTAATATCG', '>2522963-2522975-reverse St f138 AATCCATCGATATCG' ], [ 'TCGMTATCGAWTSGC', '>2522963-2522975-reverse St f144 TCGATATCGAATCGC', '>2522963-2522975-reverse St f155 TCGCTATTGAATGCG', '>2522963-2522975-reverse St f204 TCGCTATAAAAAGGC' ] ];

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}