Get the piece of code in perl

M

Maruthi Reddy

Hi All,

I want to get the pieces of code from the contents of file.
I can push the file contents into the array, But not getting some to
way to get the required piece of code.

My file looks something like

-------------------File start-------------------
............... ............. ... ... .............
................. ..... ........ ........... ......
AAA..... ............. ............. .........
............... .......... ............. .......
BBB ........ ........... ........ ...........
............ .......... .......... ...............
....... .......... .......... ........... ..........
AAA .... ........... .......... ............ .
........... ........ .............................
.............................. ..............
BBB ...........................................
.............................. ........... .....
----------------------File end ----------------

Here i would like to get the piecc of code between AAA and BBB.
Please suggest me.

Thanks,
mhm reddy.
 
P

Paul Lalli

Hi All,

I want to get the pieces of code from the contents of file.
I can push the file contents into the array, But not getting some to
way to get the required piece of code.

My file looks something like

-------------------File start-------------------
.............. ............. ... ... .............
................ ..... ........ ........... ......
AAA..... ............. ............. .........
.............. .......... ............. .......
BBB ........ ........... ........ ...........
........... .......... .......... ...............
...... .......... .......... ........... ..........
AAA .... ........... .......... ............ .
.......... ........ .............................
.............................. ..............
BBB ...........................................
............................. ........... .....
----------------------File end ----------------

Here i would like to get the piecc of code between AAA and BBB.
Please suggest me.

There's about 10 different ways to do this. Here's one:

my $file = do { local $/; <DATA> };
my @codes = ($file =~ /AAA(.*?)BBB/sg);
foreach $code (@codes) {
print "$code\n\n";
}

Paul Lalli
 
A

asimsuter

There's about 10 different ways to do this. Here's one:

my $file = do { local $/; <DATA> };
my @codes = ($file =~ /AAA(.*?)BBB/sg);
foreach $code (@codes) {
print "$code\n\n";

}

Paul Lalli

I am pasting some code below that needed a similar thing. Hope its
useful to you. Note m modifier for multiline search which is a key
element.

Regards.
Asim Suter
(e-mail address removed)


my $MATCH = qq<$STR(.*?)endif> ; # non-greedy matching
if( $whole_file =~ m/$MATCH/msg ) # g for pos
{
my $inter = $1 ;
my $TO_ADD = $ADD . $inter . "\nendif\n" ;
print "$TO_ADD" ;
my $p = pos $whole_file ;
print "POSITION:<$p>" ;

my $NEW_FILE_CONTENT = substr ( $whole_file , 0 , $p ) .
"\n\n" .
$TO_ADD .
substr ( $whole_file , $p ) ;


seek( FH , 0, 0 ) or die "Can't seek to
start of \"$fileName\": $!" ;
print FH $NEW_FILE_CONTENT or die "Can't print to
\"$fileName\": $!" ;
truncate ( FH , tell(FH) ) or die "Can't truncate
\"$fileName\": $!" ;
print LOGFILE "MODIFYING <$fileName>\n" ;

}
 
T

Tad McClellan

Note m modifier for multiline search which is a key element.
^^^^^^^^^^^^^^^^

No it isn't.

The m modifier changes the meaning of the ^ and $ anchors.

The m modifier is a key element only if your pattern contains
the ^ or $ anchors.

Regards.
Asim Suter
(e-mail address removed)


Signatures are customarily included at the _end_ of a posting
rather than in the middle.

print "$TO_ADD" ;


perldoc -q vars

What’s wrong with always quoting "$vars"?

so that should be:

print $TO_ADD ;

my $NEW_FILE_CONTENT = substr ( $whole_file , 0 , $p ) .
"\n\n" .
$TO_ADD .
substr ( $whole_file , $p ) ;


seek( FH , 0, 0 ) or die "Can't seek to
start of \"$fileName\": $!" ;
print FH $NEW_FILE_CONTENT or die "Can't print to
\"$fileName\": $!" ;
truncate ( FH , tell(FH) ) or die "Can't truncate
\"$fileName\": $!" ;


If you think that the truncate() call is necessary there, then
you are mistaken on that point as well.
 

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