in reply to Concatenating text for a hash problem

And now, for a totally different solution!

while (<DATA>) { my ($key) = /^>?(\S+)/; local $/ = '>'; chomp(my $val = <DATA>); $val =~ s/\n//g; $hash{$key} = $val; }

Redefining $/ is fun!

Replies are listed 'Best First'.
Re^2: Concatenating text for a hash problem
by deibyz (Hermit) on Oct 15, 2004 at 11:17 UTC
    I have to wake up earlier =). I've come with a similar solution redefining $/, but this time using split. Here is:

    #!/usr/local/bin/perl use strict; use warnings; use Data::Dumper; my %hash; local $/ = ">"; <DATA>; #Skip first ">" while(<DATA>){ my ($key,$value) = split /\n/, $_ , 2; $key =~ s/(\S+).*?$/$1/; $value =~ s/\n\>?//g; $hash{$key} = $value; } print Dumper \%hash; __DATA__ >EP11110 (-) TGCAATCACTAGCAAGCTCTC GCTGCCGTCACTAGCCTGTGG >EP40005 (+) GGGGCTAGGGTTAGTTCTGGA NNNNNNNNNNNNNNNNNNNNN __OUTPUT__ $VAR1 = { 'EP40005' => 'GGGGCTAGGGTTAGTTCTGGANNNNNNNNNNNNNNNNNNNNN', 'EP11110' => 'TGCAATCACTAGCAAGCTCTCGCTGCCGTCACTAGCCTGTGG' };

    Regards,
    deibyz