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:
Inserting lines into file

 



tbone587
User

Mar 12, 2011, 10:49 PM

Post #1 of 3 (1382 views)
Inserting lines into file Can't Post

I am running a script that takes a specified amount of input, than gathers information from various files and prints information into a new file. The issue I am having is I need it to add the information sequentially into the file, while incrementing. Instead of it printing out in order, it is printing out the data, then the rest of the line, then printing again. The important stuff should be ordered like this:


Code
 
<reg
reg.1.address=""
reg.1.auth.password=""
reg.1.auth.userId=""
reg.1.label=""
reg.1.server.1.address=""
reg.1.outboundProxy.address=""
reg.1.type="private"
reg.1.thirdPartyName=""
reg.1.lineKeys="2"

reg.2.address=""
reg.2.auth.password=""
reg.2.auth.userId=""
reg.2.label="0004f2e729c1"
reg.2.server.1.address=""
reg.2.outboundProxy.address=""
reg.2.type="shared"
reg.2.thirdPartyName=""
reg.2.lineKeys="2"

reg.3.address=""
reg.3.auth.password=""
reg.3.auth.userId=""
reg.3.label=""
reg.3.server.1.address=""
reg.3.outboundProxy.address=""
reg.3.type="shared"
reg.3.thirdPartyName=""
reg.3.lineKeys="2"


Instead I am getting the below output, with it printing the new data, then the rest of the file, and repeats..


Code
<?xml version="1.0" encoding="utf-8" standalone="yes"?> 
<!-- Generated reg-basic.cfg Configuration File -->
<polycomConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="polycomConfig.xsd">

<reg
reg.1.address=""
reg.1.auth.password=""
reg.1.auth.userId=""
reg.1.label=""
reg.1.server.1.address=""
reg.1.outboundProxy.address=""
reg.1.type="private"
reg.1.thirdPartyName=""
reg.1.lineKeys="2"

reg.2.address=""
reg.2.auth.password=""
reg.2.auth.userId=""
reg.2.label="a"
reg.2.server.1.address=""
reg.2.outboundProxy.address=""
reg.2.type="shared"
reg.2.thirdPartyName=""
reg.2.lineKeys="2"



<main mb.main.home="http://xml.voip..net:8080/PolycomB/EvolvePolycomController?Action=MENU&PHONEUSER=6102544040"/>
<efklist efk.efklist.1.action.string="http://xml.voip..net:8080/PolycomB/EvolvePolycomController?Action=CORPDIRMENU&PHONEUSER=6102544040" />

<!--
<tcpIpApp.sntp
tcpIpApp.sntp.address="ntp.ip.net"
tcpIpApp.sntp.gmtOffset="-18000" tcpIpApp.sntp.resyncPeriod="86400" />
-->
<!-- Group= 0001006033 -->
<!-- Platform= -->


</polycomConfig>

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Generated reg-basic.cfg Configuration File -->
<polycomConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="polycomConfig.xsd">

<reg
reg.1.address=""
reg.1.auth.password=""
reg.1.auth.userId=""
reg.1.label=""
reg.1.server.1.address=""
reg.1.outboundProxy.address=""
reg.1.type="private"
reg.1.thirdPartyName=""
reg.1.lineKeys="2"

reg.3.address=""
reg.3.auth.password=""
reg.3.auth.userId=""
reg.3.label="b"
reg.3.server.1.address=""
reg.3.outboundProxy.address=""
reg.3.type="shared"
reg.3.thirdPartyName=""
reg.3.lineKeys="2"



<main mb.main.home="http://xml.voip..net:8080/PolycomB/EvolvePolycomController?Action=MENU&PHONEUSER=6102544040"/>
<efklist efk.efklist.1.action.string="http://xml.voip..net:8080/PolycomB/EvolvePolycomController?Action=CORPDIRMENU&PHONEUSER=6102544040" />

<!--
<tcpIpApp.sntp
tcpIpApp.sntp.address="ntp.ip.net"
tcpIpApp.sntp.gmtOffset="-18000" tcpIpApp.sntp.resyncPeriod="86400" />
-->
<!-- Group= 0001006033 -->
<!-- Platform= -->


</polycomConfig>

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Generated reg-basic.cfg Configuration File -->
<polycomConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="polycomConfig.xsd">

<reg
reg.1.address=""
reg.1.auth.password=""
reg.1.auth.userId=""
reg.1.label=""
reg.1.server.1.address=""
reg.1.outboundProxy.address=""
reg.1.type="private"
reg.1.thirdPartyName=""
reg.1.lineKeys="2"

