0

I am attempting to remove 3 commas from the file below but it is not working.

#!/usr/bin/perl

use strict;
use warnings;

my @data;
my @line;

open(FH, "error_log");
@data = 

foreach $line (@data) {
   if ($line =~ /notice/) {
      if ($line =~ /rdy/) {
        $line =~ s/ /,/g;
        my @L1 = split(/notice|[[]|mpmstats:[\t]/, $line);
        foreach $line (@L1) {
           $line =~ s/,,,/,/g;
           print @L1;
           }
        }
     }
  }

My output is as follows.
Wed,Jun,13,10:23:35,2012,,,rdy,769,busy,31,rd,0,wr,22,ka,6
Wed,Jun,13,10:42:57,2010,,,rdy,758,busy,41,rd,0,wr,54,ka,5

Why is the s/,,,/,/g; not substituting the single comma?

3
Contributors
2
Replies
3
Views
4 Years
Discussion Span
Last Post by 2teez
0

This looks odd to me:

foreach $line (@L1) {
    $line =~ s/,,,/,/g;
    print @L1;
}

Sure you want to print all of @L1 every time through that loop?

As for your output, I can't tell what you expect it to do. Could you post your input and describe what you expected to happen?

0

Hi rupes0610,

Check the following:
Check your line 10, you probably omited something like:

@data = <FH>;

You didn't close the open file handler, neither did you test for failure.
I also think line 19, should be something like:

print $line;

not

 print @L1;

Moreover, I believe you can do all of these within a single while loop in an open function with no need to slurp all your input into array variable.
Show your input then let's have fun!

However, if you must have your code just like what you gave above. You can still achieve your aim by using tr///s like so:

#!/usr/bin/perl
use warnings;
use strict;

 while(<DATA>){
    chomp;
    tr/,,,/,/s;
   print $_,$/;
 }

__DATA__
Wed,Jun,13,10:23:35,2012,,,rdy,769,busy,31,rd,0,wr,22,ka,6
Wed,Jun,13,10:42:57,2010,,,rdy,758,busy,41,rd,0,wr,54,ka,5

OUTPUT:
Wed,Jun,13,10:23:35,2012,rdy,769,busy,31,rd,0,wr,22,ka,6
Wed,Jun,13,10:42:57,2010,rdy,758,busy,41,rd,0,wr,54,ka,5

This is Perl, you can make anything possible, but you also have to live with them!
Enjoy, hopes this help.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.