Thanks. This is much better !
Yossi
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of sawyer x
Sent: Sunday, November 02, 2008 4:35 PM
To: Perl in Israel
Subject: Re: [Israel.pm] using split and join to add a characters every 2
original ones
Of course this also does the trick:
$ perl -le'$str = "For example"; $str =~ s/(..)/$1#/g; print $str'
Fo#r #ex#am#pl#e
On Sun, Nov 2, 2008 at 4:32 PM, sawyer x <[EMAIL PROTECTED]> wrote:
> You could fix that join with map
>
> print map { $_ || '#' } split /(.{2}/, "For example";
>
> But split() still puts the spaces before instead of after when you use
> rounded brackets: ().
> The closest I got (sort of cheating but still quite short):
> $ perl -le'print join "", map { $_ && "$_#" } (split /(.{2})/, "For
> example");'
> Fo#r #ex#am#pl#e#
>
> The only difference is that it adds the character to the end because
> it's still a valid input and not space.
> I guess you could chop it like this in the end:
> $ perl -le'$str = join "", map { $_ && "$_#" } (split /(.{2})/, "For
> example"); chop $str if length $str % 2; print $str'
> Fo#r #ex#am#pl#e
>
> But it does make it longer.
>
> On Sun, Nov 2, 2008 at 3:48 PM, Yossi Itzkovich
> <[EMAIL PROTECTED]> wrote:
>>
>> Hi,
>>
>> I was asked by a friend to write a one-linear to add a character every 2
>> characters. For example:
>> "For example" Will become "Fo#r #ex#am#pl#e"
>>
>> I tried to do it with join and split (seems natural for me), but had no
>> success, I managed to do it only with map:
>> map({s/(..)/$1#/g,$_}$_)
>>
>> The best I got from join and split is :
>> join ("#" , split (/(..)/, $_))
>> which prints 2 # after each 2 characters. I understand why, but I could not
>> get rid of it.
>>
>> Any ideas ?
>>
>> Yossi
>>
>>
>>
>> _______________________________________________
>> Perl mailing list
>> [email protected]
>> http://perl.org.il/mailman/listinfo/perl
>
_______________________________________________
Perl mailing list
[email protected]
http://perl.org.il/mailman/listinfo/perl
_______________________________________________
Perl mailing list
[email protected]
http://perl.org.il/mailman/listinfo/perl