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

Split() first 3 elements of @array

by hmb104 (Sexton)
on Aug 28, 2012 at 18:40 UTC ( #990301=perlquestion: print w/ replies, xml ) Need Help??
hmb104 has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I'm getting about 10 lines when I perform a query on a Cisco router. From these 10 lines I need the some info from the first 3 lines. Hwo I can use the split function on ONLY the first 3 lines of the array? Below is my Code:

@Data = split( ' ', $RouterOutput[0,2] ); if ( ( $Data[0] eq 'Device' ) && ( $Data[1] eq 'ID:' ) ) { $SwitchName = $Data[2] ; $SwitchName =~ s/$'site.com'//i ; } if ( ( $Data[0] eq 'IP' ) && ( $Data[1] eq 'address' ) ) { $SwitchIP = $Data[2] ; }

Comment on Split() first 3 elements of @array
Download Code
Re: Split() first 3 elements of @array
by philiprbrenan (Monk) on Aug 28, 2012 at 18:43 UTC
Re: Split() first 3 elements of @array
by philiprbrenan (Monk) on Aug 28, 2012 at 18:46 UTC

    Select the first 3 elements of an array as:

    @array[0..2]

    exerting care in case there are less than 3 elements.

    use feature ":5.14"; use warnings FATAL => qw(all); use strict; use Data::Dump qw(dump); my @RouterOutput = split /\n/, << 'END'; 1 AAAA BBBB CCCC 2 AAAA BBBB CCCC 3 AAAA BBBB CCCC 4 AAAA BBBB CCCC END for(@RouterOutput[0..2]) {say "@{[split]}[0,3,2,1]" if $_; }

    Produces

    1 CCCC BBBB AAAA
    2 CCCC BBBB AAAA
    3 CCCC BBBB AAAA
    

      Thanks for the suggestions. I have tried 2 ways and I still can't get some of the info. Lets assume the lines that I store at my @array are the following:

      Device ID: switch1.Core-3750.abc.com Entry address(es): IP address: 10.10.10.10 Platform: cisco WS-C3750G-12S, Capabilities: Router Switch IGMP Interface: GigabitEthernet1/0/28, Port ID (outgoing port): GigabitEth +ernet2/0/7 Holdtime : 145 sec Version : Cisco IOS Software, C3750 Software (C3750-IPSERVICESK9-M), Version 12. +2(55)SE4, RELEASE SOFTWARE (fc1) Technical Support: http://www.cisco.com/techsupport Copyright (c) 1986-2011 by Cisco Systems, Inc. Compiled Tue 06-Sep-11 02:59 by prod_rel_team advertisement version: 2 Protocol Hello: OUI=0x00000C, Protocol ID=0x0112; payload len=27, val +ue=00000000FFFFFFFF01022507000000000000A8B1D400F080FF0000 VTP Management Domain: 'networks' Native VLAN: 1 Duplex: full

      I can extract the switch name but I cannot sxtract the IP address for the switch for some reason

      foreach $Line ( @RouterOutput ) { if ( $Line =~ m/^Device / ) { @Data = split( ' ', $Line ); $SwitchName = $Data[ 2 ] ; last ; } if ( $Line =~ m/^IP / ) { @Data = split( ' ', $Line ); $SwitchIP = $Data[ 2 ] ; last ; } }

        Try the following:

        use Modern::Perl; my @RouterOutput = <DATA>; my $RouterOutput = join ' ', @RouterOutput; my ( $SwitchName, $SwitchIP ) = $RouterOutput =~ /Device\s+ID:\s+([^\s]+).+IP\s+address:\s+([^\s]+)/ +s; say "$SwitchName\n$SwitchIP"; __DATA__ Device ID: switch1.Core-3750.abc.com Entry address(es): IP address: 10.10.10.10 Platform: cisco WS-C3750G-12S, Capabilities: Router Switch IGMP Interface: GigabitEthernet1/0/28, Port ID (outgoing port): GigabitEth +ernet2/0/7 Holdtime : 145 sec Version : Cisco IOS Software, C3750 Software (C3750-IPSERVICESK9-M), Version 12. +2(55)SE4, RELEASE SOFTWARE (fc1) Technical Support: http://www.cisco.com/techsupport Copyright (c) 1986-2011 by Cisco Systems, Inc. Compiled Tue 06-Sep-11 02:59 by prod_rel_team advertisement version: 2 Protocol Hello: OUI=0x00000C, Protocol ID=0x0112; payload len=27, val +ue=00000000FFFFFFFF01022507000000000000A8B1D400F080FF0000 VTP Management Domain: 'networks' Native VLAN: 1 Duplex: full

        Output:

        switch1.Core-3750.abc.com 10.10.10.10
        I cannot sxtract the IP address for the switch for some reason

        The third line, with the IP address is indented: there is some whitespace at the beginning of the line, but your regular expression is looking for 'IP' at the beginning of the line: /^IP/. Change that to accept some leading whitespace and you should get a match: /^\s*IP/

Re: Split() first 3 elements of @array
by pvaldes (Chaplain) on Aug 29, 2012 at 16:40 UTC

    maybe like this, untested

    while (<$RouterOutput>); if ($. <=3) { @Data = split /' '/, $RouterOutput; ... }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2014-09-18 10:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (111 votes), past polls