On Wed, Nov 19, 2003 at 01:33:25PM -0500, Rob wrote:
> Hi, I'm trying to find out how many newline characters are in a string.  I
> thought there would be a simple function for this, but I can't find it;
> Do I need to step through the string a character at a time to check this?

Here is an example of the Perl idiom that will do this for you.  I had a
file with 11 newlines, which I read into a string.  I then used a
regular expression to match newlines in that string and then took
advantage of Perl's list context to get the count.

perl -le'{local $/ = undef; $file = <>;} $number = () =  $file =~ /\n/g;
print $number;' file

In a non-one-liner, the important bit would be declared with my

        my $number = () =  $file =~ /\n/g 

which is equivalent to

        my @array = $file =~ /\n/g;
        my $number = @array;

You just save yourself having to explicitly mention the list/array.

Cheers, 


Damon


-- 

Damon Allen Davison
http://allolex.freeshell.org/
        
        A language is therefore a horizon, and style a vertical
        dimension, which together map out for the writer a Nature,
        since he does not choose either. The language functions
        negatively, as the initial limit of the possible, style is a
        Necessity which binds the writer's humour to his form of
        expression. In the former, he finds a familiar History, in
        the latter, a familiar personal past.  In both cases he
        deals with a Nature, that is, a familiar repertory of
        gestures, a gestuary, as it were, in which the energy
        expended is purely operative, serving here to enumerate,
        there to transform, but never to appraise or signify a
        choice.
                        
                        -- Roland Barthes (1915-1980), French
                        semiologist. ``What Is Writing??'' Writing
                        Degree Zero (1953, trans. 1967).

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to