in reply to Anon Struct Instance

Ah, sorry for the confusion, I'll try to clarify. I'll even post all of my code (just don't laugh).

You are right, it is not a reference or meant to be. I'm trying to create an instance of the struct anonymously. The value stored in the scalar $sn (which was in the third element of the array @entry) is going to be the name of the new struct. If that value were K123456, then $K123456 would be the name of the struct. I know this sort of anonymous calling is valid, I've used it before.

The actual names will be stored in a hash as keys. Their value pair is simply a count of how many times they are listed in the log files. In the end, when I want to tabulate and output the data, I will call each key out of the hash (foreach my $key (keys(%Unique_SN))) and then call the DEVICE struct associated with it ($$key).

That much I have done before, but it was with referenced arrays and stuff, never with a struct.

Anyway, here is my code, or what I have of it so far. It is in an early testing stage, so I don't actually do anything with the data, yet.

#!c:\perl\bin\ no strict; use File::DosGlob 'glob'; use Class::Struct; 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\\*")){ open (LOG, "<$logfile") or die "Can't open $logfile: $!"; while(<LOG>){ chomp; s/^\s+//; s/\s+$//; s/\s*,\s*/,/g; s/\[|\]//g; next unless length; my @entry = split(/,/, $_); #split entry: Date Time Devic +eType SN ZIP Version my @date = split(/:/, $entry[0]); #create datetime YYYYMM +DDHHMMSS my @time = split(/:/, $entry[1]); my $datetime = $date[2] . $date[1] . $date[0] . $time[0] . $ti +me[1] . $time[2]; my $sn = $entry[3]; $Unique_DeviceType{$entry[2]}++; #Add entry to unique list + of Device Types $Unique_SN{$sn}++; #Add entry to unique list of SNs if ($Unique_SN{$sn} == 1) { #If this is first occurance $$sn = new DEVICE; $$sn->datetime('$datetime'); $$sn->devicetype('$entry[2]'); $$sn->zip('$entry[4]'); $$sn->version('$entry[5]'); } elsif ($datetime > $$sn->datetime) { $$sn->datetime('$datetime'); $$sn->zip('$entry[4]'); $$sn->version('$entry[5]'); } } } foreach my $key (keys(%Unique_SN)){ print $key . "\n"; }

Replies are listed 'Best First'.
RE: Re: Anon Struct Instance
by ferrency (Deacon) on Jul 11, 2000 at 20:13 UTC
    Ah, so basically, you're trying to assign the DEVICE to a symbolically referenced scalar, whose name is coming from $entry[3]. I would recommend against doing this, and for using a hash instead, to store these DEVICEs. Like this:
    $devices{$sn} = new DEVICE; $devices{$sn}->datetime{$datetime}; # etc.
    If you rearranged your data slightly, you could get rid of one hash.
    # I've snipped out what I consider to be the important bits... # Warning: This is untested code! # First, store the sn count inside the DEVICE, since we have one DEVIC +E per $sn struct ( DEVICE => { datetime => '$', devicetype => '$', zip => '$', v +ersion => '$', sn_count => '$'}); # Get the $sn and other fields here # If this is the first time we see $sn, populate the DEVICE if (!exists $SNs{$sn}) { $SNs{$sn} = new DEVICE; $SNs{$sn}->datetime('$datetime'); # etc. } # update the sn_count for this $sn. $SNs{$sn}->sn_count($SNs{$sn}->sn_count + 1); foreach my $key (keys %SNs) { print "$key\n"; }
    What is your motivation for storing things in symbolically referenced scalars instead of hash elements?

    Alan