Cool. Thanks for the C-code lookup! Glad I'm not crazy. I have been trying to limit dependencies, but I could look at that module to see how they handle the '{}' expressions. My code should handle multiple occurrences, and assuming that there are no spaces and no nested expressions, I think it should theoretically work in every case. I'm not 100% on that though. Well, it doesn't handle escape curlies, but I'm not even sure a filename could have that... Whoops, yes they can. I just renamed a file to "tmpdelete{test}.txt". I dragged it to my terminal and it pasted it with escape characters. I guess I should make a minor edit to my code:
#Keep updating an array to be the expansion of a file pattern to
#separate files
my @expanded = ($nospace_string);
#If there exists a '{X,Y,...}' pattern in the string
if($nospace_string =~ /(?<!\\)\{.+?(?<!\\)\}/)
{
#While the first element still has a '{X,Y,...}' pattern
#(assuming everything else has the same pattern structure)
while($expanded[0] =~ /(?<!\\)\{.+?(?<!\\)\}/)
{
#Accumulate replaced file patterns in @g
my @buffer = ();
foreach my $str (@expanded)
{
#If there's a '{X,Y,...}' pattern, split on ','
if($str =~ /(?<!\\)\{(.+?)(?<!\\)\}/)
{
my $substr = $1;
my $before = $`;
my $after = $';
my @expansions = split(/,/,$substr);
push(@buffer,map {$before . $_ . $after} @expansions);
}
#Otherwise, push on the whole string
else
{push(@buffer,$str)}
}
#Reset @f with the newly expanded file strings so that we
#can handle additional '{X,Y,...}' patterns
@expanded = @buffer;
}
}
#Pass the newly expanded file strings through
return(wantarray ? @expanded : [@expanded]);
Although, I just tested that nested expressions are possible too, so I would definitely like to check out that module.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.