DanielM0412 has asked for the wisdom of the Perl Monks concerning the following question:

my %hash = (); my $line = "";

What do those two mean?? why do people do this?

Update

sub readfasta { my $ifn = shift; my $IFH; open($IFH, "<$ifn") or die "cannot open file $ifn\n"; my $line; my %ret; my $tot = 0; while($line = <$IFH> ){ chomp $line; if(not($line eq "")) { ##### why does $line equal "" ? my @tmp = split(//,$line); if($tmp[0] eq ">") { my $nm = substr($line,1); $nm = $nm . $tot; $tot++; $line = <$IFH>; chomp $line; $ret{$nm} = $line; } } } close($IFH); return(\%ret); }

what does $line = ""; mean?

Replies are listed 'Best First'.
Re: need a clearer understanding
by Your Mother (Archbishop) on Aug 09, 2011 at 19:02 UTC

    Initializing hashes or arrays that way doesn’t buy anything. The scalar initialization to an empty string can actually matter if you want to avoid a certain kind of warning–

    moo@cow[91]~>perl -we 'my $s; $s =~ /./' Use of uninitialized value $s in pattern match (m//) at -e line 1. moo@cow[92]~>perl -we 'my $s = ""; $s =~ /./' moo@cow[93]~>

    I prefer no warnings "uninitialized"; for this general case but sometimes you really do want to know if the scalar has an assignment, even the empty string, or not.

      ill update the code

        Please don't update your post without leaving the original question and labeling the update. It makes the original answers seem non-sensical.

Re: need a clearer understanding
by duyet (Friar) on Aug 09, 2011 at 19:49 UTC
    chomp $line; if(not($line eq "")) { ##### why does $line equal "" ?
    $line can be empty after chomp. So the code between the if() doesn't get executed if line is empty.

      > So the code between the if() doesn't get executed if line is empty.

      I'd probably rewrite it this way to kill one indent level:

      next if $line eq "";
Re: need a clearer understanding
by parv (Parson) on Aug 09, 2011 at 19:20 UTC
    ... while($line = <$IFH> ){ chomp $line; if(not($line eq "")) { ##### why does $line equal "" ? my @tmp = split(//,$line); ...

    what does $line = ""; mean?

    If the "#####" comment is anything to go by, then a test for "not empty line" is being done in "not( $line eq "" )".

    In that case your question is actually different than the comment, where you assign an empty string to $line variable.

      I think he means ($line eq "")
Re: need a clearer understanding
by Utilitarian (Vicar) on Aug 10, 2011 at 09:16 UTC
    I suspect that your failure to put the code in your question in a code block is the reason people are finding you hard to answer.
    if($tmp[0] eq ">") { my $nm = substr($line,1); $nm = $nm . $tot; $tot++; $line = <$IFH>; #IS THIS THE LINE YOU MEANT ? chomp $line; $ret{$nm} = $line; }
    If my guess is correct it means exactly the same as the same code in the while ($line = <$IFH>){ line that surrounds the read.

    It reads the next line into the $line variable (If not end of file) within a block which is only run if the previous line began with a ">" character.

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: need a clearer understanding
by locked_user sundialsvc4 (Abbot) on Aug 09, 2011 at 23:29 UTC

    It is really difficult to know where this question is going.   As you can see from perldoc -f chomp, this function removes any “newline” characters from the record that has just been read.   The next statement looks to see if the resulting string is “entirely empty.”   (Notice that it does not consider whether or not the string “consists entirely of spaces,” but that’s another story.)   Beyond that, we really need more clarification and guidance from you...   Help us to help you.