Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

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.

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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://129000]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2018-01-21 15:50 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (228 votes). Check out past polls.