-2

How to remove specific characters from an array elements in one line using Perl?

Example:-

In this array, the special character ' has to be removed.

@names = ("'Tom'","'Jerry'");

Expected Output :-

Tom
Jerry
5
  • 3
    The only quote marks in your code are the ones that are used to delimit your strings. They aren't special characters and they won't appear when you print those strings. So your question is rather confusing. Are you sure you're asking what you meant to ask?
    – Dave Cross
    Commented Mar 5, 2019 at 9:02
  • 1
    if you want absolutely no ' in your code you can use @names = qw(Tom Jerry); but the result will be the same Commented Mar 5, 2019 at 13:01
  • @DaveCross yes,i am sure about my question..In my array,I have each element(Strings) with the single quotes character since the data which is stored in array has single quotes from the input...It also printing the contents with single quote characters.. Commented Mar 10, 2019 at 13:46
  • 1
    @Crazycoder: Ok. But that's not what your question shows. What you meant was @names = ("'Tom'", "'Jerry'") or @names = qw('Tom', 'Jerry'). Your code doesn't put quotes in the array elements.
    – Dave Cross
    Commented Mar 10, 2019 at 17:03
  • @DaveCross Yes,you got my point Dave.I apologize for the mistake. Commented Mar 11, 2019 at 4:28

3 Answers 3

4

The string literal 'Tom' produces the string Tom, and the string literal 'Jerry' produces the string Jerry, so there is no ' in any of the elements of that array.

To print each element on a different line, you can use the following:

say for @names;

It a short version of the following:

for my $name (@names) {
   say $name;
}

This usually goes without saying, but using say requires using use feature qw( say );.

1

You can make this change in perl by negating the "specialness" of the character by placing a slash in front of it in the regex. So the regex for the variable could be s/\'//g. An example could be as follows:

#!/usr/bin/perl
@names=('\'Tom\'','\'Jerry\'');
foreach my $name (@names) {
 print "Name is $name\n";
 $name =~ s/\'//g;
 print "After regex, Name is $name\n";
}

In your array declaration with the single quotes though, perl will interpret those as simple quoting your inputs and will not include them in the output. I needed to add the negation of the single quote to produce the results which are:

Name is 'Tom'
After regex, Name is Tom
Name is 'Jerry'
After regex, Name is Jerry
2
  • 1
    Your regular expression in your code helps me to remove single quotes from the array.. Commented Mar 10, 2019 at 13:49
  • 1
    @Ribtips: Single-quote characters don't have a special meaning in a regex, so there's no need to escape them there. $name =~ s/'//g will work fine.
    – Dave Cross
    Commented Mar 11, 2019 at 9:02
1

The code and your question is different. But looking at the Output you want i will assume that you want to join/print the elements of the array. Consider the example code:

Code:

@names = ('Tom','Jerry');
my $str = join '\n', @names;
say $str;

Output:

Tom
Jerry

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.