CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
How to print next word of search criteria, multiple occurances.

 



chandhar
New User

May 11, 2011, 1:48 AM

Post #1 of 3 (307 views)
How to print next word of search criteria, multiple occurances. Can't Post

Hi Gurus,

I am new to perl and I would like to print next word of every search criteria text.

Example:

String: "Developing cloud applications demands open platforms and flexible deployment along with scalable and data infrastructure. "

Search criteria: "and"

Required output: flexible & the

Can any one please help me?

Regards,
Chandhar


miller
User

May 11, 2011, 2:08 AM

Post #2 of 3 (305 views)
Re: [chandhar] How to print next word of search criteria, multiple occurances. [In reply to] Can't Post


Code
my $str = "Developing cloud applications demands open platforms and flexible deployment along with scalable and data infrastructure. "; 

my @array = $str =~ /\s+and\s+(\S+)/g;

print "$_\n" for (@array);


- Miller


BillKSmith
Veteran

May 11, 2011, 6:24 AM

Post #3 of 3 (301 views)
Re: [chandhar] How to print next word of search criteria, multiple occurances. [In reply to] Can't Post

We need a better specification of your problem.

There is no 'the' in your string. Do you mean 'data'?

Is your search criteria the string 'and' or the word 'and'? If your string contained the pharse 'sandlot baseball', do you want to return 'baseball'?

What exactly is a 'word'? Punctuation can cause confusion. If 'flexible' in your example were in quotes, would you still want it? Would you want the quotes?

I will assume perl's definition of word character and word boundary.


Code
use strict; 
use warnings;
my $string =
'Developing cloud applications demands'
.' open platforms and flexible deployment'
.' along with scalable and data infrastructure. '
;
my $search_criteria = '\band\b';
(my @finds) = $string =~ /${search_criteria}.*?\b(\w+)\b/g;
local $, = "\n";
print @finds;

Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives