Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks.

I have the following data structure via data dumper:

$VAR1 = { 'CSTRING' => 'public', 'SPECIFIC' => '1', 'GENERIC' => '6', 'SSERVER' => 'test1', 'DATE' => 'Wed Feb 26 11:46:00 2003', 'OID' => '1.2.3.4.6', 'DEVICE' => '123.124.125.126', 'TRAPS' => { 'TRAP_LIST' => [ { 'TYPE' => 2, 'DATA' => '5', 'VARBIND' => '1.1.2.5.26' } ] }, 'PORT' => '162' }; $VAR2 = $VAR1; $VAR3 = { 'CSTRING' => 'public', 'SPECIFIC' => '0', 'GENERIC' => '3', 'SSERVER' => 'test1', 'DATE' => 'Wed Feb 26 11:13:11 2003', 'OID' => '1.3.6.1.4', 'DEVICE' => '124.125.126.128', 'TRAPS' => { 'TRAP_LIST' => [ { 'TYPE' => '2', 'DATA' => '12', 'VARBIND' => '1.3.6.1.2.1.2. +1.1' }, { 'TYPE' => '2', 'DATA' => '20', 'VARBIND' => '1.3.6.1.2.1.2. +1.1' }, { 'TYPE' => '2', 'DATA' => '22', 'VARBIND' => '1.3.6.1.2.1.2. +1.7' }, { 'TYPE' => '2', 'DATA' => '32', 'VARBIND' => '1.3.6.1.2.1.2. +1.8' } ] }, 'PORT' => '162' };
How can I get rid of $var2 = $var1?
or even better how can I check for that to prevent it?

This is the code that generates the above

my @temp = (); foreach (@log_file) { chomp; next if ($_ =~ /^\s*$/); if ($_ =~ /^Date:\s+(.+)$/) { push(@temp, $hash) if (defined($hash)); $hash = {DATE => $1}; } if ($_ =~ /^SServer:\s+(.+)$/) { $hash->{SSERVER} = $1; } elsif ($_ =~ /^Device:\s+(.+)$/) { $hash->{DEVICE} = $1; } elsif ($_ =~ /^CString:\s+(.+)$/) { $hash->{CSTRING} = $1; } elsif ($_ =~ /^Port:\s+(.+)$/) { $hash->{PORT} = $1; } elsif ($_ =~ /^SNMP Traps Generated:/) { $hash->{TRAPS} = {TRAP_LIST => []}; } elsif ($_ =~ /^\s+Trap OID:\s+(.+)$/) { $hash->{OID} = $1; } elsif ($_ =~ /^\s+Generic:\s+(.+)$/) { $hash->{GENERIC} = $1; } elsif ($_ =~ /^\s+Specific:\s+(.+)$/) { $hash->{SPECIFIC} = $1; } elsif ($_ =~ /^\s+Varbind:\s+(.+)$/) { $trap = {VARBIND => $1}; } elsif ($_ =~ /^\s+Type:\s+(.+)$/) { $trap->{TYPE} = $1; } elsif ($_ =~ /^\s+Data:\s+(.+)$/) { $trap->{DATA} = $1; push(@{$hash->{TRAPS}->{TRAP_LIST}}, $trap); } } push (@temp, $hash); # if (defined($hash)); push @lookup, @temp;
I only run into this problem when @lookup has data prior to running this sub-routine.
Any ideas?

Replies are listed 'Best First'.
Re: Array splice ??
by xmath (Hermit) on Feb 26, 2003 at 17:11 UTC
    Ehm, from what I can tell, you're pushing the same hash onto @lookup more than once. This could happen if the file does not contain a Date: line, and therefore $hash isn't being reset (for empty an empty file)

    To prevent this kind of errors, you should lexically scope your variables using my (see perldoc -f my)

      Thanks xmath

      That was it, after lexically scoping $hash it worked fine.
      Good call, Thanks again!

Re: Array splice ??
by Abigail-II (Bishop) on Feb 26, 2003 at 17:17 UTC
    Well, your call to Data::Dumper is missing, so it's very hard to say anything about it.

    But this looks wierd:

    push (@temp, $hash); push @lookup, @temp;

    That happens in a loop. Inside the loop, @temp isn't cleared. So, if you go through the loop twice, the first hash will pushed to @lookup twice. If you go three times through the loop, in total 6 times a hash will be pushed on @lookup, in the order: 1, 1, 2, 1, 2, 3. And after a fourth iteration, @lookup will have: 1, 1, 2, 1, 2, 3, 1, 2, 3, 4 as pushed hashes. Unless you have lines matching "Date", then $hash is pushed an extra time on @temp.

    Oh, to further complicate things, there's just one $hash. Nothing local or lexical to the loop.

    Abigail

      Abigail is right, your Data::Dumper call is missing from your post... you might also try the following method to dump your data:

      #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @array = qw( test this now); my %hash = ( one => 1, two => 2); print Data::Dumper->Dump([\%hash, \@array], [qw(*hash *array)]);

      cp
      ---
      "Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic."
Re: Array splice ??
by xmath (Hermit) on Feb 26, 2003 at 17:44 UTC
    As others pointed out you haven't explained the context very clearly, however here's an example of how you can parse a logfile similar to what you show:
    use strict; use warnings; use Data::Dumper; sub readlogfile { my ($filename) = @_; open my $logfile, '<', $filename or die "open $filename: $!"; my (@lookup, $entry, $trap); while (<$logfile>) { chomp; if ( /^Date:\s+(.+)$/ ) { push @lookup, ($entry = { DATE => $1 }); undef $trap; } elsif ( !$entry ) { next; } elsif ( /^(SServer|Device|CString|Port):\s+(.+)$/ ) { $entry->{uc $1} = $2; } elsif ( /^\s+(Trap OID|Generic|Specific|Varbind|Type):\s+(.+ +)$/ ) { $trap->{uc $1} = $2; } elsif ( /^\s+Data:\s+(.+)$/ ) { push @{$entry->{TRAPS}{TRAP_LIST}}, $trap if $trap; $trap = { DATA => $1 }; } } return @lookup; } print Dumper readlogfile("logfile.txt");
    I've rudely assumed "Trap OID", "Generic", and "Specific" actually belong in a trap rather than the entry. If that's not the case you can easily edit it ofcourse.

    In any case I hope this is useful for solving your problem