Pattern Matching ....

M

MJS

How can the following pattern be matched and replaced by something
similar to it. After the pattern has been replaced, the replacing
pattern is repeating itself for certain number of times.

"The puzzle is :

go to your left=> continue

go to your right <= [ 20: 909]
continue[7:0] "


move.

This is how exactly how the text is written(including the spaces).

I can't figure out how to do it. Please help.

I tried something like this and didn't work:

===========================================================
# tie @array to filename using Tie::File
tie my @array, 'Tie::File', 'result.txt' or die "Cannot open
result.txt:$!";

while(<reading the text file>){

for($firstindex; $firstindex >= 0; $firstindex--){

$n++;

for ( 0 .. $#array ) {
my $string ="the above mentioned pattern"
if ( $array[ $_ ] =~ /string/ ) {
my $new_string= some similar pattern.
splice @array, $_ +1, 0, /the_string/;
last;
}
}
}

untie @array;
=============================================
 
J

Jürgen Exner

MJS said:
How can the following pattern be matched and replaced by something
similar to it.

Ok, so far so good
After the pattern has been replaced, the replacing
pattern is repeating itself for certain number of times.

Don't understand that part at all. Are you saying that the matched text
should be replaced by x copies of the new text?
Or are you saying the new text occures several times in the original file.
But then would should be done to it, should it be ignored or deleted or
replaced or ...?
Or do you mean something completely different?
"The puzzle is :

go to your left=> continue

go to your right <= [ 20: 909]
continue[7:0] "


move.

This is how exactly how the text is written(including the spaces).

I can't figure out how to do it. Please help.

I tried something like this and didn't work:

===========================================================
# tie @array to filename using Tie::File
tie my @array, 'Tie::File', 'result.txt' or die "Cannot open
result.txt:$!";

while(<reading the text file>){

Is your file handle really called 'reading the text file'? I would have
assumed this would yield a syntax error?

And while I've never used Tie::File myself it is my understanding that
Tie::File already ties the file's content to the array. Why are you reading
the content of the file again in the while loop (if that is what you are
trying to do; you are not showing your real code, so at best this is a
guess). I think this is just superfluous and I don't know if it will
interfere with Tie::File.
for($firstindex; $firstindex >= 0; $firstindex--){

You never declare or define $firstindex. You are using strictures and
warnings, aren't you? They should have told you that this line won't work.

And you are not using $firstindex anywhere in your code anyway. If you want
a loop to repeat e.g. 50 times then just say so:
for (0..49) {

What is this $n doing here? You never ever use $n anywhere else in your
code.
for ( 0 .. $#array ) {

It appears you are only interested in the content of the array, why do you
need the index? A simple
for (@array) {
should be easier to manage.
my $string ="the above mentioned pattern"
if ( $array[ $_ ] =~ /string/ ) {

Then this line becomes
if (/$string/) {...
my $new_string= some similar pattern.

This doesn't even compile, syntax error.
Maybe you meant
my $new_string= 'some similar pattern.';
splice @array, $_ +1, 0, /the_string/;
last;

What the heck are these two lines supposed to do?
At the beginning you said you want to replace a pattern with some text.
Well, then just do so:
s/$string/$new_string/;

From your description I can't tell if maybe you need the "g" modifier, too.
In any case, that should replace the

jue
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top