RE: Should be a simple substitution?
And is this method any faster or more efficient than this? $var =~ s/\{([^}]+)\}/$v = $1; $v =~ s!,!|!g; qq!($v)!/ge; Why not find out yourself? C:\src\perltype rebench.plx use strict; use Benchmark qw/cmpthese/; sub luke { my $var = 'blargh{a,b,c}'; my $v; $var =~ s/\{([^}]+)\}/$v = $1; $v =~ s!,!|!g; qq!($v)!/ge; } sub john { my $var = 'blargh{a,b,c}'; $var =~ s{([^}]+)}(?:@{[ ($a = $1) =~ y/,/|/ $a ]}); } cmpthese(10, { 'Luke' = \luke, 'John' = \john }); C:\src\perlrebench Rate John Luke John 59809/s -- -21% Luke 75301/s 26% -- Also, switching the s!,!|!g to a y!,!|! in mine is faster yet: C:\src\perlrebench2.plx Rate John Luke John 59242/s -- -32% Luke 87642/s 48% -- Of course, unless you're doing 10 substitutions in your program, the speed difference doesn't matter at all :-) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Should be a simple substitution?
$var =~ s{([^}]+)}(?:@{[ ($a = $1) =~ y/,/|/ $a ]}); Does it not need the 'ge' at the end? I don't understand why you are creating an anonymous array with a single value, then dereferencing it... And what does the ?: do? I think the ?: must be extraneous: C:\src\perlperl -pes{([^}]+)}(?:@{[ ($a = $1) =~ y/,/|/ $a ]}) blargh{a,b,c}blargh blargh(?:a|b|c)blargh -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Should be a simple substitution?
I think the ?: must be extraneous: That construct lets the regex engine know that it doesn't need to worry about saving backreferences to the parenthesized group. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Should be a simple substitution?
I think the ?: must be extraneous: That construct lets the regex engine know that it doesn't need to worry about saving backreferences to the parenthesized group. It's on the right hand-side of the regex, however. Look at the output: C:\src\perlperl -pes{([^}]+)}(?:@{[ ($a = $1) =~ y/,/|/ $a ]}) blargh{a,b,c}blargh blargh(?:a|b|c)blargh The OP didn't want the ?: in there -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Should be a simple substitution?
It's on the right hand-side of the regex, however. Look at the output: C:\src\perlperl -pes{([^}]+)}(?:@{[ ($a = $1) =~ y/,/|/ $a ]}) blargh{a,b,c}blargh blargh(?:a|b|c)blargh The OP didn't want the ?: in there So it is! I'm usually a fan of one-liners, but I think this is a great example of something that would benefit a lot from the extra clarity that can't be shoehorned into the right half of a regex. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Should be a simple substitution?
Bryan R Harris wrote: I can usually figure out regexes, and this one seems simple, but it still eludes me-- I'm looking for a regex (or a couple of regexes) to do the following: blahblah{ab,abcd}blah -- blahblah(ab|abcd)blah blahblah{a,b,c}blah -- blahblah(a|b|c)blah If it's not obvious I'm trying to glob-select files like the tcsh would. I've got the rest, this is the last part... $var =~ s{([^}]+)}(?:@{[ ($a = $1) =~ y/,/|/; $a ]}); Luke's suggestion blew me away, but I finally came to deal with it. Now this one blew me away again... John, can you explain what this does? (?:@{[ stuff ]}) The string above would be somewhat equivalent to: join '', '(?:', stuff, ')'; stuff is inside of [] which is perl's method of creating an anonymous array, which is inside of @{} which is perl's method of dereferencing an array reference. As you may know, the second part of the substitution operator is the same as a double quoted string (see perlop for details on both) and any variable that starts with a $ or @ character is interpolated but may be evaluated as well. $ perl -le' sub test { my @x = 3 .. 7; return wantarray ? @x : 2 } my @x = 91 .. 99; my $x = q!98765!; print [EMAIL PROTECTED] 5 [EMAIL PROTECTED] 3, 5, 7 ]---; # normal interpolation print ---$x[ test [EMAIL PROTECTED] test ]---;# expression evaluation print ---${\( test )[EMAIL PROTECTED] test ]}---; # expression evaluation print ---${\( scalar test )[EMAIL PROTECTED] scalar test ]}---; # expression evaluation ' ---98765---91 92 93 94 95 96 97 98 99---96---94 96 98--- ---93---94 95 96 97 98--- ---7---3 4 5 6 7--- ---2---2--- John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Should be a simple substitution?
John W. Krahn wrote: Bryan R Harris wrote: I can usually figure out regexes, and this one seems simple, but it still eludes me-- I'm looking for a regex (or a couple of regexes) to do the following: blahblah{ab,abcd}blah -- blahblah(ab|abcd)blah blahblah{a,b,c}blah -- blahblah(a|b|c)blah If it's not obvious I'm trying to glob-select files like the tcsh would. I've got the rest, this is the last part... $var =~ s{([^}]+)}(?:@{[ ($a = $1) =~ y/,/|/; $a ]}); Oops! As has been pointed out to me that won't work, however this will: $var =~ s{([^}]+)}(?:@{[ ($a = $1) =~ y/,/|/ $a ]}); :-) John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Should be a simple substitution?
I can usually figure out regexes, and this one seems simple, but it still eludes me-- I'm looking for a regex (or a couple of regexes) to do the following: blahblah{ab,abcd}blah -- blahblah(ab|abcd)blah blahblah{a,b,c}blah -- blahblah(a|b|c)blah If it's not obvious I'm trying to glob-select files like the tcsh would. I've got the rest, this is the last part... $var =~ s{([^}]+)}(?:@{[ ($a = $1) =~ y/,/|/; $a ]}); Oops! As has been pointed out to me that won't work, however this will: $var =~ s{([^}]+)}(?:@{[ ($a = $1) =~ y/,/|/ $a ]}); Does it not need the 'ge' at the end? I don't understand why you are creating an anonymous array with a single value, then dereferencing it... And what does the ?: do? It's not in the search pattern so it doesn't look like a regex, and it doesn't look like a conditional either... And is this method any faster or more efficient than this? $var =~ s/\{([^}]+)\}/$v = $1; $v =~ s!,!|!g; qq!($v)!/ge; Thanks again. - Bryan -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Should be a simple substitution?
Bryan R Harris wrote: I can usually figure out regexes, and this one seems simple, but it still eludes me-- I'm looking for a regex (or a couple of regexes) to do the following: blahblah{ab,abcd}blah -- blahblah(ab|abcd)blah blahblah{a,b,c}blah -- blahblah(a|b|c)blah Well, you can do tr/{},/()|/ I don't know if that's robust enough for what you're doing... If it's not obvious I'm trying to glob-select files like the tcsh would. I've got the rest, this is the last part... Doesn't File::Glob have the ability to do this already? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Should be a simple substitution?
I can usually figure out regexes, and this one seems simple, but it still eludes me-- I'm looking for a regex (or a couple of regexes) to do the following: blahblah{ab,abcd}blah -- blahblah(ab|abcd)blah blahblah{a,b,c}blah -- blahblah(a|b|c)blah Well, you can do tr/{},/()|/ I don't know if that's robust enough for what you're doing... Probably not, since there may be commas elsewhere in the string. If it's not obvious I'm trying to glob-select files like the tcsh would. I've got the rest, this is the last part... Doesn't File::Glob have the ability to do this already? Perhaps. I'm dealing with many thousands of files on perl 5.4, which doesn't handle globbing correctly on large numbers of files. This script has to be super-portable, meaning I can't guarantee that the destination platform will have any modules installed (which I don't know how to do anyway). - B -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Should be a simple substitution?
I'm looking for a regex (or a couple of regexes) to do the following: blahblah{ab,abcd}blah -- blahblah(ab|abcd)blah blahblah{a,b,c}blah -- blahblah(a|b|c)blah Anybody have a faster way to do this? __CODE__ #!/usr/bin/perl use strict; use warnings; sub uncommify { my ($glob) = @_; $glob =~ s/,/|/g; return $glob; } while (DATA) { chomp(); print $_\t-\t; s/\{([^}]*?)\}/'('.uncommify($1).')'/ge; print $_\n; } __DATA__ blahblah{ab,abcd}blah blahblah{a,b,c}blah -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Should be a simple substitution?
I can usually figure out regexes, and this one seems simple, but it still eludes me-- I'm looking for a regex (or a couple of regexes) to do the following: blahblah{ab,abcd}blah -- blahblah(ab|abcd)blah blahblah{a,b,c}blah -- blahblah(a|b|c)blah Well, you can do tr/{},/()|/ I don't know if that's robust enough for what you're doing... Probably not, since there may be commas elsewhere in the string. So you only want to replace commas inside of the { } brackets? Why didn't you say that the first time :-) Can you do this: $var =~ s/\{([^}]*)\}/$v = $1; $v =~ s!,!|!g; qq!($v)!/ge; If it's not obvious I'm trying to glob-select files like the tcsh would. I've got the rest, this is the last part... Doesn't File::Glob have the ability to do this already? Perhaps. I'm dealing with many thousands of files on perl 5.4, which doesn't handle globbing correctly on large numbers of files. This script has to be super-portable, meaning I can't guarantee that the destination platform will have any modules installed (which I don't know how to do anyway). In this case, opendir() and readdir() are your friends. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Should be a simple substitution?
I can usually figure out regexes, and this one seems simple, but it still eludes me-- I'm looking for a regex (or a couple of regexes) to do the following: blahblah{ab,abcd}blah -- blahblah(ab|abcd)blah blahblah{a,b,c}blah -- blahblah(a|b|c)blah Well, you can do tr/{},/()|/ I don't know if that's robust enough for what you're doing... Probably not, since there may be commas elsewhere in the string. So you only want to replace commas inside of the { } brackets? Why didn't you say that the first time :-) Can you do this: $var =~ s/\{([^}]*)\}/$v = $1; $v =~ s!,!|!g; qq!($v)!/ge; Holy cow, is that legal??!! It took me at least 30 seconds just to figure out that those commands were inside the s/// command. It seems to work. Very nice, thanks! If it's not obvious I'm trying to glob-select files like the tcsh would. I've got the rest, this is the last part... Doesn't File::Glob have the ability to do this already? Perhaps. I'm dealing with many thousands of files on perl 5.4, which doesn't handle globbing correctly on large numbers of files. This script has to be super-portable, meaning I can't guarantee that the destination platform will have any modules installed (which I don't know how to do anyway). In this case, opendir() and readdir() are your friends. Yes, I've come to that myself. =) - Bryan -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Should be a simple substitution?
On Mon, 6 Dec 2004, Bryan R Harris wrote: Can you do this: $var =~ s/\{([^}]*)\}/$v = $1; $v =~ s!,!|!g; qq!($v)!/ge; Holy cow, is that legal??!! It took me at least 30 seconds just to figure out that those commands were inside the s/// command. See the 'e' at the end of the s/// block? That turns on Execution of the code contained inside the right-hand-side of the substitution statement. Useful trick, sometimes... -- Chris Devers -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Should be a simple substitution?
On Mon, 6 Dec 2004, Chris Devers wrote: On Mon, 6 Dec 2004, Bryan R Harris wrote: Can you do this: $var =~ s/\{([^}]*)\}/$v = $1; $v =~ s!,!|!g; qq!($v)!/ge; Holy cow, is that legal??!! It took me at least 30 seconds just to figure out that those commands were inside the s/// command. See the 'e' at the end of the s/// block? $previous_message =~ s/block/statement/; -- Chris Devers -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Should be a simple substitution?
Bryan R Harris wrote: I can usually figure out regexes, and this one seems simple, but it still eludes me-- I'm looking for a regex (or a couple of regexes) to do the following: blahblah{ab,abcd}blah -- blahblah(ab|abcd)blah blahblah{a,b,c}blah -- blahblah(a|b|c)blah If it's not obvious I'm trying to glob-select files like the tcsh would. I've got the rest, this is the last part... $var =~ s{([^}]+)}(?:@{[ ($a = $1) =~ y/,/|/; $a ]}); John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Should be a simple substitution?
I can usually figure out regexes, and this one seems simple, but it still eludes me-- I'm looking for a regex (or a couple of regexes) to do the following: blahblah{ab,abcd}blah -- blahblah(ab|abcd)blah blahblah{a,b,c}blah -- blahblah(a|b|c)blah If it's not obvious I'm trying to glob-select files like the tcsh would. I've got the rest, this is the last part... $var =~ s{([^}]+)}(?:@{[ ($a = $1) =~ y/,/|/; $a ]}); Luke's suggestion blew me away, but I finally came to deal with it. Now this one blew me away again... John, can you explain what this does? (?:@{[ stuff ]}) I'm going to do an archive search for all your replies, you come up with some amazing stuff - B -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response