Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

perl - I need to replace Subjects ex :- Subjects :***** but it is giving the output :- Subjects :*****Computer

Here you can see my code is replacing the subjects with ***** but what is happening is it is not replacing the second line. I have been trying to do this, but failed every time. if anyone could help me out please check

The output I want is:

Name : Piyush Prasad
Class : 12th
Stream : Science
Subjects :*****
Text : what ever happens to this code does not even matter to me 
Time :*****

and the output what I am getting is:

Name : Piyush Prasad
Class : 12th
Stream : Science
Subjects :*****Computer
Text : what ever happens to this code does not even matter to me 
Time :***** 

Here is the code:

 my $fm_log= "
 -------------------------------------------------------------------------------
 Name : Piyush Prasad
 Class : 12th
 Stream : Science
 Subjects : Physics Chemistry Maths
            Computer
 Text : what ever happens to this code does not 
        even matter to me 
 Time : 10.304534
 ";
 my %fm_info;
 $fm_log =~ s!.*?(---------------)!$1!s;
 $fm_log =~ s!vwmg.*!!sg;
 my @fm_log = split(/-------+/, $fm_log);
 @fm_log = grep {! m!^s*$!s} @fm_log;
 for (my $i = 0; $i < @fm_log;$i++){
     my $type = undef;
     my @tmp1 = split(/R+/,$fm_log[$i]);
     foreach (@tmp1)
       {
         $_ =~ s!^s+!xxxx! if $_ !~ m!:!;
         $_ =~ s!(Times+:)s+.*!${1}xxx!;
         $_ =~ s!(Subjectss+:)s+.*!${1}xxx!;
       }
      $fm_log[$i] = join("
",@tmp1);
      $fm_log[$i] =~ s!R+xxxx!!mg;
       }
      print @fm_log;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The reason you are getting

Subjects :*****Computer

instead of

Subjects :*****

Is that you split the original string on linebreaks R+ and then edit the lines, after which you join the string back together. Since Computer is on a new line, it does not get edited.

 my @tmp1 = split(/R+/,$fm_log[$i]);         # <---- splitting the line
 foreach (@tmp1) {                            # editing the pieces
     $_ =~ s!^s+!xxxx! if $_ !~ m!:!;
     $_ =~ s!(Times+:)s+.*!${1}xxx!;
     $_ =~ s!(Subjectss+:)s+.*!${1}xxx!;
 }
 $fm_log[$i] = join("
",@tmp1);              # joining the pieces

In other words, if you have a string such as:

my $str = "foo: bar
baz"; 

Then split the string on newline and try to remove everything after foo:

my @pieces = split /R+/, $str;

Now @pieces will contain this:

$VAR1 = [
      'foo: bar',
      'baz'
    ];

And if we try to edit them with s/foo:.*//, we will never affect the baz part of the original string.

Since you have a file that contains fields separated by lines that start with word(s) followed by a colon, followed by content that may contain newlines, what you need is to first separate the fields from each other.

Your fields look something like...

TITLE : CONTENT
        CONTENT ...
TITLE : CONTENT ...

There may be modules that read this format that are more suitable. I made up this quick hack to coerce the data into a hash, where you can edit the fields by simply assigning new values to them.

use strict;
use warnings;
use Data::Dumper;

my $fm_log= " Name : Piyush Prasad
 Class : 12th
 Stream : Science
 Subjects : Physics Chemistry Maths
            Computer
 Text : what ever happens to this code does not 
        even matter to me 
 Time : 10.304534
 ";

my @info = split /^ *([w ]+) : /m, $fm_log;   # split into fields
s/s+/ /g for @info;                           # cleanup start
s/^s+|s+$// for @info;
shift @info;                                   # cleanup complete
my %info = @info;
print Dumper \%info;                 # show the data structure we made

$info{Subjects} = "****";
$info{Time} = "****";

printf("%s : %s
", $_, $info{$_}) for qw(Name Class Stream Subjects Text Time);

This will output

$VAR1 = {
          'Class' => '12th',
          'Stream' => 'Science',
          'Time' => '10.304534',
          'Subjects' => 'Physics Chemistry Maths Computer',
          'Text' => 'what ever happens to this code does not even matter to me',
          'Name' => 'Piyush Prasad'
        };
Name : Piyush Prasad
Class : 12th
Stream : Science
Subjects : ****
Text : what ever happens to this code does not even matter to me
Time : ****

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...