CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions: Re: count of matches in a file: Edit Log



freddo
User

May 24, 2001, 4:31 PM


Views: 7441
Re: count of matches in a file

Hi again kumaichi,

You should perhaps make a new post for new questions, so people will look at it for the topic. Anyway, here is my quick and dirty solution (try to never do such awfull code, i just did it to give you the idea, it would probably be best to use an array of hashes)

Code
#!/usr/bin/perl 

open FILE, "< db.txt";
while (<FILE>) {
($ID, $PID, $PARENT_REC, $FIELD2) = split(/\|/);

$parent_rec[$ID] = $PARENT_REC;
$field2[$ID] = $FIELD2;
$count[$ID] = 0 unless $count[$ID];
$parent_id[$ID] = $PID if ($PID);
$count[$PID]+= 1 if ($PID);
}
close FILE;

for ($i=1; $i < @count; $i++) {
print "-"x40, "\n",
"I am ", $parent_rec[$i], " and have ", $count[$i], " child(s).\n";
print "I am also the child of ", $parent_rec[$parent_id[$i]], "\n"
if ($parent_rec[$parent_id[$i]]);
}


for the following db.txt file:

Code
1|0|Francois|field2 
2|1|Annette|field2
3|1|Francine|field2
4|3|Frederic|field2

Notice how i changed the id to keep the 0 to create a new person (people with $PID = 0 dont have any relation with others).

And this will output:

Code
---------------------------------------- 
I am Francois and have 2 child(s).
----------------------------------------
I am Annette and have 0 child(s).
I am also the child of Francois
----------------------------------------
I am Francine and have 1 child(s).
I am also the child of Francois
----------------------------------------
I am Frederic and have 0 child(s).
I am also the child of Francine

i hope it helps (to know my genealogy Smile )

freddo

;---
Real programmers´butcher dont understand when they just ask for 3735928559.

(This post was edited by freddo on May 24, 2001, 3:49 PM)


This post has not been edited


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives