Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Splitting a String into a List Using Seperator "*"

Reply
Thread Tools

Splitting a String into a List Using Seperator "*"

 
 
Christopher M. Lusardi
Guest
Posts: n/a
 
      01-31-2004
Hello,

How do I split a string using the library function "split" with an asterisk?
I have to tell split break the string when it sees "abc*". I.E.: If I give split
the string "abc def ghi jkl abc* message" I want to get back two parts.
I want "abc def ghi jkl" and "message".

Thank you,
Christopher Lusardi
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      01-31-2004
In article < >,
Christopher M. Lusardi <> wrote:
: How do I split a string using the library function "split" with an asterisk?

Escape it.

:I have to tell split break the string when it sees "abc*". I.E.: If I give split
:the string "abc def ghi jkl abc* message" I want to get back two parts.
:I want "abc def ghi jkl" and "message".

split /abc\*/, $string
--
Rump-Titty-Titty-Tum-TAH-Tee -- Fritz Lieber
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      01-31-2004
In article <bvevqa$rlg$>,
Walter Roberson <> wrote:
|In article < >,
|Christopher M. Lusardi <> wrote:

|:I have to tell split break the string when it sees "abc*". I.E.: If I give split
|:the string "abc def ghi jkl abc* message" I want to get back two parts.
|:I want "abc def ghi jkl" and "message".

|split /abc\*/, $string

By the way, that split would result in
'abc def ghi jkl ' and ' message' -- notice the trailing and then
leading spaces!

Possibly what you want is

split /\s+abc\*\s+/, $string

but it depends on how you would want to handle
'abc def ghi jklabc* message' or 'abc def ghi jkl abc*message'
--
Aleph sub {Aleph sub null} little, Aleph sub {Aleph sub one} little,
Aleph sub {Aleph sub two} little infinities...
 
Reply With Quote
 
Chris Mattern
Guest
Posts: n/a
 
      01-31-2004
Walter Roberson wrote:
> In article < >,
> Christopher M. Lusardi <> wrote:
> : How do I split a string using the library function "split" with an asterisk?
>
> Escape it.
>
> :I have to tell split break the string when it sees "abc*". I.E.: If I give split
> :the string "abc def ghi jkl abc* message" I want to get back two parts.
> :I want "abc def ghi jkl" and "message".
>
> split /abc\*/, $string


split / abc\* /, $string, otherwise he gets extraneous spaces.

Chris Mattern

 
Reply With Quote
 
Web Surfer
Guest
Posts: n/a
 
      02-05-2004


In article < >,
says...
> Hello,
>
> How do I split a string using the library function "split" with an asterisk?
> I have to tell split break the string when it sees "abc*". I.E.: If I give split
> the string "abc def ghi jkl abc* message" I want to get back two parts.
> I want "abc def ghi jkl" and "message".
>
> Thank you,
> Christopher Lusardi
>


my ( @fields , $string );

$string = "abc def ghi jkl abc* message";
@fields = split(/\*/,$string);

# see the command "perldoc -f split" for further details
 
Reply With Quote
 
James Willmore
Guest
Posts: n/a
 
      02-05-2004
On Fri, 30 Jan 2004 16:13:43 -0800, Christopher M. Lusardi wrote:

> How do I split a string using the library function "split" with an asterisk?
> I have to tell split break the string when it sees "abc*". I.E.: If I give split
> the string "abc def ghi jkl abc* message" I want to get back two parts.
> I want "abc def ghi jkl" and "message".


After reading some of the responses and re-reading this post again, I have
to ask ....

What *exactly* are you trying to do? It almost seems like an XY problem.
I say this becasue the last line your your post almost reads like ... you
want to parse a log file ("date ... message" versus "abc efg ... message"
- they *look* similar).

If you're trying to parse a *NIX log file, you could use a module to do
this and make life easier for yourself. Or, you could use 'unpack', which
may work better for you instead of 'split'.

HTH

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
Peace, n.: In international affairs, a period of cheating
<between two periods of fighting. -- Ambrose Bierce, "The
Devil's Dictionary"
 
Reply With Quote
 
J Krugman
Guest
Posts: n/a
 
      02-05-2004

Here's my entry:

split /\s*abc\*\s*/

(string in $_)
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
splitting a long string into a list ronrsr Python 8 11-29-2006 08:59 AM
splitting a long string into a list ronrsr Python 1 11-28-2006 05:56 AM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Dibling C++ 0 07-19-2003 04:41 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? Mark C++ 0 07-19-2003 04:24 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Ericson C++ 0 07-19-2003 04:03 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57