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

Hello all. I am new here and hope this is a landing spot for junior folks like me who are in need of suggestions. If I'm in the wrong spot, quite sorry and thank you for your time. If so, perfect as I am having some trouble working with an array element. What is going on is we use an API that will let us connect to a Cisco device (router / switch) and issue a specified command and we get back the results. I use a push to populate the array. This works pretty well. Here's an example of the output of the array:

$VAR1 = [ [ 'show users | include idle 00:00:00', '*194 vty 0 spec idle 00:00:00 10.2 +34.171.75' ] ];

What I am after is the IP address at the end. In the case of the example, 10.234.171.75 Is there a way to work backwards to just get the IP? I'm finding ways to do basically this but for an element. In my case, I just have the one element. I've included the main code from my script, though don't think this is useful.

## ## Prepare for array to hold data from cmd ## This will hold the line sent (unfortunately - future work - fi +nd way to not capture this) ## This will hold the line returned, there should just be one lin +e. ## I am interested in IP, this IP could vary ## my @res = (); ## ## Note that the 'cmd()' method will return the results ## that were observed on the target device, following the ## given command. push @res, [$con->cmd("show users | include idle +00:00:00")]; ## print Dumper \@res;

I'm trying to get a foreach loop to work thru the array a space at a time, but this seems poor. Regards, Brian

Replies are listed 'Best First'.
Re: working backwards in an array element?
by talexb (Chancellor) on Aug 27, 2010 at 17:03 UTC

    The offset [-1] gives you the last element of an array, so $res[0][-1] would give you the line with the IP address in it. After that you could just split the line and grab the last word, which would give you the IP address you want.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

      Thanks! I tried that idea out and got it working. Definitely appreciate your help. -Brian

Re: working backwards in an array element?
by Utilitarian (Vicar) on Aug 27, 2010 at 17:07 UTC
    Something like the following would produce a list of the last IP in each array element.
    my @ips; for $result (@res){ my ($ip) = ($result=~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/); push @ips, $ip if defined $ip; }
    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: working backwards in an array element?
by umasuresh (Hermit) on Aug 27, 2010 at 20:53 UTC

      A regular expression should only be used when the simpler methods just won't cut it. Remember, Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems. (See this.) Simple is best.

      Alex / talexb / Toronto

      "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re: working backwards in an array element?
by pemungkah (Priest) on Aug 29, 2010 at 00:36 UTC
    And if for some reason you did want to access an array from right to left (say in a foreach) you can always reverse it. $res[-1] is better here because all you want is the last element; reversing the whole array when you only want one item is wasteful.

    Also note that you can use any negative index you like as long as there are enough elements, so in a 5-element array you could use up to -5 as an index.