Loading ...
Sorry, an error occurred while loading the content.

finding matching values between hash and array

Expand Messages
  • Noah
    Hi there, I am trying to remove all the values in @ignore_routers from the @devices array. %hostnames is where the entire list of hostnames reside. Is there
    Message 1 of 3 , Jan 16, 2013
    • 0 Attachment
      Hi there,

      I am trying to remove all the values in @ignore_routers from the
      @devices array. %hostnames is where the entire list of hostnames
      reside. Is there any way to compact these lines in to fewer lines or a
      better way to look at this?

      ---- snip ----

      @devices = sort keys %hostnames;
      my %routers_lookup = (map { $_ => 1 } @ignore_routers);
      @devices = grep { !exists($routers_lookup{$_}) } @devices;

      Cheers,
      Noah
    • Matthew K
      The most efficient way to do it, would be to use a hash of hashes initially, and use keys like ignored and values of one or zero in the value of the
      Message 2 of 3 , Jan 16, 2013
      • 0 Attachment
        The most efficient way to do it, would be to use a hash of hashes initially, and use keys like "ignored" and values of one or zero in the value of the original hash. I'm sure the hostames have many properties and this would be an easy way to pull them out without two nested foreach loops. For example if you had 400 hosts and 50 ignored hosts you would only loop through 400 times instead of 20,000 times.

        Without doing it that way, you are pretty much going to need nested loops.
         
        --
        Matt

        From: Noah <noah-list@...>
        >To: perl-beginner@yahoogroups.com
        >Sent: Wednesday, January 16, 2013 12:37 PM
        >Subject: [PBML] finding matching values between hash and array
        >
        >

        >Hi there,
        >
        >I am trying to remove all the values in @ignore_routers from the
        >@devices array. %hostnames is where the entire list of hostnames
        >reside. Is there any way to compact these lines in to fewer lines or a
        >better way to look at this?
        >
        >---- snip ----
        >
        >@devices = sort keys %hostnames;
        >my %routers_lookup = (map { $_ => 1 } @ignore_routers);
        >@devices = grep { !exists($routers_lookup{$_}) } @devices;
        >
        >Cheers,
        >Noah
        >
        >
        >
        >
        >

        [Non-text portions of this message have been removed]
      • Charles K. Clarkson
        ... Instead of ignoring the routers in @devices, you might ignore the routers in %hostnames and then retrieve the routers in @devices. my %devices =
        Message 3 of 3 , Jan 16, 2013
        • 0 Attachment
          On 1/16/2013 1:37 PM, Noah wrote:

          > I am trying to remove all the values in @ignore_routers from the
          > @devices array. %hostnames is where the entire list of hostnames
          > reside. Is there any way to compact these lines in to fewer lines
          > or a better way to look at this?

          Instead of ignoring the routers in @devices, you might ignore the
          routers in %hostnames and then retrieve the routers in @devices.


          my %devices = %hostnames;
          delete @devices{ @ignore_routers };
          my @devices = sort keys %devices;


          HTH,

          Charles Clarkson
          --
          I'm not really a smart person. I just play one on the Internet.
          +1 (254) 434-2733
        Your message has been successfully submitted and would be delivered to recipients shortly.