Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > how to search through file for matching criteria

Reply
Thread Tools

how to search through file for matching criteria

 
 
richjungk@gmail.com
Guest
Posts: n/a
 
      06-08-2006
guys i really need help!!!
i'm a beginner in perl language.
for my code i want to look for matching criteria.

here is the contents of file

c_c_flag::
survey28::2
driver1_G2_date_month::12
driver1_student::N
current_amount::6700
driver1_suspensionstart_year::2004
p1::5217
car1_comprehensive_coverage::0
search_alternative_coverage::N
car1_liability_coverage::500
car1_stats::No
renew::July
client_phone3::7509
driver1_status::S
driver1_conviction_1_month::01
p2::6740
driver1_conviction_1_year::2005
c1::KNGN
driver1_birth_month::03
driver1_license::G
car1_km::0
LANG::
driver1_insured::2000
driver1_sex::M
postal_code_2::1L5
driver1_suspensionend_year::2004
driver1_convictions::1
driver1_suspensionend_month::08
c3::RBC
b_c_flag::
HabDateCompany_month::01
city::BRAMPTON
driver1_convictiontype_1::Criminal=CD
driver1_insured_now::Y
how_find::Sun Newspaper
current_company::echelon
car1_annual_mileage::10000
car1_drivers::1
rate_alert_period::30
driver1_training::Y
postal_code::L6P
driver_number::1
driver1_claims::0
client_phone2::888
driver1_license_date_month::12
car_number::1
driver1_birth_day::15
car1_annual_mileage_business::0
driver1_G1_date_year::1999
driver1_G1_date_month::03
driver1_nonpayment::0
driver1_suspensionstart_month::05
p3::8973
car1_make::ACURA
client_name::Stan Duhan
c2:AF
car1_collision_coverage::0
driver1_suspension::Y
car1_model::INTEGRA RS 2DR
client_email::(E-Mail Removed)
driver1_birth_year::1983
driver1_license_date_year::2003
car1_primary::1
client_phone1::416
driver1_first_name::Stan
driver1_retired::N
HabDateCompany_year::Not Applicable
car1_year::1991
driver1_G2_date_year::1999
AssumeMultiLine::N
car1_use:


As you can see, each line contains a pair of strings separated by
'::'


i need to open the file and process it line by line, and figuring out
how many driver profiles are stored in the file and store this in an
array.
Put the array values into a hash to make them unique, and put the hash
back into an array.


Close the file.


Reopen the file and loop through the array and create hashes for each
driver to provide the following aggregate information:


Number of:
male drivers under age 25 with a conviction
female drivers under age 25 with a conviction


and, here is my code that i did so far..............


#!/urs/bin/perl -w


my %DATA;
my $dnum;
my $conviction;


chdir("directory path") or die;


open(FH, "ID1234.txt") or die "$!\n";


while(<FH>){
if($_ =~ / (driver \d ) / ) { # $1 is first match
push @drivers, $1;
}
}
close FH or die;


foreach(@drivers){
$D{"$_")++;
}


@sorts = sort(keys(%D)); # "driver1" "%D"


foreach(@sorts){
open *FH, "ID1234.txt") or die;


$dnum = $_ ;


while(<FH>){
if($_ =~ / $dnum( _conviction ) / ){
$conviction = $1;
}
}
close FH or die;



}


from now on, how to i search for criteria for

Number of:
male drivers under age 25 with a conviction
female drivers under age 25 with a conviction

 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      06-08-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> guys i really need help!!!
