dwcronin
asked on
read file in perl
i have a text file. it looks like:
========================== ========
person's name. ......................text .......... .......... .......... ......
person's name. ......................text .......... .......... .......... ......
person's name. ......................text .......... .......... .......... ......
person's name. ......................text .......... .......... .......... ......
person's name. ......................text .......... .......... .......... ......
person's name. ......................text .......... .......... .......... ......
person's name. ......................text .......... .......... .......... ......
========================== ========
how do i open the file of names of people in a cemetery and return the text that goes with ONLY that name in perl?
the file is a list of about 3000 people and the text is when they died and where they are buried. any language will do
this but i'm trying to learn perl in ubuntu.
==========================
person's name. ......................text
person's name. ......................text
person's name. ......................text
person's name. ......................text
person's name. ......................text
person's name. ......................text
person's name. ......................text
==========================
how do i open the file of names of people in a cemetery and return the text that goes with ONLY that name in perl?
the file is a list of about 3000 people and the text is when they died and where they are buried. any language will do
this but i'm trying to learn perl in ubuntu.
If I understand what you're asking...
Writing this off the top of my head, so you may have to clean up errors...
#!/usr/bin/env perl
use strict;
use warnings;
use Carp qw/confess/;
my $file = shift @ARGV || confess "Provide a file name + try again...";
open(my $in,$file) || confess "Unable to open($file): $!";
my @data = <$in>;
close($fh);
chomp @data;
foreach my $rec (@data) {
# trim all data after person's name
$rec =~ s/\.\s+.+$//o;
print $rec, "\n";
}
Writing this off the top of my head, so you may have to clean up errors...
#!/usr/bin/env perl
use strict;
use warnings;
use Carp qw/confess/;
my $file = shift @ARGV || confess "Provide a file name + try again...";
open(my $in,$file) || confess "Unable to open($file): $!";
my @data = <$in>;
close($fh);
chomp @data;
foreach my $rec (@data) {
# trim all data after person's name
$rec =~ s/\.\s+.+$//o;
print $rec, "\n";
}
David, if the file is formatted as the OP has shown, then your regex isn't going to do what is needed. The logical assumption is that it contains full names, not just first or last name. Also, if you're going to load the whole file into an array, then it would make more sense to load it into a hash and do a simple hash lookup for the desired name.
We really need to see a more realistic sampling of the data and the other info I asked for.
We really need to see a more realistic sampling of the data and the other info I asked for.
Yes. You're correct. I've updated regex, to start trimming starting with the "." trailing the name.
Good catch!
Good catch!
I'm with FishMonger.
Having a real data file will be far easier to work out details.
Having a real data file will be far easier to work out details.
ASKER
ok, you can have my full data file. I just thought perl and ptk could be used to create a screen that asks for a dead person's name in the cemetery my sister keeps the books for. i thought it would be neater and faster than the current way she does this which is by hand since she has this file printed out and alphabetized by name. i suppose i could do this using visual studios but i'm trying to do this using ubuntu.
ASKER
how do i give you an ODS file?
We don't need your full data file. We just need a small but realistic sample, no more than 10 lines. Attach a short sample file, don't copy/past the data into a comment.
Writing a Tk script is more involved especially for someone that hasn't worked with Perl. I suggest working with a console based script then work your way up to a Tk script. But the choice is yours.
Writing a Tk script is more involved especially for someone that hasn't worked with Perl. I suggest working with a console based script then work your way up to a Tk script. But the choice is yours.
Is your data file a plain text file as indicated in your opening post, or is it an .ods spreadsheet? That will make a big difference with the parsing.
You mentioned, "I just thought perl and ptk" which is a completely different type of project than processing a data file.
Now you're talking about a GUI interface + interactive application.
Best to open another question about developing interactive applications, as this diverges from your original question.
Now you're talking about a GUI interface + interactive application.
Best to open another question about developing interactive applications, as this diverges from your original question.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
What have you tried? Post your code.
In what way is your script failing to achieve your goal?
What errors and/or warnings are you getting?