jffry has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Can map or splice replace my use of for loop + push + grep for my array transmogrification?
by ikegami (Patriarch) on Nov 04, 2009 at 00:16 UTC | |
by jffry (Hermit) on Nov 04, 2009 at 00:30 UTC | |
|
Re: Can map or splice replace my use of for loop + push + grep for my array transmogrification?
by ikegami (Patriarch) on Nov 03, 2009 at 23:48 UTC | |
by jffry (Hermit) on Nov 04, 2009 at 00:13 UTC | |
by ikegami (Patriarch) on Nov 04, 2009 at 00:19 UTC |