complex regex

Discussion in 'Perl Misc' started by Sami, Feb 27, 2004.

  1. Sami

    Sami Guest

    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
    Sami, Feb 27, 2004
    #1
    1. Advertising

  2. On Fri, 27 Feb 2004 07:51:36 -0800, Sami 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
    >
    >
    > 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
    James Willmore, Feb 27, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news.amnet.net.au
    Replies:
    1
    Views:
    574
    =?UTF-8?b?TMSByrtpZSBUZWNoaWU=?=
    Apr 13, 2004
  2. Stanimir Stamenkov
    Replies:
    2
    Views:
    744
    Stanimir Stamenkov
    Oct 25, 2005
  3. Robert Mark Bram
    Replies:
    0
    Views:
    682
    Robert Mark Bram
    Feb 4, 2007
  4. Replies:
    3
    Views:
    759
    Reedick, Andrew
    Jul 1, 2008
  5. Kottiyath

    How complex is complex?

    Kottiyath, Mar 18, 2009, in forum: Python
    Replies:
    22
    Views:
    761
Loading...

Share This Page