http://www.perlmonks.org?node_id=1184988

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

Monks
I have a small bit of code to find RPM's via RHNS

However it currently shows any matching RPM
i.e
hostname1 redhat-release 5.11.0.4
hostname1 redhat-release-notes 52
hostname2 redhat-release 5.11.0.4

What I need is to find a way to exclude the match on "notes"

for (@{$systems}) { my $systemid = $_->{'id'}; ## package test my $packages = $client->call('system.list_packages', $session, $sys +temid); for my $package (@$packages) { ## the next line limits to the exact rpm you wish or else you get ever +ything next unless $package->{'name'} ~~ /redhat-release/; my $systemname=$_->{'name'} . " "; chomp $systemname; print $systemname . " " ; print $package->{'name'} . " "; print $package->{'release'}; print "\n"; } }

Can any kind Monk help , to exclude "notes"
Thanks

Replies are listed 'Best First'.
Re: match exclude
by Eily (Monsignor) on Mar 17, 2017 at 09:16 UTC

    This can be done with a look-ahead assertion which allows you to express something like "that is followed by", or "that is not followed by". /redhat-release(?!-note)/ should do what you want.

      Eily
      You are indeed a true Monk
      Spot on with your answer , that worked nicly.
      Thanks.
Re: match exclude
by hippo (Bishop) on Mar 17, 2017 at 08:57 UTC
    next unless $package->{'name'} ~~ /redhat-release/;

    Are you able to explain why you are using smartmatch on this line? The reason is not obvious to me. If, as the comment suggests you would prefer an exact match then use that instead:

    next unless $package->{'name'} eq 'redhat-release';
      Perhaps I should have given a bit more in the list

      abe-n-s00008 redhat-release 5.11.0.4 abe-n-s00008 redhat-release-notes 52 abe-n-s00009 redhat-release 5.11.0.4 abe-n-s00009 redhat-release-notes 52 abe-n-s00010 redhat-release 5.11.0.4 abe-n-s00010 redhat-release-notes 52 abe-n-s00070 redhat-release 5.11.0.4 abe-n-s00070 redhat-release-notes 52 abe-n-s00070 redhat-release-server 6.8.0.5.el6

      Redhat-release can also be redhat-release-server

      What I want to exclude is redhat-release-notes

        Yes, that was the missing data - thank you. I still don't see the need for smartmatch. Instead, here is an SSCCE using a standard m match with a negative look-ahead:

        use strict; use warnings; use Test::More; my @good = ( 'redhat-release', 'redhat-release-server' ); my @bad = ( 'redhat-release-notes', 'any-old-nonsense' ); my $re = qr/^redhat-release(?!-notes)/; plan tests => @good + @bad; for my $str (@good) { like ($str, $re, "$str matched"); } for my $str (@bad) { unlike ($str, $re, "$str not matched"); }

        See How to ask better questions using Test::More and sample data for thoughts about presenting questions in this form.

Re: match exclude
by huck (Prior) on Mar 17, 2017 at 08:51 UTC

     next if ($package->{'name'}=~/\-notes$/); will that do it?

      Using
      next if ($package->{'name'}=~/\-notes$/);

      Will show ALL the other RPM's which I dont want.

      abe-n-s00010 gnutls-devel 16.el5_10 abe-n-s00010 gok 2.el5 abe-n-s00010 gphoto2 3.el5 abe-n-s00010 gpm 74.1 abe-n-s00010 gpm 74.1 abe-n-s00010 gpm-devel 74.1 abe-n-s00010 gpm-devel 74.1

        added after

        next unless $package->{'name'} ~~ /redhat-release/;