On May 12, 1:15 pm, fulls...@me.com (Brian) wrote:
> Hi all. I think this should be relatively simple but i just can't figure it 
> out. I have a list of files and directories. The files will all end in .html. 
> All the remaining elements in the list are assumed to be directories. I'm 
> looking for a regex to add a trailing slash to the directories if one doesn't 
> exist. I can do this but using a series of if statements, but I am trying to 
> make my code more concise by using the map function. Here is an example:
>
> my @pages = (
>         "lc1",
>         "lc2/",
>         "lc3/index.html",
>         "lc4/",
>         "lc5",
> );
>
> map {s/(some regex here)/$1\//} @pages;
>

Although newer perl versions can use map in void context
without penalty, it's clearer to use a for loop IMO:

map { s{ ([^/]) \z }{ "$1/" }xe  if -d } @pages;

vs.

for (@pages ) { s{ ([^/]) \z }{ "$1/" }xe  if -d };

--
Charles DeRykus


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to