in reply to perl parsing
#!/usr/bin/perl use strict; use warnings; my $line; while ( defined ($line = <DATA>)) { if ($line =~ /^name/) { $line = process_record ($line); redo if defined $line; # another name line } } sub process_record { my $line = shift; (my $name) = $line =~ /^name\s+(\w+)/; my %devices; while (defined ($line = <DATA>) and $line !~ /^name/) { if ( (my $device) = $line =~ /^device\s+(\w+\s+\w+)/) { $device =~ s/(\w+)\s+(\w+)/$1 $2/; $devices{$device}=1; } } print "$name:\n"; print " device $_\n" foreach keys %devices; return $line; } =PRINTS: Brian: device ipad 2001 Andrew: device ipad 2009 ryan: device ipad 2005 device cell 2009 =cut __DATA__ socks something name Brian shirt yellow socks black device ipad 2001 device ipad 2001 device ipad 2001 tag no tag 0 name Andrew shirt orange socks black device ipad 2009 tag no tag 0 name ryan shirt blue socks black device ipad 2005 device cell 2009 tag yes tag 1
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: perl parsing
by cbtshare (Monk) on Oct 04, 2017 at 20:50 UTC | |
|
Re^2: perl parsing
by cbtshare (Monk) on Oct 04, 2017 at 22:44 UTC | |
by AnomalousMonk (Archbishop) on Oct 05, 2017 at 00:11 UTC | |
by Marshall (Canon) on Oct 05, 2017 at 17:50 UTC |