array question (grep -v on array)

Discussion in 'Perl Misc' started by NetComrade, Jul 28, 2003.

  1. NetComrade

    NetComrade Guest


    I have an output of errors fed into an array, after which I only look
    at things I care about and put them in a different array:

    @oracle_errors =

    However, I would like to have (yet another array), e.g. @ignore_errors
    which will contain values such as
    ORA-error can be ignored
    ORA-error2 can be ignored

    How can I set that up?

    Alternatively, (if simpler), i'd like to add some kind of 'grep -v'
    functionality to my grep command (but the array method seems sweeter).

    We use Oracle on Solaris 2.7 boxes
    remove NSPAM to email
    NetComrade, Jul 28, 2003
    1. Advertisements

  2. What do you mean by that? What would be the desired semantics of

    If you are talking about an analogue of gnugrep -vf then I'd assume
    @ignore_errors was an array of regex (all of which must be unmatched).
    You could combine the patterns into one...

    my $ignore_error_pattern = join '|', map "(?:$_)", @ignore_errors;
    $ignore_error_pattern = qr/$ignore_error_pattern/;

    ....and then use...

    my @oracle_errors = grep !/$ignore_error_pattern/, @output;

    However, whilst this may look elegant, it turns out that it is not

    You are better off just doing it with explicit nested loops.

    That is, unless you really don't need @ignore_errors to be an arbirary
    array of regex.

    If you can instead make @ignore_errors be a simple array of strings
    that are going to looked for in a easily recognied place in the line
    (like, say, at the first ocurance of 'ORA-') there's a much more
    efficient solution.

    my %ignore;

    my @oracle_errors = grep { ! ( /(ORA-\S+)/ && exists $ignore{$1) ) } @output;
    I drive a Ford Escort on the LHS of the road :)

    \\ ( )
    . _\\__[oo
    .__/ \\ /\@
    . l___\\
    # ll l\\
    ###LL LL\\
    Brian McCauley, Jul 28, 2003
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.