
freddo
User
May 24, 2001, 4:31 PM
Post #6 of 8
(19476 views)
|
Re: count of matches in a file
[In reply to]
|
Can't Post
|
|
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)#!/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: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:---------------------------------------- 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 ) 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)
|