sud100 has asked for the wisdom of the Perl Monks concerning the following question:

i have a textfile which contains data as below. asd" efg" i want to remove double quote. Please suggest me perl code for this. I tried with $_=~ s/"//g; but gives an error
  • Comment on perl script to remove part of double quote

Replies are listed 'Best First'.
Re: perl script to remove part of double quote
by AnomalousMonk (Archbishop) on May 15, 2017 at 05:20 UTC
Re: perl script to remove part of double quote
by kcott (Archbishop) on May 15, 2017 at 05:39 UTC

    G'day sud100,

    Welcome to the Monastery.

    In the absence of more complete information, transliteration (see "perlop: Quote-Like Operators") is likely to be the fastest, and most straightforward, way to achive this.

    $ perl -E 'my $x = q{asd" efg"}; say $x; $x =~ y/"//d; say $x' asd" efg" asd efg

    — Ken

Re: perl script to remove part of double quote
by thanos1983 (Parson) on May 15, 2017 at 08:52 UTC

    Hello sud100,

    I did a very basic search using the following search text on Google "perl how to remove double quotes from string".

    The first result that came up is How to remove double quotes?.

    If I was you, in case that I have a problem and I want to learn a bit more about programming, I would search a bit more and I would test multiple possible solutions not just one.

    I am pretty sure that it took you much more time to post this question on this forum than actually make a search and test the code for your self.

    The following pieces of code are taken from the link above:

    #!/usr/bin/perl use strict; use warnings; use Benchmark::Forking qw( timethese cmpthese ); # UnixOS # use Benchmark qw(:all) ; # WindowsOS my $string = q{asd" efg"}; sub Solution_1 { # Solution 1 $string =~ tr/"//d; # print $string . "\n"; # asd efg } sub Solution_2 { # Solution 2 $string =~ tr/"/'/; # print $string . "\n"; # asd efg } sub Solution_3 { # Solution 3 $string =~ s/^"(.*)"$/$1/; # print $string . "\n"; # asd efg } sub Solution_4 { # Solution 4 $string =~ s/"/'/g; # print $string . "\n"; # asd efg } my $results = timethese(10000000, { Solution_1 => \&Solution_1, Solution_2 => \&Solution_2, Solution_3 => \&Solution_3, Solution_4 => \&Solution_4, }, 'none'); cmpthese( $results ); __DATA__ $ perl test.pl Rate Solution_3 Solution_1 Solution_2 Solution_4 Solution_3 7352941/s -- -63% -65% -65% Solution_1 20000000/s 172% -- -6% -6% Solution_2 21276596/s 189% 6% -- 0% Solution_4 21276596/s 189% 6% 0% -- =comment Would anyone care to share why perl can do tr/"/'/ faster than s/"/'/? Because s/// has to be able to handle all RE syntax and specialness wh +ile tr/// just considers its input to (mostly) be a simple character class +. Much easier to deal with and, thus, much faster. =cut

    Update: I read your question again, and I assume from the part of your explanation I tried with $_=~ s/"//g; but gives an error that the error is coming from somewhere else. Since $_=~ s/"//g; it does more or less of what you expect (removing the double quotes) as AnomalousMonk proved above is your error (Use of uninitialized value $_ in substitution (s///) at <your script> <line number>)?

    Something like that?

    #!/usr/bin/perl use strict; use warnings; sub test_subroutine { $_[0] =~ s/"//g; return $_[0]; } my $string = q{asd" efg"}; my $string_formatted = test_subroutine($string); print $string_formatted . "\n"; # asd efg

    Help us to help you, provide us sample of your code that replicated the error.

    Hope this helps.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: perl script to remove part of double quote
by Marshall (Canon) on May 16, 2017 at 04:16 UTC
    $_=~ s/"//g; but gives an error

    A string substitute operation like this will not yield an error except in the case where the string that is being processed is undefined. In addition, $_=~ is not needed as the substitute operator will work on $_ by default. Likely cause of your problem is that you are not setting the default $_ variable. Show your code.