Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Perl regularexpression problem with the Input file

Reply
Thread Tools

Perl regularexpression problem with the Input file

 
 
paul
Guest
Posts: n/a
 
      05-11-2007
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

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      05-11-2007
On May 11, 10:19 am, paul <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Mumia W.
Guest
Posts: n/a
 
      05-11-2007
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to set the RegularExpression ad ASP .Net 2 06-13-2005 10:47 PM
RegularExpression validator that does not allow blanks John Hoge ASP .Net 2 05-23-2004 05:26 PM
RegularExpression code for server-side validation of Date =?Utf-8?B?bWc=?= ASP .Net 3 05-19-2004 11:03 PM
Regularexpression validator on multiline text box Stephan Bour ASP .Net 2 10-28-2003 09:42 PM
Email RegularExpression not working Learning SQL Server ASP .Net 4 08-08-2003 08:51 AM



Advertisments