Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Global vars not accessable globaly

by theroninwins (Friar)
on Sep 20, 2004 at 08:34 UTC ( [id://392305]=perlquestion: print w/replies, xml ) Need Help??

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

Hello monks!

Here is my little problem:
the array final1 and final2 are declared as global but i can only print them in there function. the print @final1 just below that and the print at the end do not work.....

#!/usr/bin/perl -w use Net::SNMP qw(oid_lex_sort oid_base_match SNMP_VERSION_1 DEBUG_ALL) +; use strict; use Getopt::Std; use warnings; use diagnostics; use XML::Simple; use Data::Dumper; use vars qw($ip $name @serials @parts $response $response2 @final1 @fi +nal2); #open xml file my $xmlfile = $ARGV[0]; my $ref = eval { XMLin($xmlfile) }; #erase or creat the ipfile and the namefile open ERASER, ">ipfile.txt"; close ERASER; open ERASER, ">namefile.txt"; close ERASER; #see if open worked if ($@) { print "XML Read ERROR"; } else { #go to IPAddress tag and read infos into file foreach my $itemip (@{$ref->{Layer2Details}->{Device}}){ my @ipliste = $itemip->{IPAddress}; my @sorted = @ipliste; open OUTIP, ">>ipfile.txt"; print OUTIP @sorted, "\n"; close OUTIP; } #go to DeviceName tag and read infos into file foreach my $itemname (@{$ref->{Layer2Details}->{Device}}){ my @nameliste = $itemname->{DeviceName}; my @sortedname = @nameliste; open OUTNAME, ">>namefile.txt"; print OUTNAME @sortedname, "\n"; close OUTNAME; } } #open ipfile open IPFILE, "ipfile2.txt" or die "Can't get IPs - $!\n"; open NAMES, "namefile2.txt" or die "Can't get IPs - $!\n"; open ERASER, ">finallist.txt"; close ERASER; + #Sets knots und community my $community = 'public'; my $ifSer = '.1.3.6.1.2.1.47.1.1.1.1.11'; my $ifPart = '.1.3.6.1.2.1.47.1.1.1.1.13'; while ( $ip = <IPFILE>, $name = <NAMES> ) { chomp $ip; chomp $name; $name =~ s/\.bc\.de\.bic$//; print "Got: $ip\n"; + #open session my ( $s, $e ) = Net::SNMP->session( -hostname => $ip, -community => $community, -port => 161 ); if (!defined($s)) { print ("Falsch nix is!"); } my @args = ( -varbindlist => [$ifSer] ); my @args2 = ( -varbindlist => [$ifPart] ); if ($s->version == SNMP_VERSION_1) { while (defined($s->get_next_request(@args))) { $_ = (keys(%{$s->var_bind_list}))[0]; if (!oid_base_match($ifSer, $_)) { last; } $response=($s->var_bind_list->{$_}); @final1 = $response; @args = (-varbindlist => [$_]); } print @final1; while (defined($s->get_next_request(@args2))) { $_ = (keys(%{$s->var_bind_list}))[0]; if (!oid_base_match($ifPart, $_)) { last; } $response2 = ($s->var_bind_list->{$_}); @final2 = $response2; @args2 = (-varbindlist => [$_]); } } else { push(@args, -maxrepetitions => 25); outer: while (defined($s->get_bulk_request(@args))) { my @oids = oid_lex_sort(keys(%{$s->var_bind_list})); foreach (@oids) { if (!oid_base_match($ifSer, $_)) { last outer; } #print("%s => %s\n", $_, $s->var_bind_list->{$_}); # Make sure we have not hit the end of the MIB if ($s->var_bind_list->{$_} eq 'endOfMibView') { l +ast outer; } } # Get the last OBJECT IDENTIFIER in the returned list @args = (-maxrepetitions => 25, -varbindlist => [pop(@oid +s)]); } } #@final2 = grep { $_ ne "" } $response2; open OUTPUT, ">>finallist.txt"; for my $i (0 .. $#final1) { print "$ip;$name;$final1[$i];$final2[ +$i]\n" } close OUTPUT; print $ip;print $name; # Let the user know about any errors #if ($s->error() ne '') { # _exit($s->error()); #} # Close the session $s->close(); } exit 0;

does anyone have an idea on the why that is so???
The result that should be is
final1;final2
final1;final2
final1;final2
.....

Replies are listed 'Best First'.
Re: Global vars not accessable globaly
by davorg (Chancellor) on Sep 20, 2004 at 09:28 UTC

    That code is too long and contains too many distractions to make it easy to see what is going on (or going wrong!).

    You should post a shortened version of your code that demonstrates your problem, clearly explaining what you expect to see and what unexpected results you get.

    Also, it would make it easier for us to follow your code if you used a consistant indentation style. Perhaps you'd consider putting your code thru perltidy before posting it here.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Global vars not accessable globaly
by PodMaster (Abbot) on Sep 20, 2004 at 08:56 UTC
    Those global variables are accessible globally. What you've got is a think-o. Take for example
    use vars qw[ @foo1 @foo2 ]; use Data::Dumper; { my $bar = 666; @foo1 = $bar; } { @foo2 = 666; @foo1 = 123; } die Dumper( \@foo2, \@foo1 ); __END__ $VAR1 = [ 666 ]; $VAR2 = [ 123 ];
    Any idea why @foo1 doesn't contain 666 and 123?

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      Nice example but doesn't really help a bit more plain text would be better. and what is a think-o?
      Oh and the content of final1 in that statement is correct. When i print it there it works just fine.
        Nice example but doesn't really help a bit more plain text would be better
        Can you answer why @foo1 doesn't contain 666 and 123?

        In your original question you say the array final1 and final2 are declared as global but i can only print them in there function. the print @final1 just below that and the print at the end do not work..... .

        • "It doesn't work" is not a very good explanation (some would say it is not an explanation at all) (How (Not) To Ask A Question)
        • my snippet demonstrates why @final1 and @final2 don't contain what you expect. @foo1 = 123; is not the same as push @foo1, 123;
        what is a think-o?
        Its a brain fart (dict thinko)

        MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
        I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
        ** The third rule of perl club is a statement of fact: pod is sexy.

        @array = 1; print "@array"; 1 @array = 2; print "@array"; 2 @array = 1; push @array, 2; print "@array"; 1 2

        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
        "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
Re: Global vars not accessable globaly
by steves (Curate) on Sep 20, 2004 at 10:30 UTC

    You seem to have an issue with array usage. This code is suspect:

    $response=($s->var_bind_list->{$_}); @final1 = $response; $response2 = ($s->var_bind_list->{$_}); @final2 = $response2;
    I assume the right hand side of each expression returns a list of values. When you assign those lists to the scalars $response and $response2, the scalars get the last element of each list on the right.

Re: Global vars not accessable globaly
by dragonchild (Archbishop) on Sep 20, 2004 at 13:07 UTC
    The line
    @final1 = $response;
    should be replaced with
    push @final1, $response;

    Try it out before you reply.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

      It says push ??? i tried the other option and that didn't work. So yes it must be push and it already is (or did you post before i corrected?)
Re: Global vars not accessable globaly
by theroninwins (Friar) on Sep 20, 2004 at 09:38 UTC
    ok here is a short form:
    #open ipfile open IPFILE, "ipfile2.txt" or die "Can't get IPs - $!\n"; open NAMES, "namefile2.txt" or die "Can't get IPs - $!\n"; open ERASER, ">finallist.txt"; close ERASER; + #Sets knots und community my $community = 'public'; my $ifSer = '.1.3.6.1.2.1.47.1.1.1.1.11'; my $ifPart = '.1.3.6.1.2.1.47.1.1.1.1.13'; while ( $ip = <IPFILE>, $name = <NAMES> ) { my @final1; chomp $ip; chomp $name; $name =~ s/\.bc\.de\.bic$//; print "Got: $ip\n"; + #open session my ( $s, $e ) = Net::SNMP->session( -hostname => $ip, -community => $community, -port => 161 ); if (!defined($s)) { print ("Falsch nix is!"); } -------------------------------------------------------
    Ok here it gets interesting:
    my @args = ( -varbindlist => [$ifSer] ); my @args2 = ( -varbindlist => [$ifPart] ); if ($s->version == SNMP_VERSION_1) { while (defined($s->get_next_request(@args))) { $_ = (keys(%{$s->var_bind_list}))[0]; if (!oid_base_match($ifSer, $_)) { last; } $response=($s->var_bind_list->{$_}); @final1 = $response; @args = (-varbindlist => [$_]); } while (defined($s->get_next_request(@args2))) { $_ = (keys(%{$s->var_bind_list}))[0]; if (!oid_base_match($ifPart, $_)) { last; } $response2 = ($s->var_bind_list->{$_}); @final2 = $response2; @args2 = (-varbindlist => [$_]); } } -------------------------------------------------------
    Here is the print that must work at the end:
    #@final2 = grep { $_ ne "" } $response2; open OUTPUT, ">>finallist.txt"; for my $i (0 .. $#final1) { print "$ip;$name;$final1[$i];$final2[ +$i]\n" } close OUTPUT; print $ip;print $name; # Let the user know about any errors #if ($s->error() ne '') { # _exit($s->error()); #} # Close the session #$s->close(); } exit 0;
    And the result should be :
    ip1;name1;serial1;part1 ip1;name1;serial2;part2 ip2;name2;serial1;part1 ........
    the ip and the name is already ok I only need the serial (final1) and the part (final2) thing to work
Re: Global vars not accessable globaly
by theroninwins (Friar) on Sep 20, 2004 at 12:55 UTC
    Ok the prog is now running :
    #!/usr/bin/perl -w use Net::SNMP qw(oid_lex_sort oid_base_match SNMP_VERSION_1 DEBUG_ALL) +; use strict; use Getopt::Std; use warnings; use diagnostics; use XML::Simple; use Data::Dumper; use vars qw($ip $name $response $response2 $response3 @final1 @final2 +@final3); #open xml file my $xmlfile = $ARGV[0]; my $ref = eval { XMLin($xmlfile) }; #erase or creat the ipfile and the namefile open ERASER, ">ipfile.txt"; close ERASER; open ERASER, ">namefile.txt"; close ERASER; open ERASER, ">finallist.txt"; close ERASER; open ERASER, ">alllist.txt"; close ERASER; #see if open worked if ($@) { print "XML Read ERROR"; } else { #go to IPAddress tag and read infos into file foreach my $itemip (@{$ref->{Layer2Details}->{Device}}) { my @ipliste = $itemip->{IPAddress}; my @sorted = @ipliste; open OUTIP, ">>ipfile.txt"; print OUTIP @sorted, "\n"; close OUTIP; } #go to DeviceName tag and read infos into file foreach my $itemname (@{$ref->{Layer2Details}->{Device}}) { my @nameliste = $itemname->{DeviceName}; my @sortedname = @nameliste; open OUTNAME, ">>namefile.txt"; print OUTNAME @sortedname, "\n"; close OUTNAME; } } #open ipfile open IPFILE, "ipfile2.txt" or die "Can't get IPs - $!\n"; open NAMES, "namefile2.txt" or die "Can't get IPs - $!\n"; open ERASER, ">finallist.txt"; close ERASER; + #Sets knots und community my $community = 'public'; my $ifSer = '.1.3.6.1.2.1.47.1.1.1.1.11'; my $ifPart = '.1.3.6.1.2.1.47.1.1.1.1.13'; my $ifDesc = '.1.3.6.1.2.1.47.1.1.1.1.2'; while ( $ip = <IPFILE>, $name = <NAMES> ) { chomp $ip; chomp $name; $name =~ s/\.bc\.de\.bic$//; print "Got: $ip\n"; + #open session my ( $s, $e ) = Net::SNMP->session( -hostname => $ip, -community => $community, -port => 161 ); if (!defined($s)) { print ("Falsch nix is!"); } my @args = ( -varbindlist => [$ifSer] ); my @args2 = ( -varbindlist => [$ifPart] ); my @args3 = ( -varbindlist => [$ifDesc] ); if ($s->version == SNMP_VERSION_1) { while (defined($s->get_next_request(@args))) { $_ = (keys(%{$s->var_bind_list}))[0]; if (!oid_base_match($ifSer, $_)) { last; } $response=($s->var_bind_list->{$_}); push @final1, $response; @args = (-varbindlist => [$_]); } while (defined($s->get_next_request(@args2))) { $_ = (keys(%{$s->var_bind_list}))[0]; if (!oid_base_match($ifPart, $_)) { last; } $response2 = ($s->var_bind_list->{$_}); push @final2, $response2; @args2 = (-varbindlist => [$_]); } while (defined($s->get_next_request(@args3))) { $_ = (keys(%{$s->var_bind_list}))[0]; if (!oid_base_match($ifDesc, $_)) { last; } $response3=($s->var_bind_list->{$_}); push @final3, $response3; @args3 = (-varbindlist => [$_]); } } else { push(@args, -maxrepetitions => 25); outer: while (defined($s->get_bulk_request(@args))) { my @oids = oid_lex_sort(keys(%{$s->var_bind_list})); foreach (@oids) { if (!oid_base_match($ifSer, $_)) { last outer; } #print("%s => %s\n", $_, $s->var_bind_list->{$_}); # Make sure we have not hit the end of the MIB if ($s->var_bind_list->{$_} eq 'endOfMibView') { l +ast outer; } } # Get the last OBJECT IDENTIFIER in the returned list @args = (-maxrepetitions => 25, -varbindlist => [pop(@oids +)]); } } open OUTPUT, ">alllist.txt"; for my $i (0 .. $#final1) { print OUTPUT "$ip;$name;$final1[$i];$ +final2[$i];$final3[$i]\n" } close OUTPUT; @final1=(); @final2=(); @final3=(); open FINISH, "<alllist.txt"; my @data = grep { !/\S+;;/ } <FINISH>; open WRITER,">>finallist.txt"; print WRITER @data; close WRITER; close FINISH; # Let the user know about any errors #if ($s->error() ne '') { # _exit($s->error()); #} # Close the session #$s->close(); } exit 0;
    The prog gets the serial n/o, part name and the describtion of each part and lists it in finallist.txt like:

    ip;name;serial1;part1;desc1 ip;name;serial2;part2;desc2 ip;name;serial3;part3;desc3
    after reading the parsing the ips and names from a xml file.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://392305]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-25 20:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found