in reply to Uninitialized value in substr command

Well, if the array already exists, maybe try this (untested):
my $string = ""; my @new_array; for my $item (@contigfile) { if ( $item =~ /^>\d) { push @new_array, $string unless $string eq ""; $string = ""; } else { $string .= $item; } } push @new_array, $string; # takes care of the last element
If you have a file, you should probably avoid using a temporary array (at least if you file is big), but rather do essentially the same thing directly in a while loop reading the file:
my $string = ""; while (my $line = <$input_fh>) { chomp $line; if ( $line =~ /^>\d) { push @new_array, $string unless $string eq "";; $string = ""; } else { $string .= $line; } } push @new_array, $string; # take care of the last element
Update: fixed the typo indicated by Lotus1 (in the post just below) in the regex (I had inadvertently forgotten the '>'). Thanks to Lotus1.

Replies are listed 'Best First'.
Re^2: Uninitialized value in substr command
by Lotus1 (Vicar) on Aug 21, 2014 at 14:06 UTC

    Laurent_R: I tested your code and got it working after fixing a typo in your regex.

        if ( $item =~ /^>\d/ ) {
Re^2: Uninitialized value in substr command
by lmtaylor (Novice) on Aug 21, 2014 at 17:44 UTC
    Thanks! I had to look up a few bits of your code to see what they meant, but now that I have I see that's a smart way to do it.