A nested subroutine. : Nested « Subroutine « Perl

Home
Perl
1.Array
2.CGI
3.Class
4.Data Type
5.Database
6.File
7.GUI
8.Hash
9.Language Basics
10.Network
11.Regular Expression
12.Report
13.Statement
14.String
15.Subroutine
16.System Functions
17.Win32
18.XML
Perl » Subroutine » Nested 
A nested subroutine.
   
#!/usr/local/bin/perl 

($wordcount, $charcount= &getcounts(3)
print ("Totals for three lines: ")
print ("$wordcount words, $charcount characters\n")

sub getcounts 
   my ($numlines= @_; 
   my ($charpattern, $wordpattern)
   my ($charcount, $wordcount)
   my ($line, $linecount)
   my (@retval)
   $charpattern = ""
   $wordpattern = "\\s+"
   $linecount = $charcount = $wordcount = 0
   while (1) { 
       $line = <STDIN>; 
       last if ($line eq "")
       $linecount++; 
       $charcount += &count($line, $charpattern)
       $line =~ s/^\s+|\s+$//g; 
       $wordcount += &count($line, $wordpattern)
       last if ($linecount == $numlines)
   }
   @retval = ($wordcount, $charcount)


sub count 
   my ($line, $pattern= @_; 
   my ($count)
   if ($pattern eq "") { 
      @items = split (//, $line); 
   else 
      @items = split (/$pattern/, $line)
   
   $count = @items; 


   
    
    
  
Related examples in the same category
1.Nested method
2.Nested subroutine with local variable
3.nested subroutine
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.