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:
Tripping Over My Own Loop (While Loop)

 



preston3271
Novice

Apr 10, 2013, 12:37 PM

Post #1 of 3 (284 views)
Tripping Over My Own Loop (While Loop) Can't Post

Hello, all:

I am working on a file update module and I am having trouble with a basic while loop. Here is my module:

file: editHost.pm

Code
package editHost; 


sub new {
my $class = shift;;
my ($ipaddr, $lgName, $stName, $file) = @_;
my $ref = {
"IP Address" => $ipaddr,
"Long Name" => $lgName,
"Short Name" => $stName,
"File" => $file

};
bless($ref, $class);
}

sub display {
my $self = shift;
foreach $key ( @_ )
{ print "$key: $self->{$key}\n"; }

}

sub update {
my $self = shift;
#Build a search that will look through file for
#matching IP Address, Long Names, or Short Names
#before making changes.

open(FH, $self->{"File"}) || die "Can't open file: $!\n";
while(<FH>) {
print "Now beginning new entry insert . . . \n" if /lnmtmytest2/;
}
close(FH);
}

sub check {
my $self = shift;
#Build a search that will look through file for
#matching IP Address, Long Names, or Short Names
#before making changes.

open(FH, $self->{"File"}) || die "Can't open file: $!\n";
while(<FH>) {
print "IP Entry element already in file. \n" if (/$self->{"IP Address"}/ || /$self->{"Long Name"}/ || /$self->{"Short Name"}/);

#print if /lnmtmytest2/;
}


file: mytest.pl


Code
#!/bin/perl 
use editHost;

my $newEntry = editHost->new("10.100.20.1", "lnmtmytest.e911.net", "lnmtmytest", "file001.txt");
$newEntry->display("IP Address", "Long Name", "Short Name");
$newEntry->update("File");
$newEntry->check("IP Address", "Long Name", "Short Name", "File");


Output:

Code
 
root@incoov01>/export/home/lthornto # ./mytest.pl
IP Address: 10.100.20.1
Long Name: lnmtmytest.e911.net
Short Name: lnmtmytest
Now beginning new entry insert . . .
IP Entry element already in file.
IP Entry element already in file.
IP Entry element already in file.
root@incoov01>/export/home/lthornto #


Okay, so what I want is a single line of "IP Entry element already in file, assuming that it ready all the way through the file. Right now it returns three lines because of the match / / which I think is a =~ find similar. Anyway, it a basic concept issue I need to get clear on.

Thanks.


FishMonger
Veteran / Moderator

Apr 10, 2013, 1:11 PM

Post #2 of 3 (280 views)
Re: [preston3271] Tripping Over My Own Loop (While Loop) [In reply to] Can't Post

Your question is not very clear, but see if this does what you want.

change:

Code
print "IP Entry element already in file. \n" if (/$self->{"IP Address"}/ || /$self->{"Long Name"}/ || /$self->{"Short Name"}/);


to:

Code
print "IP Entry element already in file. \n" and last if (/$self->{"IP Address"}/ || /$self->{"Long Name"}/ || /$self->{"Short Name"}/);



preston3271
Novice

Apr 10, 2013, 1:22 PM

Post #3 of 3 (276 views)
Re: [preston3271] Tripping Over My Own Loop (While Loop) [In reply to] Can't Post

I just wanted a single line of output. Your answer is correct and what I was looking for. Thank you.

 
 


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

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