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: Intermediate:
Problem with writing to a file with specific conditions

 



Xer()
New User

Feb 25, 2009, 10:07 AM

Post #1 of 8 (922 views)
Problem with writing to a file with specific conditions Can't Post

Ok

So i'm pulling data from another device, which loads into an array called @lines...this output looks something like the below

98 name=XXXXXX state=CP_IDLE mod=N confnum=0 vll=N
99 name=YYYYYY state=CP_IDLE mod=N confnum=0 vll=N

the above all equate to the following variables $lcn, $name, $state, $modStatus, $confNum, and $vll

i currently have the following setup to open a file and print the info for ALL lines that contain vll=Y, i know i will need a condition statement to sift through all lines and weed out the ones with vll=Y, but how can I get them to actually print each line to the file I open?

I think i've been looking at this far to long and now my brain is fried. any help would be appreciated.


1arryb
User

Feb 25, 2009, 10:37 AM

Post #2 of 8 (921 views)
Re: [Xer()] Problem with writing to a file with specific conditions [In reply to] Can't Post

Hi Xer(),

I need a few more details.
1. What do individual @lines records look like? Are they:
a) Strings like '98 name=XXXXXX state=CP_IDLE mod=N confnum=0 vll=N'; or
b) arrays like ('98', 'XXXXX', 'CP_IDLE', 'N', '0', 'N'); or
c) something else?

2. Where is $icn, etc used?

3. What format do you wish to output?

If @lines records are simple strings, just use a regex:

Code
... 
open (OUT, ">$outFile") or die "can't open $outFile for writing.";
map { print OUT "$_\n" if $_ =~ /vll=Y$/ } (@lines);
close OUT;
...


otherwise, you'd have to do something else.

Larry


Xer()
New User

Feb 25, 2009, 11:01 AM

Post #3 of 8 (920 views)
Re: [1arryb] Problem with writing to a file with specific conditions [In reply to] Can't Post

basically it's strings that are "printed output" from another server.

that string output breaks out as the following:


98 name=XXXXXX state=CP_IDLE mod=N confnum=0 vll=N

where
98 = $lcn (stands for Line Number/Port)
name= $name (name of person on that line)
state=$state (the current state of that line/port)
mod=$modStatus (tells me if this person is muted on the phone or not)
confNum=$confnum (tells me where this person's telephone line has terminated on the server)
vll=$vll (tells me whether or not an operator has accidently left a line in a linking mode)

the output of these strings in the array is about 1080 lines; so it basically ranges from line 0 to line 1079...i basically use the variables to sift through the output and tell me or calculate different things.

I just need to take the string and if it shows VLL=Y I need that printed to the file ARGV[0].vll (where i have ARGV[0] being the server name, i have many and run this script against all of them)

I'm trying to display it in the file in the following format under the following condition:

if $vll=~ /Y/;

then print in this format to ARGV[0].vll (i.e. server1.vll)

$lcn $vll
$lcn $vll
$lcn $vll
...and so on..depending on how many return with vll=Y

The end result is another I app that i will write will access this X.vll file and display it to our Operation staff so they can eliminate anything with a Y status for VLL.


1arryb
User

Feb 25, 2009, 11:42 AM

Post #4 of 8 (915 views)
Re: [Xer()] Problem with writing to a file with specific conditions [In reply to] Can't Post

Xer(),

If this won't work, I still don't understand your question:

Code
 
#!/usr/bin/perl

use strict;
use warnings;

my $outFile = $ARGV[0];
open (OUT, "> $outFile") or die "Can't open $outFile for writing.";

my @lines = ();

# Populate @lines somehow. This is just test data.

@lines = (
'98 name=XXXXXX state=CP_IDLE mod=N confnum=0 vll=N',
'19 name=XXXXXX state=CP_IDLE mod=N confnum=0 vll=Y',
'5 name=XXXXXX state=CP_IDLE mod=N confnum=0 vll=Y'
);

# Array of 'vll=y' icns for later output.
# NOTE: If icn repeats within @lines, you'll have to change
# @icns to a hash in order to avoid duplicates in the output:
# my %icns = ();
# ...
# $icns->{$icn} = 1;
# ...
# map { print OUT "$_ Y\n" } (sort { $a <=> $b } (keys(%icns));
my @icns = ();
# Process @lines records
for my $line (@lines) {
# Get rid of the line termination.
chomp($line);

# Split the line in to records on spaces.
my @rec = split(/ /, $line);
my $icn = $rec[0];

# Get the part after 'vll='.
my $vll = (split(/=/, $rec[5]))[1];

# Discard unwanted records.
next unless $vll eq 'Y';

# Store the 'vll=y' icns we'll output.
push(@icns, $icn);
}

# Output in icn-sorted order.
map { print OUT "$_ Y\n" } (sort { $a <=> $b } @icns);

# Fixed!
close OUT;


Cheers,

Larry


(This post was edited by 1arryb on Feb 25, 2009, 3:18 PM)


KevinR
Veteran


Feb 25, 2009, 3:09 PM

Post #5 of 8 (913 views)
Re: [1arryb] Problem with writing to a file with specific conditions [In reply to] Can't Post

Its not going to work if you close the file before trying to print data to it. Wink
-------------------------------------------------


KevinR
Veteran


Feb 25, 2009, 3:15 PM

Post #6 of 8 (912 views)
Re: [Xer()] Problem with writing to a file with specific conditions [In reply to] Can't Post

Borrowing from Larry a little, this can be simplified using grep:


Code
#!/usr/bin/perl  
use strict;
use warnings;
my @lines = (
'98 name=XXXXXX state=CP_IDLE mod=N confnum=0 vll=N',
'19 name=XXXXXX state=CP_IDLE mod=N confnum=0 vll=Y',
'5 name=XXXXXX state=CP_IDLE mod=N confnum=0 vll=Y'
);


my $outFile = $ARGV[0];
open (OUT, ">", $outFile") or die "Can't open $outFile for writing: $!";
print OUT grep {!/vll=Y$/} @lines;
close OUT;


My code assumes there is a newline still on the end of the lines in the array, if not, just print a newline as needed.
-------------------------------------------------


(This post was edited by KevinR on Feb 25, 2009, 3:15 PM)


1arryb
User

Feb 25, 2009, 3:19 PM

Post #7 of 8 (908 views)
Re: [KevinR] Problem with writing to a file with specific conditions [In reply to] Can't Post

Kevin,

Yes, that was my first idea. Something Xer() said made me think some sort of processing was going on with the parsed vars.

Cheers,

Larry


Xer()
New User

Mar 2, 2009, 12:46 PM

Post #8 of 8 (888 views)
Re: [1arryb] Problem with writing to a file with specific conditions [In reply to] Can't Post

Help appreciated both of you...1arry, your first post helped me get to where I needed. although it provides me with all the details in the string, i am able to utilize them with more detailed reporting on the VLL status...

props!!..thanks alot!

 
 


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

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