Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

multidimensional array printing

by chanslor (Acolyte)
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 hme1 UP 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;

Replies are listed 'Best First'.
Re: multidimensional array printing
by toolic (Bishop) on Aug 25, 2012 at 23:31 UTC
Re: multidimensional array printing
by Marshall (Abbot) 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 = ''; 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 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 hostname hme1 UP hostname END: Interface report on host1
        I need to figure out when a new interface come by and give a new line...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://989760]
Approved by toolic
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2018-02-19 16:34 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (266 votes). Check out past polls.