reg.4.address=""
reg.4.auth.password=""
reg.4.auth.userId=""
reg.4.label="c"
reg.4.server.1.address=""
reg.4.outboundProxy.address=""
reg.4.type="shared"
reg.4.thirdPartyName=""
reg.4.lineKeys="2"



<main mb.main.home="http://xml.voip..net:8080/PolycomB/EvolvePolycomController?Action=MENU&PHONEUSER=6102544040"/>
<efklist efk.efklist.1.action.string="http://xml.voip..net:8080/PolycomB/EvolvePolycomController?Action=CORPDIRMENU&PHONEUSER=6102544040" />

<!--
<tcpIpApp.sntp
tcpIpApp.sntp.address="ntp.ip.net"
tcpIpApp.sntp.gmtOffset="-18000" tcpIpApp.sntp.resyncPeriod="86400" />
-->
<!-- Group= 0001006033 -->
<!-- Platform= -->


My code is below

Code
open EACFGIN2, "<Polycom-user-$EA_Mac.cfg" or die $!; 
my @Exec_Assistant2 = <EACFGIN2>;
close (EACFGIN2);

open EACFGOUT2, ">Polycom-user-$EA_Mac.cfg" or die $!;


my $Reg_Num = 2; #Registration Number
my $Reg_Counter = 0; #Allows the information to be printed from the arrays
my $Reg_Pointer = 1; #Allows the loop to find the proper registration to print the next

for my $a (0..$#Monitored_Users)
{

foreach my $EALine2 ( @Exec_Assistant2 )
{

chomp ($EALine2);

if ($EALine2 =~ /reg.1.lineKeys/)
{
print EACFGOUT2 "$EALine2\n";
print EACFGOUT2 "\n";
print EACFGOUT2 "\t\treg.$Reg_Num.address=\"$Phone_Array[$Reg_Counter].1\"\n";
print EACFGOUT2 "\t\treg.$Reg_Num.auth.password=\"$Sip_Array[$Reg_Counter]\"\n";
print EACFGOUT2 "\t\treg.$Reg_Num.auth.userId=\"$Phone_Array[$Reg_Counter]\"\n";
print EACFGOUT2 "\t\treg.$Reg_Num.label=\"$Monitored_Users[$Reg_Counter]\"\n";
print EACFGOUT2 "\t\treg.$Reg_Num.server.1.address=\"\"\n";
print EACFGOUT2 "\t\treg.$Reg_Num.outboundProxy.address=\"\"\n";
print EACFGOUT2 "\t\treg.$Reg_Num.type=\"shared\"\n";
print EACFGOUT2 "\t\treg.$Reg_Num.thirdPartyName=\"\"\n";
print EACFGOUT2 "\t\treg.$Reg_Num.lineKeys=\"2\"\n\n";

}

else
{
print EACFGOUT2 "$EALine2\n";
}

}

$Reg_Pointer++;
$Reg_Num++;
$Reg_Counter++;

}


close (EACFGOUT2);




Any ideas would be greatly appreciated!


(This post was edited by tbone587 on Mar 12, 2011, 10:53 PM)


Karazam
User

Mar 13, 2011, 8:20 AM

Post #2 of 3 (1369 views)
Re: [tbone587] Inserting lines into file [In reply to] Can't Post

Seems to me the problem is in this part:


Code
                if ($EALine2 =~ /reg.1.lineKeys/)  
{
print EACFGOUT2 "$EALine2\n";
#...
}
else
{
print EACFGOUT2 "$EALine2\n";
}


It is printing each line in the file whether it matches or not.

Couple of points. Remember that in a regex a period matches any character. Escape them to match actual periods.

It's sometimes handy to quote strings with qq() instead of quotes. That way, you don't have to escape quotes within the string. For example:


Code
print qq(\t\treg.Reg_Num.thirdPartyName=""\n);


It seems you are not using the $a in


Code
for my $a (0..$#Monitored_Users)


If that is right, you can just have


Code
for (0..$#Monitored_Users)


Hope this helps. Smile


tbone587
User

Mar 13, 2011, 8:40 AM

Post #3 of 3 (1367 views)
Re: [Karazam] Inserting lines into file [In reply to] Can't Post

I got it working, but thanks for your shortcuts. Its nice to not have to use the extra variable on that for loop. You are the man!

 
 


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

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