Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Why is my result empty?

by Anonymous Monk
on Mar 20, 2014 at 00:04 UTC ( #1079022=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am trying to use strict perl coding for this simple task, but my variable returns empty. Why?
use strict; my $final_res=''; open FILE, $infile; while(my $final_line=<FILE>) { if($final_line=~/^SeqID/) { my $final_line=<FILE>; chomp $final_line; my @split_final=split(/\t/, $final_line); my $final_res = $split_final[7]; $final_res=~s/D/M/g; $final_res=~s/U/M/g; #print $final_res."\n"; } } close FILE; print $final_res."\n";

If I print inside the loop, in the comment, it prints fine. what am I missing?

Replies are listed 'Best First'.
Re: Why is my result empty?
by toolic (Bishop) on Mar 20, 2014 at 00:14 UTC
      Damn! Silly mistake :)
      Thank you!
Re: Why is my result empty?
by Anonymous Monk on Mar 20, 2014 at 00:18 UTC

    Hi, I am trying to use strict perl coding for this simple task, but my variable returns empty. Why?

    Because you're not Coping with Scoping (you're not Lexical scoping like a fox)

    Consider this program, then ask yourself why you do this in your program

    $ perl -le " use warnings; use strict; my $one = 1; { my $one = 2; pri +nt $one; } print $one; " 2 1

    what am I missing?

    Sample input (and sample output) portion of How do I post a question effectively?

Re: Why is my result empty?
by Anonymous Monk on Mar 20, 2014 at 00:20 UTC

    Because my $final_res = $split_final[7]; creates a new variable that is local to the if block, and it hides the other variable named $final_res that you defined before the loop. Write $final_res = $split_final[7]; (remove the my) and you'll only have one variable named $final_res and your code will do what you intend.

    In general, you should use warnings; in addition to use strict; to help you catch some potential problems. Also, take a look at Perl::Critic, which can help you catch this kind of error.

Re: Why is my result empty?
by Marshall (Monsignor) on Mar 20, 2014 at 05:49 UTC
    Another idea:
    use strict; my $final_res=''; open FILE, $infile; while(my $final_line=<FILE>) { if($final_line=~/^SeqID/) { my $final_line=<FILE>; chomp $final_line; my @split_final=split(/\t/, $final_line); my $final_res = $split_final[7]; $final_res=~s/D/M/g; $final_res=~s/U/M/g; #print $final_res."\n"; } } close FILE; print $final_res."\n"; ################ # different coding ################ use strict; my $infile = ''; #some file open (FILE, "<", $infile) or die "unable to open $infile $!"; while(my $line=<FILE>) { chomp $line; my ($token) = (split(/\t/, $line))[7]; $token=~s/D/M/g; print "Token= ", $token, "\n"; }
Re: OT: Why is my result empty?
by AnomalousMonk (Canon) on Mar 20, 2014 at 16:27 UTC

    Slightly OT, but if you have a lot of such character-for-character translation to do (a bio-app?), consider using  tr/// instead of  s/// (pattern substitution) for faster operation (see  tr/// in Quote-Like Operators in perlop):

    c:\@Work\Perl\monks>perl -wMstrict -le "my $s = 'xxxDxUxxDDxxUUxDUDxUDUx'; print qq{'$s'}; ;; $s =~ tr/DU/M/; print qq{'$s'}; " 'xxxDxUxxDDxxUUxDUDxUDUx' 'xxxMxMxxMMxxMMxMMMxMMMx'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1079022]
Approved by toolic
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2016-07-28 17:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What is your favorite alternate name for a (specific) keyboard key?


















    Results (255 votes). Check out past polls.