[Israel.pm] splitting a string

Tal Kelrich tal at musicgenome.com
Thu Sep 28 05:19:13 PDT 2006


On Wed, 27 Sep 2006 15:41:35 +0300
Tal Kelrich <tal at musicgenome.com> wrote:

> On Wed, 27 Sep 2006 14:58:30 +0200
> "Benrey Yosi" <YosiB at pelephone.co.il> wrote:
> 
> > $a="123456";
> > ($b,$c)=(substr($a,0,length($a)/2),substr($a,length($a)/2,length($a)/2))
> > ;
> > 
> 
> This only works if your string contains an even number of characters,
> so isn't very good.
> you could use the following, but it's equally silly, IMHO.
> 
> $a="1234567";
> $hlen=int(length($a)/2);
> ($b,$c)=(substr($a,0,$hlen),substr($a,$hlen)) ;
> print "$b - $c" 
> 

here's another version you shouldn't use, as it is really inefficient.
mainly exists because I was wondering about using the .. operator

$s="1234567";
$l=length($s);
$hl=int($l/2);
$f=eval("sub {0..$hl}");
for $. (0..$l){(&$f?$b:$c).=substr($s,$.,1)};
print "|$b|$c|\n";

-- 
Tal Kelrich
PGP fingerprint: 3EDF FCC5 60BB 4729 AB2F  CAE6 FEC1 9AAC 12B9 AA69
Key Available at: http://www.hasturkun.com/pub.txt
----
Remember, in 2039, MOUSSE & PASTA will be available ONLY by
prescription!! ----
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://perl.org.il/pipermail/perl/attachments/20060928/35b3b93b/attachment.pgp 


More information about the Perl mailing list