in reply to Please help with arrays

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

Replies are listed 'Best First'.
Re^2: Please help with arrays
by clint (Novice) on Jun 22, 2005 at 13:24 UTC
    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