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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



popeye
Novice

Jan 24, 2014, 3:00 AM

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


Code
@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.


FishMonger
Veteran / Moderator

Jan 24, 2014, 6:24 AM

Post #2 of 2 (1143 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.

Code
lc($_) for @x;


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

Code
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