THuG has asked for the wisdom of the Perl Monks concerning the following question:
Seems I am having way too much fun with anonymous stuff.
What I am doing is reading through a bunch of log files. They are all CSV with date, time, device type, serial number, ZIP, and version. I am looking for the most recent entry for each serial number, so I can log its last upload time and its current version. Then I want to spit all of this data out into a file for each device type.
The script was working just fine, but I was looping through the device types found and opening a file, then looping through the list of serial numbers and checking if it was the type of device I wanted.
If I had three different types of devices, then I had to loop through the serial number list three times.
What I want to do is open each of the file handles anonymously, and then run through the list of serial numbers once, and then close the files.
Mind you, I'm doing this because I'm somewhat bored and am just trying to make it better for the sake of being better, and learning more Perl.
Here is what I have:
#!c:\perl\bin\ use strict; use Class::Struct; use File::DosGlob 'glob'; use FileHandle; use Getopt::Std; use vars qw($opt_a); getopts('a:'); my $logPath = "\\\\" . $opt_a . "\\d\$\\iims\\history"; print "Searching $logPath\n"; struct ( DEVICE => { datetime => '$', devicetype => '$', zip => '$', v +ersion => '$'}); my %Unique_DeviceType; my %Unique_SN; foreach my $logfile (glob("$logPath\\*.log")){ open (LOG, "<$logfile") or die "Can't open $logfile: $!"; while(<LOG>){ chomp; #pul +l off newline s/^\s+//; #pull + out leading whitespace s/\s+$//; #pull + out trailing whitespace s/\s*,\s*/,/g; #pul +l out whitespace around commas s/\[|\]//g; #re +move [ ] around date time stamp next unless length; #mo +ve to the next line unless there is something left in this one my @entry = split(/,/, $_); #sp +lit entry: Date Time DeviceType SN ZIP Version my @date = split(/:/, $entry[0]); #crea +te datetime YYYYMMDDHHMMSS my @time = split(/:/, $entry[1]); my $datetime = $date[2] . $date[1] . $date[0] . $time[0] . $ti +me[1] . $time[2]; if (!exists $Unique_DeviceType{$entry[2]}){ #Ad +d entry to unique list of Device Types $Unique_DeviceType{$entry[2]} = new FileHandle; open $Unique_DeviceType{$entry[2]}, ">$entry[2].csv"; } if (!exists $Unique_SN{$entry[3]}) { #If th +is is first occurance $Unique_SN{$entry[3]} = new DEVICE; #Ad +d entry to unique list of SNs $Unique_SN{$entry[3]}->datetime($datetime); $Unique_SN{$entry[3]}->devicetype($entry[2]); $Unique_SN{$entry[3]}->zip($entry[4]); $Unique_SN{$entry[3]}->version($entry[5]); } elsif ($datetime > $Unique_SN{$entry[3]}->datetime) { #If t +his occurance is newer $Unique_SN{$entry[3]}->datetime($datetime); #up +date its info $Unique_SN{$entry[3]}->zip($entry[4]); $Unique_SN{$entry[3]}->version($entry[5]); } } close (LOG); } foreach my $sn (keys(%Unique_SN)){ print $Unique_DeviceType{$Unique_SN{$sn}->devicetype} join(",", $U +nique_SN{$sn}->datetime, $sn, $Unique_SN{$sn}->zip, $Unique_SN{$sn}-> +version); #ERROR LINE print $Unique_DeviceType{$Unique_SN{$sn}->devicetype} "\n"; #ER +ROR LINE } foreach my $devicetype (keys(%Unique_DeviceType)){ close $Unique_DeviceType{$devicetype}; } #foreach my $devicetype (keys(%Unique_DeviceType)){ # open (LIST, ">$devicetype.csv") or die "can't open $devicetype.cs +v: $!"; # foreach my $sn (keys(%Unique_SN)){ # if($Unique_SN{$sn}->devicetype eq $devicetype){ # print LIST join(",", $Unique_SN{$sn}->datetime, $sn, $Uni +que_SN{$sn}->zip, $Unique_SN{$sn}->version); # print LIST "\n"; # } # } # close (LIST); #}
The commented code at the bottom is the old way of doing it.
Currently it gets an error when it parses the print line. Syntax error between the } and join.
Funny, if I put a comma there, it will print to the screen the pointer to the filehandle and the data. So I suspect the filehandle if valid. It even touches the files in the directory.
-Travis
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Anon FH
by btrott (Parson) on Jul 12, 2000 at 20:23 UTC | |
|
Re: Anon FH -Thank you
by THuG (Beadle) on Jul 12, 2000 at 21:56 UTC |