LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-20-2010, 06:51 AM   #31
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374

Hi,

This is one solution:
Code:
#!/usr/bin/perl

use strict ;
use warnings ;

my $count = 0 ;

while ( <STDIN> ) {
   for ( split ) {         # break up everything into single words
      if ( $_ =~ /hi/ ) {  # only increase counter if word equals hi
         $count++ ;
      }
   }
}
print "Counted hi: $count time(s).\n" ;
I introduced a new perl function: split

Have a look here for an explanation:
(perl tutorial) split
(perldoc) split
(perlmeme) Using the Perl split() function

Hope this helps.
 
Old 02-21-2010, 11:40 PM   #32
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Original Poster
Rep: Reputation: 32
Hi druuna,
Again you are coming with other solution than the one I used.
Actually I implemented the same in different ways. But, I took it as a challenge to achieve this code only with the help of perl regular expression's special variables($&,$',$`) and some control statements(like while,for,etc.,..). That is why I'm more particular about this post.

Hope you understood my concern.
Thank You!

Last edited by ashok.g; 02-22-2010 at 01:20 AM.
 
  


Reply

Tags
matching, perl


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regexp: difference between sed and Perl matiasar Programming 2 10-15-2009 11:03 AM
Need a perl regexp master sal_paradise42 Programming 8 10-14-2007 06:17 PM
Perl Regexp search-n-replace jpbarto Programming 2 06-16-2005 12:45 PM
perl simple regexp champ Programming 3 07-07-2004 03:27 AM
perl regexp problem raven Programming 4 03-21-2004 11:49 PM


All times are GMT -5. The time now is 11:30 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration