Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Print selected records only?

by rnahi (Curate)
on Jun 02, 2004 at 13:27 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://359413]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-19 13:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found