Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > splitting problem

Reply
Thread Tools

splitting problem

 
 
Jan
Guest
Posts: n/a
 
      10-31-2003
Hi!
I have to split a string with a pattern which contains sometimes chars
like + \ (the code is needed in an interpreter, written in Perl, of my
own scripting language, so I never know the pattern exactly).

split(/$pattern/, $string);

It works well most of all time, but if the pattern is + the execution
of the program stops and perl prints an error message.

$pattern = '+';
split(/"$pattern"/, $string); #ok

$pattern = '#';
split(/"$pattern"/, $string); #doesn't work really, but no
error
#occurs

Can anyone tell me how to solve this problem?
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      10-31-2003
Jan wrote:
> I have to split a string with a pattern which contains sometimes chars
> like + \ (the code is needed in an interpreter, written in Perl, of my
> own scripting language, so I never know the pattern exactly).
>
> split(/$pattern/, $string);
>
> It works well most of all time, but if the pattern is + the execution
> of the program stops and perl prints an error message.
>
> $pattern = '+';
> split(/"$pattern"/, $string); #ok
>
> $pattern = '#';
> split(/"$pattern"/, $string); #doesn't work really, but no error
> #occurs
>
> Can anyone tell me how to solve this problem?


I don't think any of those works, since you are actually trying to
split on the strings '"+"' and '"#"'.

You should escape special characters. Try:

split /\Q$pattern/, $string;

Also, stop asking questions in this newsgroup. It's defunct. Use
comp.lang.perl.misc instead.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
 
 
 
nikolay
Guest
Posts: n/a
 
      10-31-2003
В письме Fri, 31 Oct 2003 00:35:15 -0800, Jan написал:

> Hi!

Hi

> split(/$pattern/, $string);
>
> It works well most of all time, but if the pattern is + the execution
> of the program stops and perl prints an error message.

This is most like '+' means repeat more than zero.
I recommend do something like that if you want pattern to be a string
(not regexp):
$pattern = "string";
$qpattern = quotemeta($pattern)
split(/$pattern/,$string);


--
With best wishes Nikolay
mail: http://www.velocityreviews.com/forums/(E-Mail Removed)-kpi.kiev.ua
ICQ#: 136497739

 
Reply With Quote
 
Jan
Guest
Posts: n/a
 
      10-31-2003
Thank you, the examples work!
.... and excuse me about posting this thread in the wring newsgroup.
Jan
 
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
Problem splitting lines in a file Mothra Perl 1 01-12-2005 02:09 PM
Problem in splitting a string Angelo Secchi Python 10 07-22-2004 08:40 PM
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