Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: Create based on repeating data

by NetWallah (Canon)
on Aug 24, 2016 at 03:21 UTC ( [id://1170281]=note: print w/replies, xml ) Need Help??


in reply to Re: Create based on repeating data
in thread Create based on repeating data

Avoiding the assumption that hostnames start with "H", and simplifying logic:
#! perl -slw use strict; use Data::Dumper; my( @data, $current ); while( <DATA> ) { if( my ($host,$ip,$brand) =/^(\w+)\s+(\S+)\s+(\S+)/ ) { $current and push @data, $current; $current = {HOST=>$host, IP=>$ip, BRAND=>$brand}; next; } my( $key, $val ) = m[\s+(.+)\s+(\S+)$] or next; $current->{$key} = $val; } $current and push @data, $current; print Dumper \@data; __DATA__ Hostname1 1.1.1.1 Cisco Chassis Serial Number xyz123 Interface Gig0/0/31 Hostname2 2.2.2.2 Juniper Chassis Serial Number abc123 Interface Gi-0/0/31
Output:
$VAR1 = [ { 'Chassis Serial Number' => 'xyz123', 'Interface' => 'Gig0/0/31', 'BRAND' => 'Cisco', 'HOST' => 'Hostname1', 'IP' => '1.1.1.1' }, { 'HOST' => 'Hostname2', 'BRAND' => 'Juniper', 'Chassis Serial Number' => 'abc123', 'Interface' => 'Gi-0/0/31', 'IP' => '2.2.2.2' } ];
Alternative, using hostname as a key to a hash:
my ($current,%data) ... $current and $data{$current->{HOST}} = $current; # in 2 places $current = {HOST=>$host, IP=>$ip, BRAND=>$brand};

        "Software interprets lawyers as damage, and routes around them" - Larry Wall

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1170281]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-23 16:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found