CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Extract elements from an array



Jan 24, 2014, 3:00 AM

Post #1 of 2 (2186 views)
Extract elements from an array Can't Post

@x = ("mickey", "Donald", "minnie", "Daisy", "Tralfaz");

I looped through some device configs isolated the hostname and pushed into an array. I want to define those names not conforming to "the naming standard". Anything starting with a capital letter is wrong. I want to extract only those elements from the array, maybe push into another array so that I can loop, do a lc(), and fix the device name.

So I guess my question is, is there a way to pull directly from @x or do I have to loop through @x and check each element with m/^[A-Z]/

as usual, thanks for the help.

Veteran / Moderator

Jan 24, 2014, 6:24 AM

Post #2 of 2 (2175 views)
Re: [popeye] Extract elements from an array [In reply to] Can't Post

If you need to check each element then obviously you'll need to loop over those array elements. That could be done with an explicit for/foreach loop or implicitly with grep or map.

If you simple want to use lc() normalize the case then a simple for loop would be the easiest.

lc($_) for @x;

If you want to copy the offending elements into another array then grep would be the obvious choice.

my $offending = grep(/^[A-Z]/, @x);


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives