in reply to If statement issue from a Noob

Do yourself a favour (additionally to care for proper indentation) and use descriptive variable names;
$price = $hashref->{price}; $title = $hashref->{title};
instead of
$tempdesc = $hashref->{price}; $tempdesc2 = $hashref->{title};
Saves you a lot of looking up, wondering and bug hunting.


holli

You can lead your users to water, but alas, you cannot drown them.

Replies are listed 'Best First'.
Re^2: If statement issue from a Noob
by tomdee27 (Initiate) on Oct 04, 2017 at 19:16 UTC

    Thanks to everyone for your suggestions. I was able to use a combination of suggestions to get the solution. I apologize in advance for the bad formatting, but here's what I used:

    $tempdesc = $hashref->{price}; $tempdesc2 = $hashref->{title}; if ($tempdesc >= 55 && $tempdesc2 !~ /electric/) { print FH_OUTFILE "free_batteries\t"; } { print FH_OUTFILE "\n"; }

      Note that the extra set of braces around print FH_OUTFILE "\n"; makes it look at first glance like you're missing an else when you're actually not (since it seems this is based on 1nickt's initial code), I recommend removing them. Also, you should take holli's advice (and as several others have shown) to use more descriptive variable names.

      I apologize in advance for the bad formatting

      Why apologize when you can fix it? ;-) Or at least have perltidy do it for you.

      Still not rightly formatted, the print FH_OUTFILE "free_batteries" should be indented compared to the if statement:
      $tempdesc = $hashref->{price}; $tempdesc2 = $hashref->{title}; if ($tempdesc >= 55 && $tempdesc2 !~ /electric/) { print FH_OUTFILE "free_batteries"; } print FH_OUTFILE "\n";
      The reason may not be very obvious here, but if you have several statements in the body of the if block, then you can make much more visually obvious what depends on the condition in the if statement, and what not:
      if ($tempdesc >= 55 && $tempdesc2 !~ /electric/) { print FH_OUTFILE "free_batteries\n"; print FH_OUTFILE "something else\n"; print "A message displayed on the screen if the condition is satis +fied\n"; } print FH_OUTFILE "\n";
      With this, anyone can see clearly that the three print statements between the curlies should be executed only if the condition is met, while the last print statement after the closing curly will run anyway.

      Besides that, I agree with what other monks have said: use meaningful variable names, use strict; and use warnings;, and declare your variables with the my keyword. Also use lexical file handles. So your full code could end up being something like that:

      use strict; use warnings; # here the code where the $hashref is populated (or maybe it is actual +ly part of a hash of hashes or array of hashes, we don't know enough +details) open my $FH_OUTFILE, ">", "output.txt" of die "could not open the outp +ut file $!"; my $price = $hashref->{price}; my $item_name = $hashref->{title}; if ($price >= 55 and $item_name !~ /electric/) { print $FH_OUTFILE "free_batteries"; } print $FH_OUTFILE "\n"; # perhaps some code to close $FH_OUTFILE (if you're done at this point +)