CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Finding if a line does not exist

 



darian
Deleted

Feb 9, 2000, 9:07 AM

Post #1 of 4 (818 views)
Finding if a line does not exist Can't Post

I need to be able to find out if a line does not exist. See the record below for the format. I need to find out if the Subject: line does not exist. I have tried it as:

if($a =~ /^Subject:/)
{ $sub=$';};
else { $sub="no subject"; }

but as you can see this if it reads a line that has the subject in it and then goes to the next line with no subject it will then change $sub to "no subject". I need to say that once it find that subject line to go to the next line of code but if it gets to the end of that record and there was no subject line then it will make $sub ="no subject".

Record Format w/ Subject Line:

To: Joe Shmoe
From: John Doe
Subject:Meeting Tues, 3pm
Body:Meeting my office Tues, 3pm

Record Format w/o Subject Line:

To: Joe Shmoe
From: John Doe
Body:Meeting my office Tues, 3pm


Jasmine
Administrator / Moderator

Feb 8, 2000, 10:08 PM

Post #2 of 4 (818 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 Smile ).

<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!


darian
Deleted

Feb 9, 2000, 11:10 PM

Post #3 of 4 (818 views)
Re: Finding if a line does not exist [In reply to] Can't Post

Thanks Jasmine. It sort of worked. I had to play around with it a bit to make it work for the message format but it was definetly the push in the right direction. Thanks.



japhy
Enthusiast

Feb 10, 2000, 3:57 AM

Post #4 of 4 (818 views)
Re: Finding if a line does not exist [In reply to] Can't Post

Instead of first checking

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


if ($headers =~ /^Subject:/m) {
</pre><HR></BLOCKQUOTE>

And then looping through the lines to extract the subject, you can just say:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$sub = ($headers =~ /^Subject: (.*)/m) ? $1 : "no subject";
</pre><HR></BLOCKQUOTE>

That makes use of the ternary (hook) operator ``?:'' and uses the $DIGIT memory variables, because $`, $&, and $' slow a Perl program down.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives