in reply to Re: Manipulating tab delimited file
in thread Manipulating tab delimited file

Hello, Monks! Please, one more question. I didn't think I should start a new thread for this.
while( defined(my $head = <FASTQIN>) && defined(my $seq = <FASTQIN>) && defined(my $qhead = <FASTQIN>) && defined(my $quality = <FASTQIN>) ){ substr($head, 0, 1, '>'); my $temp = print $head, $seq; } close (FASTQIN); my %count_seq; open my $fh, '<:encoding(UTF-8)', $temp or die "Cannot open $temp +$!"; while (<$fh>) { chomp; next if /^>/; next if length($_) > 30 or length($_) < 15; $count_seq{$_}++; }
this is a section of code I am trying to write. Is there a way to make the variable  my $temp that was used in the while loop also available in the open filehandle statement? The code works OK till  close (FASTQIN);. the error reads:
Use of uninitialized value $temp in open at test.pl line 44. Use of uninitialized value $temp in concatenation (.) or string at tes +t.pl line 44. Cannot open No such file or directory at test.pl line 44.
Line 44 is this:
open my $fh, '<:encoding(UTF-8)', $temp or die "Cannot open $temp $!";
Kindly advise, monks! Thanks!

Replies are listed 'Best First'.
Re^3: Manipulating tab delimited file
by Laurent_R (Canon) on Apr 29, 2016 at 06:25 UTC
    We don't know what your code looks like now, so we don't know where and how you're using the $temp variable, but if it was declared within the while loop, then it no longer exists once the while loop is completed.

    So you could just declare a new my $temp; variable there (although, since you ask the question, these things are probably not entirely clear to you, you might as well use another variable name for clarity).

Re^3: Manipulating tab delimited file
by Athanasius (Archbishop) on Apr 30, 2016 at 06:11 UTC

    Hello andyBio,

    It looks as though your first while loop constructs filename strings and prints them out; then, immediately before the second while loop, you want to open the last constructed filename for writing. If this is what you are doing, there are three problems:

    (1) As Laurent_R says, $temp is out of scope by the time you try to use it in the open statement. To fix this, you need to give it a wider scope:

    ... my $temp; # declare $temp before the loop while (...) { ... } close FASTQIN; my %count_seq; open my $fh, ... # $temp is still in scope ...

    (2) The statement my $temp = print $head, $seq; doesn’t do what you want it to, because print returns a boolean value indicating whether the print operation was successful or not. You need something like this:

    $temp = $head . $seq; print $temp;

    (3) The first while loop’s condition may never be true, in which case $temp will never be initialized; so you should test for this possibility:

    close FASTQIN; my %count_seq; if (defined $temp) { open my $fh, '<:encoding(UTF-8)', $temp or die "Cannot open $temp +$!"; while (<$fh>) { ... } }

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,