2

I have a one array . I want the array to be such that it does not have any values of $regex (in a logical OR way). I am using grep command in a negation way, but I don't think this is solving my problem. Any help is highly appreciated. Thanks

 #!/usr/bin/perl -w 

use Data::Dumper;

    my @array = ['hard_link is not present', 'dynamic variable', 'segfault'] ;

    my $regex = qr/create_hard_link|Failed to reassign|Global variable/ ;

    print Dumper(\@array) ;
    my @wanted_array = grep  {!$regex} @array ;
    print Dumper(\@wanted_array);

it gives me an output as

$VAR1 = [
          [
            'hard_link is not present',
            'dynamic variable',
            'segfault'
          ]
        ];
$VAR1 = [];
6
  • 2
    Also, [ ... ] creates an array reference. You want @array = ( ... ). So @array = [ ... ] creates that arrayref and assigns it to the first element of @array. I figure that this isn't what you want. Commented Jul 24, 2017 at 20:50
  • Then, I suggest to go through your other questions and vote (now you have enough credit for that) and accept as suitable. This may or may not relate to strange downvotes on your current question. I am not posting this comment there so to not perhaps aggrevate that furtther. Let me know when you have seen this. Commented Sep 21, 2017 at 21:29
  • @zdim how is my voting on other question help with the current downvote situation ? Commented Sep 21, 2017 at 21:38
  • Honestly I have no idea what's going on with that question. And other questions shouldn't matter for voting on that one, of course, but life's not that simple as we know. In my experience people don't retract downvotes ... but it may help future questions. If that's what caused it -- I'm guessing because I don't understand those votes (on a good question). But then again, by reviewing your other questions and voting (as appropriate) you do them justice anyway. Commented Sep 21, 2017 at 21:43
  • Alright got it. Thanks for your explanation, one downvote was retracted there, the other I dont know. But I will try to vote other answers as well. And I agree life is not simple mate :) Commented Sep 21, 2017 at 21:46

2 Answers 2

4

Unlike the command line tool by the same name, grep doesn't take a regex pattern; it takes an expression that evaluate to a true value for the items to keep.

$regex has a true value —the compiled regex pattern doesn't stringify to 0 or the empty string— so !$regex is always false, so grep filters everything out. You want to perform a regex match, so you will need the regex match operator.

my @wanted_array = grep  { !/$regex/ } @array;

There is a second problem.

# An array that contains a single element: a reference to an anonymous array.
my @array = ['hard_link is not present', 'dynamic variable', 'segfault'];

should be

# An array that contains a three strings.
my @array = ('hard_link is not present', 'dynamic variable', 'segfault');
Sign up to request clarification or add additional context in comments.

Comments

2

First off, don't use -w in scripts (unless you're a time traveler from before 2000).

Always start your Perl files with use strict; use warnings;.

The problem in your code is that you're using !$regex as the filter condition. $regex contains a regex object, which is a true value, so negating it returns false. This is why you end up with an empty array.

Fix: Actually do a regex match:

my @wanted_array = grep { !/$regex/ } @array;

(This uses the match operator m// (but the m is optional if you're using / as the delimiter).)


The other problem is that your @array only contains a single element, which is a reference (to another array).

You want

my @array = ('hard_link is not present', 'dynamic variable', 'segfault');

[ ... ] is a single scalar value.

6 Comments

thank you @ikegami , melpomene and zdim, it worked. However I am noticing that even if I pass $regex as a string, it works the same way. I am wondering if it would be a good idea
Sorry for the above incomplete comment, However I am noticing that even if I pass $regex as a string, it works the same way. I wonder how 'my $regex = qr// ' playing a different role. I am wondering if it would be a good idea? for example : my $regex = 'hard_link|Failed to reassign|Global variable|segfault'; gives the same output as before
@zdim no, try it and see. The reason to use qr is to compile the regex earlier and guarantee that it only needs to be compiled once
@SinanÜnür Yeah, thank you ... I've been through this once already. It trips me up that some things regex work with ''. I just prefer '' for literals and qr for a regex. Always.
@hitman99 My previous comment was wrong, as in this case the '' works just like qr does. However, they are very different as qr is meant precisely for regex. See qr in perlop.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.