
Jasmine
Administrator
/ Moderator
Feb 8, 2000, 10:08 PM
Post #2 of 4
(305 views)
|
|
Re: Finding if a line does not exist
[In reply to]
|
Can't Post
|
|
The way you handle this depends on how the message is stored. For example, if your message is stored in a multilined scalar variable, you can use the m search modified. The m modifier allows multiple lines in the string. In the example below, the search looks for Subject at the beginning of each line in the scalar, then splits the scalar into an array so you can extract the subject from the message if a subject is found. If a subject is not found in the multiline scalar, $subject gets defined as "No Subject". Some may protest that it's easier to split the scalar into an array first and just treat it like my second example below, but I think it's more efficient to spend the time splitting only if a subject line is found, and not before. (I'm sure there's an easier way to do this, but I haven't had my second cup of coffee yet ). <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> my $record1 = "To: Joe Shmoe From: John Doe Subject:Meeting Tues, 3pm Body:Meeting my office Tues, 3pm"; my $found = 0; if ($record1 =~ /^Subject:/m){ my @record1 = split(/\n/,$record1); foreach (@record1){ if (/^Subject:/){ $found = 1; $subject = $_; last; } } } else { $subject = "No Subject"; } print $subject; </pre><HR></BLOCKQUOTE> If the message is in an array, it's just a matter of looping through the array to see if the subject line exists. <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> my @record2 = ("To: Joe Shmoe","From: John Doe","Body:Meeting my office Tues, 3pm"); my $found = 0; foreach (@record2){ if (/^Subject:/){ $found = 1; $subject = $_; last; } } unless ($found){ $subject = "No Subject"; } print $subject;</pre><HR></BLOCKQUOTE> Both of the above examples use a $found scalar. So if the subject is found, the initialized false value (0) of $found is changed to true (1). Hope this helps!
|