What you say :
...I am reading a text file with 5 lines in it. Each line contains a variable=value with a new line at the end. Some of these files have a variable that equals nothing, in which case I want the sub sendemailspecial to run. If the two contain values, then sub sendemailnormal.
Unforturnately the if statement does not appear to be working. Thanx for anyone's help and suggestions.
What this seems to me to mean :
I want to parse a file for paired data separated by an equals sign. If I come across a pair where the second datum is undefined, run sendmailspecial.
What you're doing, listed with line numbers, in two parts
Part 1 : Parsing the file
#1 do {
#2 $firstline=<FILE>;
#3 ($name, $value)=split(/=/, $firstline);
#4 $value =~ s/^\s+//;
#5 $value =~ s/\s+$//g;
#6 $value =~ s/\n//g;
#7 print "$name\=$value\n";
#8 $data{$name}=$value;
#9 } while $.<5;
#1 : the do block strikes me as odd. If the file will always contain exactly 5 lines,
just use a while. This do block would be appropriate if you needed to skim off a
header of some sort, but for the data you described, this is more effort than
you need.
#4- #6 : the space removal regexes are fine. Why did you choose to use a regex
to delete newlines rather than using chomp?
#8 : hash storage. I worry that you could clobber a hash entry, but this
probably isn't a concern in your scenario.
Part 2 : Business Logic
#1 if ($data{'SONUMBER'} eq "" || $data{'SALESPERSON'} eq "") {
#2 $data{'SONUMBER'}="No S.O. attached";
#3 $data{'SALESPERSON'}="No salesperson assigned";
#4 &sendemailspecial;
#5 } else{
#6 &sendmailnormal;
#7 }
#8
#9 unlink $currentfile;
#1 : this if statement makes a few assumptions which makes me uncomfortable.
How do you know that SONUMBER and SALESPERSON are really there?
I would use something like :
if ($hash{key}= "" || !defined $hash{key}) or
unless (defined $hash{key} and $hash{key} ne "")
just to make sure that we do the special emailing if the keys aren't there at
all.
#9 : Please close files before unlinking them.
To answer your question, your if statement is probably defective. Make sure you
check for definedness as suggested.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.