On Jun 21, Paul said:

>--- Chuck Ivy <[EMAIL PROTECTED]> wrote:
>> I recall some programming languages treat strings as arrays of 
>> characters. Is there a quick perl function or variable that
>> represents 
>> the length of a string? I didn't see any obvious entries in the index
>> of 
>> Programming Perl, but I may have been looking in the wrong place.
>
>I'd agree with Japhy's substring suggestion, but I think you could do
>something like this if you just wanted to play array games with the
>string:
>
> @str = $str =~ /(.)/;
> $#str = 4095;
> $str = join '', @str;

Ewww. ;)  First, you need the /s modifier on the regex.  Second, you need
the /g modifier on the regex.  Third, you don't need the () in the regex.

  @chars = $str =~ /./gs;

Fourth, split() is far nicer to look at.

  @chars = split //, $str;

Fifth, just join() the elements you want.

  $str = join '', @chars[0 .. 4095];

Sixth, get rid of the array.

  $str = join '', (split //, $str)[0 .. 4095];

But we're not using this approach anyway, are we? ;)

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
I am Marillion, the wielder of Ringril, known as Hesinaur, the Winter-Sun.
Are you a Monk?  http://www.perlmonks.com/     http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc.     http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter.         Brother #734
**      Manning Publications, Co, is publishing my Perl Regex book      **

Reply via email to