Help; Pattern Matching

M

MJS

The following is what I have and I can't figure out how to do it. I
read faq and I don't get the output what I intent to. I am having
difficulties with proper indentation and white/blank lines.

User has to enter an integer. Depending on that, the pattern should be
repeated the number of times the user specified. Everytime the pattern
is repeated, there should be an increament somewhere in the pattern.
The output is stored in a file.

For example: the exact pattern with the spaces is

"Can someone help
me with


problem number1 => "

This pattern should be replaced by
"This is
solution


number 1, <= "

"This is
solution


number 2, <= "

and so on depending on the user input.
========================================================
use strict;
my $number;
my $pattern;
my $replace;
# data.txt is the file containing the text pattern
open(OUTPUT,"> result.txt") or die("Couldn't open data.txt :$!");

print "Please enter a positive integer for Address = ";
chomp( my $number = <STDIN> );

if ($number =~ tr/1-9// != length ($number)) {
print "You did not enter an interger";
}

else {
$pattern=~/Can someone help
me with


problem number1 =>/;

$replace=~/"This is
solution


number /;
while(<OUTPUT>){
for (my $n=1; $n<=$number; $n++){
s/$pattern/$replace."$n". '<='/gsm;
}
}
}
close(OUTPUT);
======================================
 
M

MJS

The following is what I have and I can't figure out how to do it. I
read faq and I don't get the output what I intent to. I am having
difficulties with proper indentation and white/blank lines.

User has to enter an integer. Depending on that, the pattern should be
repeated the number of times the user specified. Everytime the pattern
is repeated, there should be an increament somewhere in the pattern.
The output is stored in a file.

For example: the exact pattern with the spaces is

"Can someone help
me with


problem number1 => "

This pattern should be replaced by
"This is
solution


number 1, <= "

"This is
solution


number 2, <= "

and so on depending on the user input.
========================================================
use strict;
my $number;
my $pattern;
my $replace;
# data.txt is the file containing the text pattern
open(OUTPUT,"> result.txt") or die("Couldn't open data.txt :$!");

print "Please enter a positive integer for Address = ";
chomp( my $number = <STDIN> );

if ($number =~ tr/1-9// != length ($number)) {
print "You did not enter an interger";
}

else {
$pattern=~/Can someone help
me with


problem number1 =>/;

$replace=~/"This is
solution


number /;
while(<OUTPUT>){
for (my $n=1; $n<=$number; $n++){
s/$pattern/$replace."$n". '<='/gsm;
}
}
}
close(OUTPUT);
======================================

Any Help????????
 
G

Gunnar Hjalmarsson

MJS said:
The following is what I have and I can't figure out how to do it. I
read faq and I don't get the output what I intent to. I am having
difficulties with proper indentation and white/blank lines.

I have a feeling that you're having difficulties with much more than
that... Actually, I was about to reply to your message before you
reposted after less than 10 hours, but I didn't know where to start,
so I didn't.

I think you need to both structure the problem better and learn some
more Perl before you are ready to accomplish what you are trying to
do. As far as I understand, the code you posted isn't even close to
what you say that you want the script to do.

Questions about well defined problems are welcome in this group, at
least as long as they are not FAQs, but requests for help, that in
effect would be equal to doing the whole work, are not.

To summarize, I think you need to do some homework before asking for
help here.

http://learn.perl.org/
 
M

MJS

Rather than telling me to read the stuff, why doesn't anybody tell me
where I am doing wrong. After reading the stuff, thats what I came up
with though I admit I lack experience. I still appreciate what you
have said as you definitely spend your precious time on reply. Thanks
a lot.
 
G

Gunnar Hjalmarsson

MJS said:
Rather than telling me to read the stuff, why doesn't anybody tell
me where I am doing wrong.

I would guess it is because you are not doing enough of it right.

If you are really interested in learning programming, especially
programming in Perl, and provided that you show that you are making
efforts to learn, there are quite a few very experienced people here
who are willing to give a hand.
After reading the stuff, thats what I came up with though I admit I
lack experience.

A couple of examples:

- You mention a file named data.txt, where some pattern is supposed to
be stored, but you don't open that file.

- There are both syntax and logic errors, which at least make me
confused about what exactly you are trying to do.

You simply need to try harder. Also, reading the posting guidelines
for this group may give you a better feeling for how to ask questions
here (later on):
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
 
J

Jay Tilton

(e-mail address removed) (MJS) wrote:

: Rather than telling me to read the stuff, why doesn't anybody tell me
: where I am doing wrong.

Like Gunnar said, there are so many mistakes that it is hard to know
where to begin. Many of the mistakes are of such a basic nature (like
confusing the binding operator with the assignment operator) that the
most logical place to begin is by reading the docs.

IOW, you need to understand what each piece does before trying to
construct a complete program from them.

Also, that the problem is so urgent that it deserved a repost within ten
hours must have raised the "homework problem" red flag in many readers'
minds. It is, after all, still September.

[ content taken from earlier message,
Message-ID: <[email protected]> ]

: use strict;

use warnings;

That addition alone would have caused several messages to be emitted.
Those messages are an invaluable guide to debugging.

: my $number;
: my $pattern;
: my $replace;

Better to declare lexicals within the smallest scope necessary than to
give them all file scope like that.

: # data.txt is the file containing the text pattern
: open(OUTPUT,"> result.txt") or die("Couldn't open data.txt :$!");
^^^^^^^^^^ ^^^^^^^^
Which filename is correct?

: print "Please enter a positive integer for Address = ";
: chomp( my $number = <STDIN> );
: if ($number =~ tr/1-9// != length ($number)) {
: print "You did not enter an interger";
: }

Integers are not allowed to contain the digit 0 ?

A better way to disallow non-numeric characters is to simply check that
the string does not contain them.

if( $number =~ /\D/ ) { .... }

: else {
: $pattern=~/Can someone help
: me with
:
:
: problem number1 =>/;

You want to assign a value to $pattern, not bind the match operator to
it.

my $pattern = "Can someone help....";

: $replace=~/"This is
: solution
:
:
: number /;

Ditto.

my $replace = "This is solution....";

: while(<OUTPUT>){

The OUTPUT filehandle was opened for output only. Why is the program
trying to read from it? Are you really trying to do an in-place edit of
the file?

: for (my $n=1; $n<=$number; $n++){

Blecch.

for my $n (1..$number) {

: s/$pattern/$replace."$n". '<='/gsm;
^^^^
See perlfaq4, ' What's wrong with always quoting "$vars" '

Assuming for a moment that the filehandle was open for input and a
record was read into $_, that record will contain only one line. Your
pattern spans several lines. The match will never succeed.

Assuming for a moment that you had slurped the entire file content into
$_ and the match has a chance of succeeding, the s/// operator will do
nothing after the first iteration, since you have irreversibly mangled
$_. Copy the value of $_ into another scalar then mangle the copy.

The replacement string, $replace."$n". '<=' , is code. Either make it
an ordinary double-quotish string, e.g.

s/$pattern/$replace$n<=/g;

or tack the /e switch onto the s/// operator so the replacement string
gets evaluated as code.

On the s/// operator, the /m switch only affects the meaning of the ^
and $ regex metacharacters and the /s switch only affects the meaning of
the . regex metacharacter. None of those characters appear in the
pattern, so those switches can be discarded.

Did you mean to output the altered string somewhere?

: }
: }
: }
: close(OUTPUT);
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top