Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading files and regex, Newb at Perl

Status
Not open for further replies.

CoffeeFrapp

Technical User
Jun 18, 2009
4
US
I'm trying to write a program that reads the 27th line of a file(chuck), then depending if the line contains a command(hello) then have it print a line(Goodbye). I've tried to find examples of how to do this but its never worked properly. If somebody could just show me how this would work using the things in parenthesis it would greatly be appreciated
 
What have you tried? Even if it's not working, post some code.
 
It may seem like homework because i have no idea what im doing, and i don't want to look stupid because of how retarded my code looks lol. I've tried a few things including
Code:
open(INBOX,'/var/spool/mail/chuck') or die "$!";
$line = 0;
while(<INBOX>) {
if ($line >= 27) {
my $command == $_;
print "$_ success";
} 
else {
print "Failure\n";
}
}
and
Code:
open(INBOX,'/var/spool/mail/chuck') or die "No new Email Commands";
 while (<INBOX>) {
chomp($line) if 27 .. 27;
my ($command) = $_;
}
These were just trying to test if i could figure out how to read the file
 
Code:
open(INBOX,'/var/spool/mail/chuck') or die "$!";
<INBOX> for (1..26);
chomp(my $line 27 = <INBOX>);
if (lc($line) eq 'hello') {
   print "Goodbye\n";
}
else {
   print "Now what?\n";
}
close INBOX;

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
this line:

chomp(my $line 27 = <INBOX>);

should be:

chomp(my $line = <INBOX>);



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thank you that works beautifully. If you were wondering what I'm trying to do is write a program to perform system commands via SMS texts/emails
 
How so? Every time it runs it clears the file, and the message always is on the same line, and if the keyword doesn't match any command it returns an error
 
Hi

Mail files are composed by variable number of header lines, a blank line, then the body. There is no reason to suppose that the number of header lines will be always the same. Even more, header lines may be physically wrapped to multiple lines, or not.

I suppose the command is in the mail body. So better search for separator blank line, and your command will be on next line.

Based on Kevin's code I would do it like this :
Perl:
[b]open[/b][teal]([/teal]INBOX[teal],[/teal][green][i]'/var/spool/mail/chuck'[/i][/green][teal])[/teal] or [b]die[/b] [green][i]"$!"[/i][/green][teal];[/teal]

[b]do[/b] [teal]{[/teal]
  [b]chomp[/b][teal]([/teal][navy]$line[/navy][teal]=[/teal][green][i]<INBOX>[/i][/green][teal]);[/teal]
[teal]}[/teal] [b]while[/b] [teal]([/teal][navy]$line[/navy] ne [green][i]''[/i][/green][teal]);[/teal]

[b]chomp[/b][teal]([/teal][navy]$line[/navy][teal]=[/teal][green][i]<INBOX>[/i][/green][teal]);[/teal]

[b]if[/b] [teal]([/teal][b]lc[/b][teal]([/teal][navy]$line[/navy][teal])[/teal] [b]eq[/b] [green][i]'hello'[/i][/green][teal])[/teal] [teal]{[/teal]
  [b]print[/b] [green][i]"Goodbye\n"[/i][/green][teal];[/teal]
[teal]}[/teal] [b]else[/b] [teal]{[/teal]
  [b]print[/b] [green][i]"Now what?\n"[/i][/green][teal];[/teal]
[teal]}[/teal]
[b]close[/b] INBOX[teal];[/teal]

Feherke.
 
I agree it sounds like a bad idea for a general application but it could work for a specific application.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I meant to post this earlier - guess I didn't.

You can also look at the $. special variable. It contains the line number from the last filehandle that was read.
 
CoffeeFrapp said:
If you were wondering what I'm trying to do is write a program to perform system commands via SMS texts/emails
Please tell me this doesn't run as root...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top