Edward Wijaya wrote:
> Hi,
> 
> I have a file that contain this:
> 
> GCTTGACGG: GCTTGACGA,GCTTGACCG,GCTTGACGA
> CTTGAAGAG: CTTGACGAG,CTTGACGAG,CTTGACGAG,CTTGACGAG,CTTGAAGAA
> CTTGACGAA: CTTGACGAG,CTTGACGAG,CTTGACGAG,CTTGACGAG
> 
> My problem is that I want to create hash of and array.
> Where the 'word' to the left of the colon is the hash name
> and the 'words that comes with commas' after the colon
> is the element.
> 
> Is there any efficient way to do this?
> Thanks so much for your time.
> Hope to hear from you again.
        If you have the control as stated, here is a start for you:
use strict;
use warnings;
my %MHA = ();
my $MyHashArray = \%MHA;
{   # start of bracket a

    my $MyKey;
    my $MyData;
    
    while ( <DATA> ) {
        chomp;
        next if ( /^\s*$/ );
        ($MyKey, $MyData) = split(/\s+/, $_);
        $MyKey =~ s/:$//g;
        $MyHashArray->{$MyKey} = [split(/,/, $MyData)];
     }
 }   # end of bracket a

{   # start of bracket b
    my $MyLineMax = 6;
    my $MyLineCnt = 0;
    my $MyLinePrt ;
    
    foreach my $MyKey ( sort keys %{$MyHashArray}) {
        printf "%s:\n", $MyKey;
        $MyLinePrt = '  ';
        foreach my $MyArrItem (@{$MyHashArray->{$MyKey}}) {
            $MyLinePrt .= sprintf "%s, ", $MyArrItem;
            if ( ++$MyLineCnt > $MyLineMax ) {
                printf "%s\n", $MyLinePrt;
                $MyLinePrt = '  ';
                $MyLineCnt = 0;
             }
         }
        if ( $MyLineCnt ) {
            printf "%s\n", $MyLinePrt;
            $MyLinePrt = '  ';
            $MyLineCnt = 0;
         }
     }
 }   # end of bracket b

Output:
CTTGAAGAG:
  CTTGACGAG, CTTGACGAG, CTTGACGAG, CTTGACGAG, CTTGAAGAA,
CTTGACGAA:
  CTTGACGAG, CTTGACGAG, CTTGACGAG, CTTGACGAG,
GCTTGACGG:
  GCTTGACGA, GCTTGACCG, GCTTGACGA,

Wags ;)

> 
> Regards
> Edward WIJAYA
> SINGAPORE


*******************************************************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
*******************************************************


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to