Re: Need help with a question.




Quoth Trev <trevor.dodds@xxxxxxxxx>:
On Jun 28, 1:40 pm, Ben Morrow <b...@xxxxxxxxxxxx> wrote:
Thanks Ben for the detailed reply, I've been able to get the output
correct, but I'm stuck on how to read $cpqlogline into an array
without writing it to a file.

my files look like this:

the perl file:

use warnings;
use strict;

my $server_file = "list.txt";
my $mactmp = "mactmp.txt";
my $maclog = "logfile.txt";

sub LoadFile
{
open(my $SRV, '<', $server_file)
or die("Could not open file: $!");

while (my $server = <$SRV>) {
chomp($server);
open (my $DAT, '<', 'output.txt')
|| die("Could not open 'output.txt': $!");

If you reopen output.txt every time you will start at the beginning
again.

open (OTF, ">blah.txt");
print OTF $DAT;

What are you trying to achieve here? You don't ever seem to make use of
the contents of blah.txt.

while (my $cpqlogline = <$DAT>) {
{
chomp($cpqlogline);
if ($cpqlogline =~ /MAC/)
{
$cpqlogline =~ s/ <FIELD NAME="Subject" VALUE="//i;
$cpqlogline =~ s/ <FIELD NAME="MAC" VALUE="//i;
$cpqlogline =~ s/"\/>//i;

open (TMP, ">>$mactmp");
print TMP "$server". "," . "$cpqlogline\n";
close TMP;

You don't need to keep everything in temporary files like this. Perl has
variables for keeping data in.

Here you write all the data into mactmp.txt; in fact, you write a line
for every server with every mac address. I don't think this is what you
meant.

}
}
}
}
close OTF;
}

sub CreateLOG
{
open (TMP, "<$mactmp");
open (LOG, ">>$maclog");
my @lines=<TMP>;
print LOG "$lines[1]";

Here you reopen mactmp.txt, read all the data into @lines, print the
second line (only) to maclog.txt, and throw the rest away. This is why
you only get one record in the log at the end.

I'm not quite sure how to help you at this point. You seem to be missing
several rather fundamental points about how Perl works. If I were
writing this program, I might write it something like this (completely
untested):

#!/usr/bin/perl

my $srvs = 'list.txt';
my $macs = 'output.txt';
my $log = 'logfile.txt';

# Only open each file once. We don't need any temporary files.
open my $SRVS, '<', $srvs or die "can't read '$srvs': $!";
open my $MACS, '<', $macs or die "can't read '$macs': $!";
open my $LOG, '>>', $log or die "can't append to '$log': $!";

# For each line in output.txt...
while (<$MACS>) {

# if it matches the pattern, put the VALUE field in $mac...
my ($mac) = /<FIELD NAME="MAC" VALUE="([^"]+)">/
# otherwise move on to the next line.
or next;

# Get the next server from the list.
my $srv = <$SRVS> or die "Not enough servers.\n";
chomp $srv;

# Write a line to the logfile.
print $LOG "$srv,$mac\n";
}

# Close the logfile explicitly so we can check all the data got
# written safely.
close $LOG or die "writing to '$log' failed: $!";

# Check if there are any servers left.
<$SRVS> and die "Not enough addresses.\n";

__END__

Can you understand what that's doing?

Ben

--
We do not stop playing because we grow old;
we grow old because we stop playing.
ben@xxxxxxxxxxxx
.



Relevant Pages

  • Re: asynchronously writing to a file, a cheap way?
    ... your typical asynchronous route for writing to a file, ... performance when the processing server is running. ... end of the pipe doesn't need to be running. ... I you already have a thread pool or IOCP that you can use t handle the ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Are write() calls guaranteed atomic?
    ... >> writing the data have no knowledge of the FIFO, ... > If you cannot modify the clients, try changing your server to create a ... > Unix domain socket instead of a named pipe (the clients shouldn't see ...
    (freebsd-hackers)
  • Re: Access to Access DB
    ... I am also a softeware trainer in the middle of writing ... side scripted language and rent server space. ... Finding a hosted service for Office is not a ... cost or work and skilling up. ...
    (microsoft.public.access.gettingstarted)
  • Re: Windows SBS 2003 R2 Administrators Campanion
    ... Server MVP - was the Tech Editor, and he is meticulous, checking me at every ... Russ - SBITS.Biz (MCP-SBS) wrote: ... (Writing a book without seeing the product that's cool) ...
    (microsoft.public.windows.server.sbs)