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:
store each word of string

 



harshmane
Novice

Jun 26, 2011, 11:35 PM

Post #1 of 16 (1641 views)
store each word of string Can't Post

hi i want to store each word in string using \s and \S regex not by using any split or while loop.

if any one can can

$_ =" hi john good morning how are you"
#or any string i want to know the reg exp wich work equivalent #to split function


rovf
Veteran

Jun 27, 2011, 12:21 AM

Post #2 of 16 (1640 views)
Re: [harshmane] store each word of string [In reply to] Can't Post

You have asked the same already here some time ago, and received an answer:

http://perlguru.com/gforum.cgi?post=57373

Do you want me to copy and paste that answer for your new posting, or are you able to do this by yourself?


harshmane
Novice

Jun 27, 2011, 1:57 AM

Post #3 of 16 (1635 views)
Re: [rovf] store each word of string [In reply to] Can't Post

yes i think it is somewhat same but it differ i wud write my code.

#! /usr/bin/perl

$_= "hi how are you good morning";
m/(\w+)/ ;# it wud match 1st word hi
print $1; # hi
m/(\s)(\w)/ ;# wud match for space and "how"
print $3; # print how

# in these way i want all words of the string is there any way to use while loop or to keep on doin these thing regression .


rovf
Veteran

Jun 27, 2011, 2:26 AM

Post #4 of 16 (1631 views)
Re: [harshmane] store each word of string [In reply to] Can't Post


Quote
doin these thing regression


??? What kind of regression (???) do you want to do?


Quote
is there any way to use while loop


In your original posting, you wrote:

i want to store each word in string using \s and \S regex not by using any split or while loop.

So, first you had said you looking for a solution WITHOUT while-loop. Now you say, you are looking for a solution WITH while-loop. How about making up your mind what kind of solution you are looking for?


harshmane
Novice

Jun 27, 2011, 3:33 AM

Post #5 of 16 (1630 views)
Re: [rovf] store each word of string [In reply to] Can't Post

thing is like that i have given any string wich can have any number of words what i actuually want is to capture each & every word using \s or \S operation the way split function ...
split(" ");
Unsure


rovf
Veteran

Jun 27, 2011, 3:43 AM

Post #6 of 16 (1626 views)
Re: [harshmane] store each word of string [In reply to] Can't Post

... and this is EXACTLY what I had answered you already in your other post.


harshmane
Novice

Jun 27, 2011, 4:03 AM

Post #7 of 16 (1625 views)
Re: [rovf] store each word of string [In reply to] Can't Post

in my example a string can have any word so how we are going to group your example was having like xxabayyyybcbbbzz and we want abc to be grouped but in my case i dont know what type of word is goin 2 come only spaces is the only parameter 2 define it .


rovf
Veteran

Jun 27, 2011, 8:50 AM

Post #8 of 16 (1608 views)
Re: [harshmane] store each word of string [In reply to] Can't Post


Quote
n my case i dont know what type of word is goin 2 come only spaces is the only parameter 2 define it


Sorry, I fail to understand this sentence. Maybe you can rephrase it?

Also, you need to define first what is a "word". Is it a set of lower and upper case letters? What about accented letters or letters in Japanese Alphabet? Is it maybe a set of everything but space? Is it a set of everything but white space?


harshmane
Novice

Jun 27, 2011, 9:14 AM

Post #9 of 16 (1606 views)
Re: [rovf] store each word of string [In reply to] Can't Post

word can be in upper case or lower case it going to be in english word they are going 2 be seperated by space.


rovf
Veteran

Jun 27, 2011, 9:24 AM

Post #10 of 16 (1602 views)
Re: [harshmane] store each word of string [In reply to] Can't Post

This means, the input can NOT look like this:


Code
abCD EFG  hIJkL


because there would be one space, not two, and the input can also NOT look like this:


Code
abc   def  gh


(3 spaces separating), neither can it be


Code
abc!  defG


Is this correct?

So the next question is: Do you have to provide error checking (if the input does not meet these requirements), or can you silently assume that the input has the correct format?


harshmane
Novice

Jun 27, 2011, 10:11 AM

Post #11 of 16 (1597 views)
Re: [rovf] store each word of string [In reply to] Can't Post

all your inputs are eligible . no matter whether there are 1 space or 2 space or any special character is also there.


rovf
Veteran

Jun 28, 2011, 3:12 AM

Post #12 of 16 (1567 views)
Re: [harshmane] store each word of string [In reply to] Can't Post

Sorry, but you are contradicting yourself:

In your earlier post, you wrote


Quote
word can be in upper case or lower case it going to be in english word they are going 2 be seperated by space.


but now you are writing:

Quote
all your inputs are eligible . no matter whether there are 1 space or 2 space or any special character is also there.


You need to make up your mind, what of those you want. Unless you have specified your problem, you can not implement it.


harshmane
Novice

Jun 28, 2011, 3:22 AM

Post #13 of 16 (1566 views)
Re: [rovf] store each word of string [In reply to] Can't Post

\s means it can be be a space or tab. if you have idea how to solve it then please reply.


rovf
Veteran

Jun 28, 2011, 3:25 AM

Post #14 of 16 (1565 views)
Re: [harshmane] store each word of string [In reply to] Can't Post

\s can also be a newline.


Quote
if you have idea how to solve it then please reply.


You have not responded to my question in my previous posting.

Note: Unless you don't explain which problem you need to solve, it is futile to develop ideas; and as long as you contradict yourself when stating the problem, your task is not explained yet....


harshmane
Novice

Jun 28, 2011, 10:04 AM

Post #15 of 16 (1564 views)
Re: [rovf] store each word of string [In reply to] Can't Post


Code
use strict; 
use warnings;


$xyz= "hello hi friend do you want some help.";

$xyz=~m/(\s|\S+)/;
print "$1 \n"; #print hello

$xyz=~m/(\s)(\S+)/;
print "$2 \n"; #print hi


$xyz=~/((\s)(\S+)){2}/;
print " $1\n"; #print friend



(This post was edited by harshmane on Jun 28, 2011, 10:05 AM)


rovf
Veteran

Jun 28, 2011, 11:22 AM

Post #16 of 16 (1562 views)
Re: [harshmane] store each word of string [In reply to] Can't Post

This script doesn't compile without giving warnings, so I suggest correcting these warnings.

Aside of this, it is a nice script, but you still didn't answer my recent questions.

 
 


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

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