Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Replace the nth occurence

by choroba (Chancellor)
on Nov 21, 2012 at 10:39 UTC ( #1004892=note: print w/replies, xml ) Need Help??

in reply to Replace the nth occurence

#!/usr/bin/perl use warnings; use strict; use feature 'say'; sub replace { my ($string, $from, $to, $count) = @_; my $pos = '0E0'; # plain 0 means the string begins with $from while ($count-- and $pos >= 0) { $pos = index $string, $from, $pos eq '0E0' ? $pos : $pos + 1; } substr $string, $pos, 1, $to if $pos > 0; return $string; } say replace($_, ',' => '|', 2) for qw( a,b,c,d pq,rs,tu,vw ,s,t,a,r,t ,,yuck 1,2 );
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1004892]
[Lady_Aleena]: james28909, I use get_data in a subroutine called movies.
[Lady_Aleena]: james28909, I put it at the top of my scratchpad, if you want to look at it.
[Lady_Aleena]: I remember being pretty proud of that little sub when I wrote it.
[james28909]: oh xD
[james28909]: i see the predicament here.
[james28909]: maybe rename it to 'query'

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2017-05-24 04:38 GMT
Find Nodes?
    Voting Booth?