[Israel.pm] splitting a string

Benrey Yosi YosiB at pelephone.co.il
Wed Sep 27 07:48:34 PDT 2006


The new line will work for even and not even string 


($b,$c)=(substr($a,0,int(length($a)/2)),substr($a,int(length($a)/2),length($a)-int(length($a)/2)));

 

-----Original Message-----
From: perl-bounces at perl.org.il [mailto:perl-bounces at perl.org.il] On Behalf Of Tal Kelrich
Sent: Wednesday, September 27, 2006 2:42 PM
To: perl at perl.org.il
Subject: Re: [Israel.pm] splitting a string

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" 

-- 
Tal Kelrich
PGP fingerprint: 3EDF FCC5 60BB 4729 AB2F  CAE6 FEC1 9AAC 12B9 AA69
Key Available at: http://www.hasturkun.com/pub.txt
----
I don't have an eating problem. I eat. I get fat. I buy new clothes. No
problem.
----




More information about the Perl mailing list