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

Re: Print selected records only?

by rnahi (Curate)
on Jun 02, 2004 at 13:27 UTC ( #359413=note: print w/replies, xml ) Need Help??


in reply to Print selected records only?

Here is one possible solution.

#!/usr/bin/perl -w use strict; use Data::Dumper; my $currec = undef; my @recs =(); while (<DATA>) { if ( /^Record\s+(\d+)/) { $currec = $1 } elsif ( /^ (\S+) # non spaces. This is the key name \s+ # some spaces follows : # and a colon \s+ # more spaces [^\[]+ # any non bracket character \[ # an opening bracket ( [^\]]* # any more characters before a closing bracket ) \] # a closing bracket /x) { $recs[$currec]{$1} = $2; } } print Data::Dumper->Dump([ \@recs ], ['original']); my @twelves = grep { $recs[$_]->{Ref} == 12 } 0.. $#recs; print Data::Dumper->Dump( [ \@{[ @recs[@twelves] ]} ], ['twelves']); __DATA__ ================================================= Record 0 Name : John [John] I/C : >123< [123] Sex : 'Male' [Male] Ref : >12< [12] ================================================== Record 1 Name : Pretty [Pretty] I/C : >125< [125] Location : 'Female' [Female] Ref : >12< [12] ================================================== Record 2 Name : Kim [Kim] I/C : >124< [124] Location : 'Male' [Male] Ref : >11< [11] ==================================================

And the output:

$original = [ { 'I/C' => '123', 'Ref' => '12', 'Sex' => 'Male', 'Name' => 'John' }, { 'I/C' => '125', 'Ref' => '12', 'Location' => 'Female', 'Name' => 'Pretty' }, { 'I/C' => '124', 'Ref' => '11', 'Location' => 'Male', 'Name' => 'Kim' } ]; $twelves = [ { 'I/C' => '123', 'Ref' => 12, 'Sex' => 'Male', 'Name' => 'John' }, { 'I/C' => '125', 'Ref' => 12, 'Location' => 'Female', 'Name' => 'Pretty' } ];

This should be more than enough for a start.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://359413]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2018-04-19 16:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?