Perl regularexpression problem with the Input file

Discussion in 'Perl Misc' started by paul, May 11, 2007.

  1. paul

    paul Guest

    Hello,

    The following code is to read data from the file and store it into the
    hash-arrayreference, it seems to work for the data in the code but not
    for the file.
    ===== the file that the program reading is
    1- what is psychologist?
    a- study of mental processing
    b- study about human intelligent
    c- study both human and animal
    d- study biolpsychology
    2- what is neuron?
    a- a connection between cell body
    b- a electro chemical
    c- chimal reaction between oxygen and water
    d- part of soma
    ===============
    #!/usr/bin/perl

    use strict;
    use warnings;

    my $file_name = shift;
    my %question_answer_haf;

    open(OFILE, "$file_name") || die("Can't open the file: $!");

    while(<OFILE>){
    my $question;
    my @answers;
    #print "$2\n" if(/(\d)+\s*-\s*(.*)/) ;
    #print "$2\n" if(/(?i)(\w)+\s*-\s*(.*)/);

    if(/\d+\s*-\s*(.*)/) {
    $question = ${1};
    }
    if(/(?i)(\w)+\s*-\s*(.*)/) {
    push @{$question_answer_haf{$question}}, $2;
    }

    }

    close(OFILE);

    ==========================
    the work version,

    my %hashQuestion= (
    "1 - What is Psychology? "=>[ "A- study about mind and mental
    concept*",
    "B- is part of a seudopsychology",
    "C- about all the life and ",
    "d- none of the above"],
    "2- what is neuron? "=>[ "a- is an electro in the body* ",
    "b- a fuid in the boyd"]
    );

    my %hash2 ;

    foreach my $question1 (keys %hashQuestion) {
    print "$question1 \n";

    foreach my $i (0..$#{$hashQuestion{$question1}}) {
    print $hashQuestion{$question1}[$i] . "\n";
    push @{$hash2{$question1}}, $hashQuestion{$question1}
    [$i];
    }
    }


    thank
     
    paul, May 11, 2007
    #1
    1. Advertising

  2. paul

    Paul Lalli Guest

    On May 11, 10:19 am, paul <> wrote:
    > Hello,
    >
    > The following code is to read data from the file and store it into the
    > hash-arrayreference, it seems to work for the data in the code but not
    > for the file.
    > ===== the file that the program reading is
    > 1- what is psychologist?
    > a- study of mental processing
    > b- study about human intelligent
    > c- study both human and animal
    > d- study biolpsychology
    > 2- what is neuron?
    > a- a connection between cell body
    > b- a electro chemical
    > c- chimal reaction between oxygen and water
    > d- part of soma
    > ===============
    > #!/usr/bin/perl
    >
    > use strict;
    > use warnings;
    >
    > my $file_name = shift;
    > my %question_answer_haf;
    >
    > open(OFILE, "$file_name") || die("Can't open the file: $!");
    >
    > while(<OFILE>){
    > my $question;


    You're defining $question within the scope of a single iteration of
    the while loop. That means as soon as this iteration is done, this
    $question goes out of scope. On the next iteration, a brand new
    $question - still undefined - is created.

    > my @answers;
    > #print "$2\n" if(/(\d)+\s*-\s*(.*)/) ;
    > #print "$2\n" if(/(?i)(\w)+\s*-\s*(.*)/);
    >
    > if(/\d+\s*-\s*(.*)/) {
    > $question = ${1};


    $question is set in this iteration of the loop

    > }
    > if(/(?i)(\w)+\s*-\s*(.*)/) {
    > push @{$question_answer_haf{$question}}, $2;


    But this $question is a completely separate instance, and has nothing
    to do with the $question that was set above.

    > }
    >
    > }


    Move $question's definition to above the while loop.

    Also, change your second if() to an elsif, as \w matches digits as
    well, so you're going to be including the question as one of its own
    possible answers.

    Paul Lalli
     
    Paul Lalli, May 11, 2007
    #2
    1. Advertising

  3. paul

    Mumia W. Guest

    On 05/11/2007 09:19 AM, paul wrote:
    > Hello,
    >
    > The following code is to read data from the file and store it into the
    > hash-arrayreference, it seems to work for the data in the code but not
    > for the file.
    > ===== the file that the program reading is
    > 1- what is psychologist?
    > a- study of mental processing
    > b- study about human intelligent
    > c- study both human and animal
    > d- study biolpsychology
    > 2- what is neuron?
    > a- a connection between cell body
    > b- a electro chemical
    > c- chimal reaction between oxygen and water
    > d- part of soma
    > ===============
    > #!/usr/bin/perl
    >
    > use strict;
    > use warnings;
    >
    > my $file_name = shift;
    > my %question_answer_haf;
    >
    > open(OFILE, "$file_name") || die("Can't open the file: $!");
    >
    > while(<OFILE>){
    > my $question;
    > my @answers;


    $Question and @answers probably need to go outside of the while loop.

    > #print "$2\n" if(/(\d)+\s*-\s*(.*)/) ;
    > #print "$2\n" if(/(?i)(\w)+\s*-\s*(.*)/);
    >
    > if(/\d+\s*-\s*(.*)/) {
    > $question = ${1};
    > }


    This seems to eliminate the question number from the question.

    > if(/(?i)(\w)+\s*-\s*(.*)/) {
    > push @{$question_answer_haf{$question}}, $2;
    > }


    And this eliminates the answer letter.

    >
    > }
    >
    > close(OFILE);
    >
    > ==========================
    > the work version,
    >
    > my %hashQuestion= (
    > "1 - What is Psychology? "=>[ "A- study about mind and mental
    > concept*",
    > "B- is part of a seudopsychology",
    > "C- about all the life and ",
    > "d- none of the above"],
    > "2- what is neuron? "=>[ "a- is an electro in the body* ",
    > "b- a fuid in the boyd"]
    > );
    >
    > my %hash2 ;
    >
    > foreach my $question1 (keys %hashQuestion) {
    > print "$question1 \n";
    >
    > foreach my $i (0..$#{$hashQuestion{$question1}}) {
    > print $hashQuestion{$question1}[$i] . "\n";
    > push @{$hash2{$question1}}, $hashQuestion{$question1}
    > [$i];
    > }
    > }
    >
    >
    > thank
    >


    I also think that the questions and answers are not well worded;
    however, I suspect that you posted a simplified version of a program
    that is being written in another natural language.
     
    Mumia W., May 11, 2007
    #3
    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. Learning SQL Server

    Email RegularExpression not working

    Learning SQL Server, Aug 7, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    2,470
    Chris R. Timmons
    Aug 8, 2003
  2. Stephan Bour
    Replies:
    2
    Views:
    7,180
    Stephan Bour
    Oct 28, 2003
  3. =?Utf-8?B?bWc=?=
    Replies:
    3
    Views:
    945
    Peter Blum
    May 20, 2004
  4. John Hoge
    Replies:
    2
    Views:
    599
    John Hoge
    May 23, 2004
  5. ad
    Replies:
    2
    Views:
    3,774
    Tod Birdsall
    Jun 13, 2005
Loading...

Share This Page