in reply to different return type for split

Others already pointed out the scalar context trick. There are also non-destructive methods available to count the occurence of a certain character. You can find this in perlop as well. Depending on what you need exactly, you can use different solutions:
$txt = "This is a string"; $count = $txt =~ tr/ //; #counts all spaces $count = () = $txt =~ m/\s/g; #counts all whitespace characters $count = () = $txt =~ m/\s+/g; #counts whitespace occurences (default + [split] behavior)
It's probably a good idea to check out 7 Stages of Regex Users as well. Oh yeah, Don't be confused by the =~. It's not only an assignment, it's also a function that returns something, just like when it acts on $_. See perldoc, illustrated by:
$count = tr/ // for ($txt);
Does exactly the same, but now the match is done on $_. $_ is assigned to by for. Don't assign to $_ yourself (or use local). A complete different approach still is the use of index, which may be handy under certain circumstances.

Hope this helps,

Jeroen
"We are not alone"(FZ)
Update: I must have been sleeping.... read merlyn's reply... code is working now.

Replies are listed 'Best First'.
Re: Re: different return type for split
by merlyn (Sage) on Mar 19, 2001 at 19:46 UTC
    $count = /\s/ for ($txt);
    I had to stare at that three times before I got what you were doing. It was the $count variable, which isn't a count. It's merely true/false, if $txt contains a space or not.

    Perhaps what you were reaching for is:

    $count = () = /\s/g for $txt;

    -- Randal L. Schwartz, Perl hacker