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

Hi Monks,

I need your help!
I have want to create an array of arrays.
I have data in arrays "gate (10.1.254.254)","router (193.83.153.254)". But if I push them, I get array length zero. How can I push the array into the array so that each is in a separate line?
Many thanx!

Clint

Replies are listed 'Best First'.
Re: Please help with arrays
by tlm (Prior) on Jun 22, 2005 at 13:04 UTC

    I don't know what you mean by "gate (10.1.254.254)", etc. but, in general, if you have arrays @arr1, @arr2, @arr3, etc., you can make an array of arrays like this:

    my @AoA = ( \@arr1, \@arr2, \@arr3, ... );
    If gate, router are functions returning lists, then you can do this:
    my @AoA = ( [ gate('10.1.254.254') ], [ router('193.83.153.254') ], .. +. );
    See perlreftut, perllol, and perlref.

    the lowliest monk

Re: Please help with arrays
by sh1tn (Priest) on Jun 22, 2005 at 12:59 UTC
Re: Please help with arrays
by robot_tourist (Hermit) on Jun 22, 2005 at 13:05 UTC

    I do not know for certain what you want your datastructure to look like. The data looks like it might be more logical in a hash. for example:

    $ip_data{gate} == "10.1.254.254"

    Please give us more details on the datastructure you have and what you need to do with it.

    How can you feel when you're made of steel? I am made of steel. I am the Robot Tourist.
    Robot Tourist, by Ten Benson

      Hi robot_tourist,

      many thanx for your help!

      $refaLines has following data:
      1 cat-itd-05 (10.1.254.253) 1.16 ms 4.352 ms 2.714 ms
      2 gate (10.1.254.254) 0.792 ms 1.178 ms 7.758 ms
      3 router (193.83.153.254) 9.341 ms * 14.323 ms

      I only want a list which looks like this:
      1 cat-itd-05 (10.1.254.253)
      2 gate (10.1.254.254)
      3 router (193.83.153.254)

      I wrote following code:
      sub ExtractHopHostIp{ my $refaLines=shift; my @Fields; my @FoundHops; print @$refaLines,"\n"; for (@$refaLines) { s/[ \t]+/ /g; # remove duplicate blanks and tabs @Fields=split / /,$_; splice(@Fields,4); # get fields from the array push @FoundHops,@Fields; } @FoundHops = join " ",@FoundHops; print @FoundHops,"\n"; }
      But I get this:
      1 cat-itd-05 (10.1.254.253) 2 gate (10.1.254.254) 3 router (193.83.153.254)

      Many thanx for any help!

      Clint

      20050623 Edit by ysth: code tags

        Please contain your code (or program output) in <code> tags, as follows.

        <code>

        This is code and will be formatted as such.
        </code>

        --
        [ e d @ h a l l e y . c c ]

        I do not think the last join is doing what you want it to. It is putting your list of values into one long string.

        How can you feel when you're made of steel? I am made of steel. I am the Robot Tourist.
        Robot Tourist, by Ten Benson

        Check the three lines after the s/\t+/ /g; and think about where your data are actually going. You really just want an array of strings. One string for each line, in this case one string for each hop. Each separate element of @Fields became a separate element of @FoundHops. Then you joined it all together into one long string.

        e.g. after the first time round the loop, @FoundHops is the following list of scalars: ("1", "cat-itd-05", "10.1.254.253")

        How can you feel when you're made of steel? I am made of steel. I am the Robot Tourist.
        Robot Tourist, by Ten Benson