Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Counting the number of items returned by split without using a named array

by prasadbabu (Prior)
on May 03, 2006 at 09:58 UTC ( #547100=note: print w/replies, xml ) Need Help??


in reply to Counting the number of items returned by split without using a named array

Here is one way to do it.

use strict; use warnings; $_ = 'here the text goes'; my @entries; print scalar (@entries = split /\s+/, $_);

Without return list, here is one way

use strict; use warnings; $_ = 'here the text goes'; my $count; print $count = $_ =~ s/\S+//g;

updated: Added second method as blazar pointed out, without return list, though not a most elegant way. Thanks.

Prasad

Replies are listed 'Best First'.
Re^2: Counting the number of items returned by split without using a named array
by blazar (Canon) on May 03, 2006 at 10:53 UTC

    "So what's the most elegant way to get the number of entries and throw away the resulting array?"

    I think he means: "discarding the return list, retaining only its lenght".

Re^2: Counting the number of items returned by split without using a named array
by Anonymous Monk on May 03, 2006 at 12:53 UTC
    Ha, great
    print $count = $_ =~ s/\S+//g;

    was probably what I wanted - just didnt know you could do that with a simple search (although I wondered if one couldnt just use a search in some way)

    Thx a lot

    Why does a simple
    print $count = s/\S+//g;

    not work, though?

    This (as suggested below) did not count anything either:

    $count = () = s/\S+//g;

    I.
      I use m//g bellow __not__ s///g.
      Boris

      Although this may occasionally work for you in this circumstance, it's not logical to modify the original string just to count the number of occurrences. Just use /\S+/g instead.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://547100]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2021-01-23 10:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?