Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Reusing camel code

by chromatic (Archbishop)
on Dec 03, 2001 at 00:00 UTC ( #129000=note: print w/ replies, xml ) Need Help??


in reply to Reusing camel code

split looks like a better choice than a regex. I whittled away at this and came up with the untested:

for (split(/:/, $$pattern)) { # make sure it starts and ends with spaces $_ = 'S0' . $_ unless /^S/; $_ .= 'S0' unless /S\d+$/; my @pieces = split(/[SF](\d+)/, $_); while (my ($spaces, $fills) = splice(@pieces, 0, 2)) { $out .= " " x $spaces . substr($camel, $camelcount, $fills); } $out .= "\n"; }
I'm fairly pleased with that, but might try to fit a pack in there. (How would you have used map?)

Aside, this:

while (/(.)/g) { $camel .= $1 if $1 ne " "; }
could be spelled:
tr/ //d; $camel .= $_;
I'm sure you can trim that down more, too. Don't forget to check out Acme::EyeDrops for a different take on things.

Update, very shortly after: You could, of course, just split on [SF] and probably get the same effect, though you'll have an empty element at the start. It may or may not be more clear that way.


Comment on Re: Reusing camel code
Select or Download Code
Replies are listed 'Best First'.
Re: Re: Reusing camel code
by gmax (Abbot) on Dec 03, 2001 at 15:19 UTC
    Thank you very much for your hints. I tested your code
    for (split(/:/, $$pattern)) { $_ = 'S0' . $_ unless /^S/; $_ .= 'S0' unless /S\d+$/; print STDERR "pattern: $_\n"; my @pieces = split(/[SF](\d+)/, $_); foreach my $count (0..$#pieces) { print STDERR "$count -> [$pieces[$count]] "; } print STDERR "\n"; }
    But I got an odd output. Split gives back some empty items
    pattern: S51F11S0 0 -> [] 1 -> [51] 2 -> [] 3 -> [11] 4 -> [] 5 -> [0]
    Changing split to match only /[SF]/ (your update suggestion) and adding a shift afterwards improves the output,
    pattern: S51F11S0 0 -> [51] 1 -> [11] 2 -> [0]
    leaving me with the second problem, i.e. that having an odd number of items in my array, splice(@,0,2) will give back an undefined second element.
    Nothing catastrophic, but it is going to increase the number of necessary checks.
    Even if it doesn't do what I need, though, this code of yours gives me some ideas that I can further develop.
    As for the second hint:
    tr/ //d; $camel .= $_;
    yes, it looks much faster (not to mention smarter) than my code.
    Ciao gmax

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://129000]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2015-07-08 08:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (96 votes), past polls