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: Beginner:
need some advice

 



hanja
Deleted

Mar 30, 2001, 7:20 PM

Post #1 of 3 (200 views)
need some advice Can't Post

Here is a file that contains,


-

Author (id number) : id1234
Phone number : 555-5555
Work number : 555-5556
Vechicle : truck
File name : example.txt
Modification Date : 02/23/2001
Paragarph Number Page number Other
PAGE-XX-4.x CAR-PAGE-2 23
PAGE-SS-5 CAR-PAGE-34 23

--


Ok what I want to do is read the whole file and only record
bits and piceses of it. Here is what I have so far


--
############################################################
#OPENS AND READS A FILE RECORDING/DISPLAYING THE AUTHOR/DATE
############################################################
#!/usr/local/bin/perl -w

open(FILE, "<example.txt") or die ("can't open file);
while ($line = <FILE))
{
if ($line =~ /Author\s*\(id number\)\s*:(.*)/)
{
$author = $1;
chomp $author;
}

if ($line =~ /Modification Date\s*:\s*(.*)/)
{
$date = $1;
chomp $date;
}
}
close (FILE);

print "author is $author\n";
print "date is $date\n";

--


Ok my question is how do I record the paragraph/page/other information. I know that
I've to use a regular expression but can you give me a clue of how to write one
for this. Also how should I store this information? For example, if I open another
file it may have more paragraph/page/other information than the "example.txt" one. I
wouldn't know until I opened up the file. Should I use an array of hashes for this
part or is there a better way?

Thanks.



Jean
User


Apr 1, 2001, 7:00 AM

Post #2 of 3 (189 views)
Re: need some advice [In reply to] Can't Post

Please give a detailed example of what you expect to receive.
In case you want to get the lines following the line containing word "Paragraph", use additional reading statements ($line = <FILEHANDLE>) inside the main loop.

Jean Spector
QA Engineer @ Extent Technologies, Ltd.
mage@lycosmail.com


geest
Deleted

May 1, 2001, 1:28 PM

Post #3 of 3 (160 views)
Re: need some advice [In reply to] Can't Post

complex data structures should be handled by hashes of hashes

$data_file = "daten.txt";

open DATA_FILE, "$data_file" or die "Can't open $data_file: $!\n";

$author = {};

while (<DATA_FILE>) {
if ( /(^Author \(id number\) :)\s+(.+)/ ) {
$id = $2;
$author->{$id} ={};
} elsif ( /(^\D+)\s:\s+(.+)/ ){
$key = $1;
$value = $2;
$author->{$id}->{$key} = $value;
} else {
$author->{$id}->{'rest'} .= $_;
}

}

# output
foreach $id (keys %$author) {
print "$id\n";
foreach $id_field (keys %{$author->{$id}}) {
print $id_field ,":", $author->{$id}->{$id_field}, "\n";
}

}


 
 


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

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