> i'm a beginner in perl language.
> for my code i want to look for matching criteria.
>
> here is the contents of file
>
> c_c_flag::
> survey28::2
> driver1_G2_date_month::12
> driver1_student::N
> current_amount::6700
> driver1_suspensionstart_year::2004
> p1::5217
> car1_comprehensive_coverage::0
> search_alternative_coverage::N
> car1_liability_coverage::500
> car1_stats::No
> renew::July
> client_phone3::7509
> driver1_status::S
> driver1_conviction_1_month::01
> p2::6740
> driver1_conviction_1_year::2005
> c1::KNGN
> driver1_birth_month::03
> driver1_license::G
> car1_km::0
> LANG::
> driver1_insured::2000
> driver1_sex::M
> postal_code_2::1L5
> driver1_suspensionend_year::2004
> driver1_convictions::1
> driver1_suspensionend_month::08
> c3::RBC
> b_c_flag::
> HabDateCompany_month::01
> city::BRAMPTON
> driver1_convictiontype_1::Criminal=CD
> driver1_insured_now::Y
> how_find::Sun Newspaper
> current_company::echelon
> car1_annual_mileage::10000
> car1_drivers::1
> rate_alert_period::30
> driver1_training::Y
> postal_code::L6P
> driver_number::1
> driver1_claims::0
> client_phone2::888
> driver1_license_date_month::12
> car_number::1
> driver1_birth_day::15
> car1_annual_mileage_business::0
> driver1_G1_date_year::1999
> driver1_G1_date_month::03
> driver1_nonpayment::0
> driver1_suspensionstart_month::05
> p3::8973
> car1_make::ACURA
> client_name::Stan Duhan
> c2:AF
> car1_collision_coverage::0
> driver1_suspension::Y
> car1_model::INTEGRA RS 2DR
> client_email::(E-Mail Removed)
> driver1_birth_year::1983
> driver1_license_date_year::2003
> car1_primary::1
> client_phone1::416
> driver1_first_name::Stan
> driver1_retired::N
> HabDateCompany_year::Not Applicable
> car1_year::1991
> driver1_G2_date_year::1999
> AssumeMultiLine::N
> car1_use:
>
>
> As you can see, each line contains a pair of strings separated by
> '::'
>
>
> i need to open the file and process it line by line, and figuring out
> how many driver profiles are stored in the file and store this in an
> array.


Why an array?

> Put the array values into a hash to make them unique,


Why not just put them into a hash in the first place?

> and put the hash back into an array.


Why?


> Close the file.
>
>
> Reopen the file and loop through the array and create hashes for each
> driver to provide the following aggregate information:


There should be no need to read the file twice, you should be able to collect
all of the data you need in one pass.


> Number of:
> male drivers under age 25 with a conviction
> female drivers under age 25 with a conviction


You will probably need the Date::Calc module to calculate the age of the
driver from the birth date provided.

Something like this should work (UNTESTED):


#!/usr/bin/perl
use warnings;
use strict;
use Date::Calc 'Delta_YMD';

my $dir = 'directory path';
my $file = 'ID1234.txt';

chdir $dir or die "Cannot chdir '$dir' $!";

open my $fh, '<', $file or die "Cannot open '$file' $!";


my ( $today_year, $today_month, $today_day ) = ( localtime )[ 5,4,3 ];
$today_year += 1900;
$today_month++;

my ( %data, $current_driver );

while ( <$fh> ) {

if ( /^(driver\d+)_sex:[MF])/ ) {
$data{ $1 }{ sex } = $2;
}

if ( /^(driver\d+)_convictions:\d+)/ ) {
$data{ $1 }{ convictions } = $2;
}

if ( /^(driver\d+)_birth_(year|month|day):\d+)/ ) {
$data{ $current_driver = $1 }{ $2 } = $3;
}

if ( exists $data{ $current_driver }{ year } and
exists $data{ $current_driver }{ month } and
exists $data{ $current_driver }{ day } ) {

my ( $delta_year, $delta_month, $delta_day ) =
Delta_YMD( @{ $data{ $current_driver } }{ qw/year month day/ },
$today_year, $today_month, $today_day );
$data{ $current_driver }{ age } =
$delta_month > 0 ? $delta_year
: $delta_day >= 0 ? $delta_year
: $delta_year - 1;
delete @{ $data{ $current_driver } }{ qw/year month day/ }
}
}

close $fh;

my %stats;

for my $driver ( keys %data ) {
$stats{ $data{ $driver }{ sex } }++
if $data{ $driver }{ convictions } and
$data{ $driver }{ age } < 25;
}

print <<STATS;
Number of:
male drivers under age 25 with a conviction is $stats{M}
female drivers under age 25 with a conviction is $stats{F}
STATS

__END__



John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
XSLT: selecting nodes matching either of 2 criteria Andy Fish XML 5 07-31-2007 10:20 AM
build an array based on data from file matching criteria shaun.pille@gmail.com C++ 1 07-21-2006 07:36 PM
how to search through file for certain criteria richjungk@gmail.com Perl Misc 3 06-08-2006 01:28 AM
Forwarding search criteria from the view VisionSet Java 2 10-28-2004 02:42 PM



Advertisments