Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

multidimensional array printing

by chanslor (Novice)
on Aug 25, 2012 at 22:41 UTC ( #989760=perlquestion: print w/ replies, xml ) Need Help??
chanslor has asked for the wisdom of the Perl Monks concerning the following question:

Monks, I would like to know how I can print this multidimensional array all on one line:
print "START: Interface report on $hostname\n"; for(my $i = 0; $i <= $#dat; $i++){ # $#dat gives the highest index from the array for(my $j = 0; $j <= $#dat ; $j++){ print "$dat[$i][$j] "; } print "\n"; } print "END: Interface report on $hostname\n";
Currently show this output:
START: Interface report on nodename hme0 UP xxx.xxx.xxx.xxx hme1 UP xxx.xxx.xxx.xxx END: Interface report on nodename
I would just like for the entire hme0 UP with IP and hostname all on one line.... here is where I push the data:
push @dat, [ $interface_name, $ifstatus ] ; then later... $dat[$ip_count][2]=$ipaddr;
Thanks...

Comment on multidimensional array printing
Select or Download Code
Re: multidimensional array printing
by toolic (Chancellor) on Aug 25, 2012 at 23:31 UTC
Re: multidimensional array printing
by Marshall (Prior) on Aug 26, 2012 at 05:19 UTC
    Try using:
    use Data::Dumper; print Dumper \@something;
    Data::Dumper is a core module and is a part of all Perl installations. You will see this extra "\n" in your code.

    #/usr/bin/perl -w use strict; use Data::Dumper; my @twoD = (['x', 32], ['y', 45]); #a 2D array is an array of references to array print "Using row reference...\n"; foreach my $row_ref (@twoD) { print "@$row_ref\n"; } print "\n",'$twoD[1][1] is ',"$twoD[1][1]\n"; #prints 45 print "\nUsing Data::Dumper\n"; print Dumper \@twoD; __END__ Using row reference... x 32 y 45 $twoD[1][1] is 45 Using Data::Dumper $VAR1 = [ [ 'x', 32 ], [ 'y', 45 ] ];
Re: multidimensional array printing
by Anonymous Monk on Aug 26, 2012 at 12:28 UTC
    my $ifstatus = 'UP'; my $ip_count = 0; my $ipaddr = 'xxx.xxx.xxx.xxx'; my $interface_name = 'hme0'; push @dat, [$interface_name, $ifstatus]; $dat[$ip_count][2] = $ipaddr; print "START: Interface report on $hostname\n"; for (my $i = 0 ; $i <= $#dat ; $i++) { for (my $j = 0 ; $j <= $#{$dat[$i]} ; $j++) { print "$dat[$i][$j] "; } print "\n"; } print "END: Interface report on $hostname\n"; __END__ START: Interface report on hme0 UP xxx.xxx.xxx.xxx END: Interface report on
      Why not simply like this:
      print "START: Interface report on $hostname\n"; print join(" ", map @{$_}, @dat) . "\n"; print "END: Interface report on $hostname\n";
        Or much simple:
        print "@{$_}\n" for @dat;
        Almost there, here is the output with your suggestion:
        START: Interface report on host1 hme0 UP xxx.xxx.xxx.xxx hostname hme1 UP xxx.xxx.xxx.xxx hostname END: Interface report on host1
        I need to figure out when a new interface come by and give a new line...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (14)
As of 2014-07-31 18:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (251 votes), past polls