Re: complex regex
From: Jim Gibson (jgibson_at_mail.arc.nasa.gov)
Date: 02/27/04
- Next message: Sandman: "Re: Double Jump Box Redirection"
- Previous message: fifo: "Re: using sed from with a perl script"
- In reply to: Sami: "complex regex"
- Next in thread: James Willmore: "Re: complex regex"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 27 Feb 2004 09:10:12 -0800
In article <dd593a87.0402270751.55f32bf@posting.google.com>, Sami
<amoura@sbcglobal.net> wrote:
> 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
You have a "database" problem, here. You want a set of persistent data
that stores subjects, students, and grades. This is also a "relational
database" problem, in that you have three things that are related.
If this is a "real" program to solve some task, then you should be
investigating database-based solutions. Perl gives you access to many,
including some free ones, such as MySQL and Postgres, and even some
perl specific solutions. Since I haven't used any of these, I won't
recommend any or even describe them. Perhaps someone who has used them
will suggest one. You can also search the archive of free, downloadable
Perl modules at www.cpan.org.
If this is just a learning program, then I have some additional
suggestions:
The data shown above has a somewhat form-free structure. You are better
off making it more structured and restrictive. Something like:
MATH,Sue,A
MATH,Mike,A
COMPUTER,DON,A
COMPUTER,Sue,B+
HISTORY,TOM,B+
Note that this is not as readable for a person, but it is much easier
for a program to parse. If you want a nicely-formatted report, you can
write a program to take this file and generate the report shown above.
Once you have a good representation of your data, the programming
becomes much easier.
>
>
> 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 .
You should be posting a complete, workable program that demonstrates
the problem you are having.
You should also always include the following lines at the beginning of
your program:
use strict;
use warnings;
>
> 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;
For the benefit of those trying to help you, none of whom ever forget
to get rid of the newline character from the input, please leave out
the unnecessary (for us) comments.
>
> 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);
You should not call subroutines using the ampersand unless you are
aware of the effect of that and need it.
>
>
>
> sub update {
> my $grade = $_[0];
Please use proper indenting to make your program more readable. Indent
loops, compound statements such as subroutines.
> my $subject = $_[1];
> my $name = $_[2];
my( $grade, $subject, $name ) + @_;
>
> #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);
Don't forget to get rid of the newline character from the input:
chomp(@in);
You are better off writing to an output file that is different than the
input file. This has two advantages: 1) you can have them both open at
the same time, which can make a difference when your dataset gets
really big, and 2) if your program crashes after you have opening the
output file, you won't wipe out your data. After you have successfully
written the output file, rename the input file as a backup set and then
rename the output file to the name of the input file.
>
>
> #Now open the same file again for output
> open (FILE, ">$Info") ||die "couldn't open $Info for writing";
open( FILE, ">$Info.tmp" ) or die("Couldn't open $Info.tmp for
writing: $!");
You should use "or" instead of "||" for precedence reasons and include
the reason ($!) in your message.
>
> #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 :(
Here, you would look for a match to subject, student, and grade. If you
use the modified data structure above, you could say:
my( $sub,$stu,$grd ) = split(',');
if( ($subject eq $sub) && ($name eq $stu) ) {
# you have found a duplicate entry
print "Duplicate entry for ($subject,$student)\n;
You could also replace the old entry with the new if that is what you
want:
$_ = "$subject,$student,$grade";
}
Since we got rid of the newline:
print FILE "$_\n";
> }
> close(FILE);
>
>
> Can someone advide please ? thanks much
HTH.
- Next message: Sandman: "Re: Double Jump Box Redirection"
- Previous message: fifo: "Re: using sed from with a perl script"
- In reply to: Sami: "complex regex"
- Next in thread: James Willmore: "Re: complex regex"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|