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

Dear Masters,
How can I get my code below
use Data::Dumper; my %bighash; my $motif; my $score; my %hoa; my $tag; while ( <DATA> ) { chomp; my @aoa; if ( /^TAG/ ) { $tag = (split(/: /,$_))[1]; } elsif(/^MOTIF/) { ($motif,$score)=(split (/[\s,]/,$_))[1,3]; push @{$hoa{$motif}}, $score; } elsif(/^\d,-\d+,[ATCG]*$/) { @aoa = split(/,/,$_); push @{$hoa{$motif}}, [ @aoa ]; } #print Dumper \%hoa ; $bighash{$tag} = { %hoa }; } # ----- end while ----- print Dumper \%bighash; __DATA__ TAG: Set1 >instances MOTIF: CTTTTTAG, 10.6222222222222 Submotif: CTTTT TTTAT 2,-76,CTTTTTAG 2,-77,TCTTTTTA 3,-60,ACTTTTTG Decompose: 2,-16,CGTTTTTT CGTTT GTTTT TTTTT 1,-72,AATTTTAC ATTTT TTTTA TTTAC 2,-76,CTTTTTAG CTTTT TTTTT TTTTA TTTAG ============= MOTIF: CTTTTATC, 9 Submotif: GTTTT TTTTT 2,-76,CTTTTTAG Decompose: 2,-76,CTTTTTAG CTTTT TTTTT ============= TAG: Set2 >instances TAG: Set3 >instances MOTIF: TAGTTGAAAAAAATT, 17.8181818181818 Submotif: AAAAA AAATA AAATT 1,-49,CACACAAAAAATAAA 1,-82,AGTTGAAAAAAATTT Decompose: 1,-49,CACACAAAAAATAAA AAAAA AAAAA AAAAT AAATA 1,-82,AGTTGAAAAAAATTT AAAAA AAAAA AAAAA AAAAT AAATT AATTT =============
to capture "TAG: Set2" area that has no value. So that it gives something like this:
$VAR1 = { 'Set1' => { 'CTTTTTAG' => [ '10.6222222222222', [ '2', '-76', 'CTTTTTAG' ], [ '2', '-77', 'TCTTTTTA' ], [ '3', '-60', 'ACTTTTTG' ] ], 'CTTTTATC' => [ '9', [ '2', '-76', 'CTTTTTAG' ] ] }, 'Set3' => { 'TAGTTGAAAAAAATT' => [ '17.8181818181818', [ '1', '-49', 'CACACAAAAAATAAA' ], [ '1', '-82', 'AGTTGAAAAAAATTT' ] ], 'Set2' => { } };

Replies are listed 'Best First'.
Re: Capturing No-values Region in Regex match
by tlm (Prior) on Jul 27, 2005 at 04:44 UTC

    Try this:

    my %bighash; my $motif; my $score; my $hoa; # formerly %hoa my $tag; while ( <DATA> ) { chomp; my @aoa; if ( /^TAG/ ) { $tag = (split(/: /,$_))[1]; die "No tag!" unless $tag; $bighash{ $tag } = $hoa = +{}; } elsif(/^MOTIF/) { ($motif,$score)=(split (/[\s,]/,$_))[1,3]; push @{$hoa->{$motif}}, $score; } elsif(/^\d,-\d+,[ATCG]*$/) { @aoa = split(/,/,$_); push @{$hoa->{$motif}}, [ @aoa ]; } #print Dumper $hoa ; } # ----- end while -----
    Note that I turned %hoa into $hoa; I think it's easier to work with hashrefs in this case.

    the lowliest monk