bh_perl has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
Below are some of my sample files, but I have a problem how to print all the data which are contain Ref equals to 12 (as example). Could some body help me ?

sample.txt
================================================= 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] ==================================================
Otherwise, is that any possiblity to print the result like that :
================================================= Record 0 Name : John I/C : 123 Sex : Male Ref : 12 ================================================== Record 1 Name : Pretty I/C : 125 Location : Female Ref : 12 ==================================================

TQ, bh

Replies are listed 'Best First'.
Re: Print selected records only?
by rnahi (Curate) on Jun 02, 2004 at 13:27 UTC

    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.

Re: Print selected records only?
by sweetblood (Prior) on Jun 02, 2004 at 13:27 UTC
    This sounds suspiciously like homework, so I'll give you a hint.

    Look up $/

    Good Luck

    Sweetblood

Re: Print selected records only?
by deibyz (Hermit) on Jun 02, 2004 at 13:27 UTC
    If it's not very large file, I think the simpliest way is to put the whole file into an string as explained here.
    Once done, split and regex will do the rest of the work:

    use strict; use warnings; my $text = <<EOF; ================================================= 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] ================================================== EOF my @goods = grep /Ref\s+:\s+>12</, split /=+/, $text; # For this example print join "\n==================================================\n", +@goods;

    Hope it helps,
    deibyz