Here is the context of the problem. Suppose our environment has several DNS subdomains of the pattern "area01", "area02", "area03". Withing each subdomain, the hosts names are repeated: "peanut", "cashew", "almond", "hazel".

My perl script will be doing something with these hostnames, and I want to give the user a shorthand for inputting the host name on the command line. One can run the command like this.

./myscript.pl -h peanut 01 13 44 18 27

This will target the hosts peanut.area01, peanut.area13, and so forth.

Here is the test example of how I'm doing this.

#!/usr/bin/perl -w use warnings; use strict; # Will be set by getopt(). my %opt = ( 'h' => 'peanut,cashew'); # Contains only shorthand subdomains for now, but items will be # expanded to full hostnames. my @hosts = ( '01', '13', '09'); # The short hostnames to target in each subdomain. my @shorts = (split /,|\s+/, $opt{'h'}); for my $h (@hosts) { if ($h =~ m/^(\d{2})$/) { for my $short (@shorts) { push @hosts, "$short.area$h"; } } } @hosts = grep {!/^\d{2}$/} @hosts; print join(' ', @hosts) . "\n";

What I am wondering is there a map (maybe splice?) syntax that will accomplish this?

I am familiar (as of today) with tye's Filter function in Algorithm::Loops, and, in fact, reading the Motivation section is greatly educational. However, I would rather not use a module as I want to gain a greater mastery with the core functions at this time. Regardless, if you have a helpful module in mind, please list it for the benefit of others with a similar problem who may be reading this node.


In reply to Can map or splice replace my use of for loop + push + grep for my array transmogrification? by jffry

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.