in reply to Re^3: An overlapping regex capture
in thread An overlapping regex capture

Okay, so I tried to implement that as so:

foreach $id (keys %id2seq){ my $filename = (split /\|/, $id)[0]; open my $out_fh, '>>', "$filename.fa" or die $!; print $out_fh (">".$id."\n",$id2seq{$id}, "\n"); close $out_fh; }
But I got an error saying that $filename was uninitialized at the 3rd line down ---  open  my $out_fh, '>>', "$filename.fa" or die $!;

So I'm just trying to figure out where I should be declaring  $filename

Pete.

Replies are listed 'Best First'.
Re^5: An overlapping regex capture
by 1nickt (Canon) on Jun 21, 2017 at 22:11 UTC

    First, follow the advice given. Go through your script and correct *all* the items Discipulus pointed out. Second, when in doubt, print out the value of your variables.

    use Data::Dumper; ... foreach my $id (keys %id2seq) { warn "ID: $id"; my @segments = split /\|/, $id; warn "Segments: " . Dumper \@segments; my $filename = $segments[0]; ... }
    Once the code is working right, remove the debug statements.


    The way forward always starts with a minimal test.
Re^5: An overlapping regex capture
by hexcoder (Curate) on Jun 22, 2017 at 11:22 UTC
    No, you got the error saying that $filename.fa is uninitialized.

    So perl saw that as a variable name while you intended to only use $filename instead.

    In order to delimit the variable name you can use this syntax: "${filename}.fa".

    Good luck, hexcoder
      No, you got the error saying that $filename.fa is uninitialized. So perl saw that as a variable name ...

      Sorry, but that's not correct in this case, a dot does end the variable name being interpolated. As the OP is using strict, that would have caught the error anyway. Your advice does apply for other characters though.

      $ perl -wMstrict -le 'my $fn="x"; print "$fn.y"' x.y $ perl -wMstrict -le 'my $fn="x"; print "$fn_y"' Global symbol "$fn_y" requires explicit package name (did you forget t +o declare "my $fn_y"?) at -e line 1. Execution of -e aborted due to compilation errors. $ perl -w -le 'my $fn="x"; print "$fn_y"' Name "main::fn_y" used only once: possible typo at -e line 1. Use of uninitialized value $fn_y in string at -e line 1. $ perl -wMstrict -le 'my $fn="x"; print "${fn}_y"' x_y