t0j0 has asked for the wisdom of the Perl Monks concerning the following question:

how do i return different kind of expected result from function such as split. what i wanted to do is to print return value of number of element splitted from split rather than the the array.. if i do this
$str = "this is a string"; print $split(/\s/,$str);
it will print thisisastring . it is not what i want.

what i want is something that work like below except that i don't want to create extra storage variable like $i
$str = "this is a string"; $i = split(/\s/,$str); print $i;

Replies are listed 'Best First'.
Re: different return type for split
by jeroenes (Priest) on Mar 19, 2001 at 14:50 UTC
    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.

      $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

Re: different return type for split
by busunsl (Vicar) on Mar 19, 2001 at 14:05 UTC
    Use scalar, like this:

    $str = "this is a string"; print scalar split(/\s/,$str);
Re: different return type for split
by snowcrash (Friar) on Mar 19, 2001 at 14:06 UTC
    if you don't call split in list context, it returns the number of fields found. try print scalar split(/\s/,$str); to print the number of elements it returns.

    cheers
    snowcrash //////
Re: different return type for split
by t0j0 (Novice) on Mar 19, 2001 at 14:03 UTC
    sorry the first snippet should be
    $str = "this is a string"; print split(/\s/,$str);
    i'd mistakenly put the $ sign before the split function
Re: different return type for split
by arturo (Vicar) on Mar 19, 2001 at 23:30 UTC

    Evil hack that only works for single-character splits that are otherwise well-behaved (i.e. no trailing spaces, in this case):

    print ++($string =~ tr / //);

    Side note: with perl 5.6, I get "implicit split to @_ deprecated" with the scalar trick outlined above if I run under -w (and I do, oh yes, I do!)

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor