Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Extract Data Between Lines

by kcott (Abbot)
on Apr 04, 2014 at 06:17 UTC ( #1081069=note: print w/ replies, xml ) Need Help??


in reply to Extract Data Between Lines

G'day namelkcip,

Welcome to the monastery.

Here's how you can use $/ to get each "object ..." as a record; specify which fields you want; and ignore fields (e.g. description) which don't exist in any given object.

Note how $/ is localised within an anonymous block. This makes $/ = "\nobject" a temporary value which only affects the while (<DATA>) {...} loop; elsewhere in your script, $/ will have its default value. See "Temporary Values via local()" for more details.

#!/usr/bin/env perl use strict; use warnings; my @objects; my @fields = qw{network host description}; { local $/ = "\nobject"; while (<DATA>) { my %object; for my $field (@fields) { / $field \s ( .*? ) $ /mx and $object{$field} = $1; } push @objects, \%object if keys %object; } } use Data::Dump; dd \@objects; __DATA__ object network Microsoft.Lync.Host.3 host 138.108.25.111 description Help Desk Ticket #476739 object network Microsoft.Lync.Host.4 host 138.108.25.112 description Help Desk Ticket #476739 object network Microsoft.Lync.Host.5 host 138.108.25.113 description Help Desk Ticket #476739 object network Microsoft.Lync.Host.6

Output:

[ { description => "Help Desk Ticket #476739", host => "138.108.25.111", network => "Microsoft.Lync.Host.3", }, { description => "Help Desk Ticket #476739", host => "138.108.25.112", network => "Microsoft.Lync.Host.4", }, { description => "Help Desk Ticket #476739", host => "138.108.25.113", network => "Microsoft.Lync.Host.5", }, { network => "Microsoft.Lync.Host.6" }, ]

-- Ken


Comment on Re: Extract Data Between Lines
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2015-07-02 05:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (29 votes), past polls