complex regex

S

Sami

Hi There, I have a challenging problem here , atleast to me . I have a
file contains students information as follow:


GRADE MATH COMPUTER HISTORY GOVERNEMENT
A Sue DON
Mike
TOM
B+ Sue


What I am doing is reading the student information with the grade and
class and updating that information file. I need to watchout for not
inserting information for the same student twice for one subject. I am
able to find the grade but not able to instert the entry in the
correct location .

my $Info="/home/Info";

print " Enter the GRADE\n";
my $grd = <STDIN>;
#Don't forget to get rid of the newline character from the input
chomp $GRADE;

print " Enter the SUBJECT\n";
my $subj = <STDIN>;
#Don't forget to get rid of the newline character from the input
chomp $subj;

print " Enter the Student Name\n";
my $std = <STDIN>;
#Don't forget to get rid of the newline character from the input
chomp $std;


&update($grd, $subj, $std);



sub update {
my $grade = $_[0];
my $subject = $_[1];
my $name = $_[2];

#Open the file and read it in:
open(FILE, "$Info") || die "couldn't open $Info for reading";
#Each line is stored in an array
my @in=<FILE>;
close(FILE);


#Now open the same file again for output
open (FILE, ">$Info") ||die "couldn't open $Info for writing";

#Now print out everything and update:
for (@in)
{
print FILE ;
if /^$grade/ # and here where I lose it ,, what to do to match
+ the rest :(
}
close(FILE);


Can someone advide please ? thanks much
 
J

James Willmore

Hi There, I have a challenging problem here , atleast to me . I have a
file contains students information as follow:


GRADE MATH COMPUTER HISTORY GOVERNEMENT
A Sue DON
Mike
TOM
B+ Sue


What I am doing is reading the student information with the grade and
class and updating that information file. I need to watchout for not
inserting information for the same student twice for one subject. I am
able to find the grade but not able to instert the entry in the
correct location .
[snip]

Can someone advide please ? thanks much

Short answer - use DBD::CSV :)

Explaination - if you want to use a database, but really it's just a "flat
file" that "thinks" it's a database, then using DBI with DBD::CSV, IMHO,
is the way to go. I suggest this because ....

* a CSV file can be read/written/etc. from an editor as well as a script
of your own design.

* you can use SQL againist the file to not only prevent duplication, but
do such wonderful things as calculating averages and selecting grades for
one student only.

* when you decide that you hvae quite a bit of information in this file
and want to use a "real" database, it won't be such a pain to convert.
And the script(s) you write will only need one or two lines changed versus
total re-writing.

HTH

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
The hardest thing in the world to understand is the income tax.
-- Albert Einstein
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top