Re: Need help with a question.



On Jun 28, 6:22 pm, Ben Morrow <b...@xxxxxxxxxxxx> wrote:
Quoth Trev <trevor.do...@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.
                            b...@xxxxxxxxxxxx

Hi

I'm only after 1 line (MAC) per server, which is value [1] the other
MAC's I do not need. Is it possible to grep MAC from the Array (Entire
File) and pass the results into a new array which I could then
print[1] to a file. This will then extract 1 line for every server
that I have an xml file for.
.



Relevant Pages

  • Re: "Sorting" assignment
    ... If the array is already sorted, this means that you end up ... less time than a bubble sort, ...     int intLeft, ... I don't declare anything inside of a loop is why. ...
    (comp.programming)
  • Re: #defining an array within another in C language
    ... you do not know the number of elements of the array. ...     int var; ... C99 flexible array members or the plain old "struct hack". ...
    (comp.lang.c)
  • Re: Puppy Mastiff wants to Nip at Faces
    ... in my first college textbook on structured programming. ... they did was loop through an array to show how you could easily access ...   arrays and loops because it makes for less work. ...
    (rec.pets.dogs.behavior)
  • Re: The crux of peoples issues with PLT Scheme?
    ... a last word to the alleged bug in the bigloo code: ... typical array was 67x67x50000, or 7x7x10000, or 7x7x65000. ... real    0m1.944s ... sys     0m0.148s ...
    (comp.lang.scheme)
  • Re: BinaryStream.Write ByteArray erroring with Code 800A0BB9, Source ADODB.Stream
    ... script is reaching out to a SQLServer database and grabbing some data ... BinaryStream.Write method expects a byte array, ...     Else ... I made the changes to the script using your code, ...
    (microsoft.public.scripting.vbscript)