473,651 Members | 3,093 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

splitting problem

Jan
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?
Jul 19 '05 #1
3 2209
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

Jul 19 '05 #2
÷ ÐÉÓØÍÅ 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($patt ern)
split(/$pattern/,$string);
--
With best wishes Nikolay
mail: ni*****@asu.ntu-kpi.kiev.ua
ICQ#: 136497739

Jul 19 '05 #3
Jan
Thank you, the examples work!
.... and excuse me about posting this thread in the wring newsgroup.
Jan
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
2062
by: robsom | last post by:
Hi, I have a problem with a small python program I'm trying to write and I hope somebody may help me. I'm working on tables of this kind: CGA 1988 06 21 13 48 G500-050 D 509.62 J.. R1 1993 01 28 00 00 880006 CGA 1988 06 21 14 04 G500-051 D 550.62 J.. R1 1993 01 28 00 00 880007 I have to read each line of the table and put it into comma-separated lists like these for later manipulation: ...
10
1923
by: Angelo Secchi | last post by:
Hi, I have string of numbers and words like ',,,,,,23,,,asd,,,,,"name,surname",,,,,,,\n' and I would like to split (I'm using string.split()) it using comma as separator but I do not want to split in two also the "name,surname" field. In other word I would like python in separating fields to skip that particular comma.
3
2534
by: William Ahern | last post by:
I'm looking for resources on splitting and merging XML trees. Specifically, on methods to pare large XML documents into smaller documents which can be merged later. Off of the top of my head, I can envision unions of node sets, and unions of node text. But I know there's much more to the subject than that, if not more alternatives than greater technical detail. TIA,
5
1285
by: Peter Oliphant | last post by:
I was thinking it might be a good idea to split this newsgroup into different newsgroups, depending on the version of VS C++.NET being discussed. Thus, there would be 2002, 2003, and 2005 newsgroups instead of just one for all. This would at least end any confusion if someone asks a question and doesn't state which version they are using (which of course can change the answer to such questions). Just a thought...
0
924
by: melis | last post by:
Hi all, I am new to MFC, and cannot find a way to the following problem :( What I am trying to do is just to split the window into two parts, tyring to have a CFormView or CDialog on left and a Cview on the right. I found lots of examples and tried them all-now I want to make a new project from scratch, when I try to do so, I have no problem in splitting the window to two Cviews but get an assert failure error when I try it with to use...
2
1481
by: CharChabil | last post by:
Using Vb.net 2005, I want to read each part in this string in an array (splitting the string) ----------- A1/EXT "BK82 LB73 21233" 105 061018 1804 ----------- That Code that i used is as follow: Dim s As String, h As String Dim delim(1) As Char delim(0) = "/"
9
8853
by: conspireagainst | last post by:
I'm having quite a time with this particular problem: I have users that enter tag words as form input, let's say for a photo or a topic of discussion. They are allowed to delimit tags with spaces and commas, and can use quotes to encapsulate multiple words. An example: tag1, tag2 tag3, "tag4 tag4, tag4" tag5, "tag6 tag6" So, as we can see here anything is allowed, but the problem is that splitting on commas obviously destroys tag4...
2
3264
by: shadow_ | last post by:
Hi i m new at C and trying to write a parser and a string class. Basicly program will read data from file and splits it into lines then lines to words. i used strtok function for splitting data to lines it worked quite well but srttok isnot working for multiple blank or commas. Can strtok do this kind of splitting if it cant what should i use . Unal
6
1702
by: jacc14 | last post by:
Good morning all. I have been working on a database for the past couple of weeks and it is pretty nippy. I have an ODBC link in there from another software program. Since splitting it and putting the backend in a backend folder (on the same network) may I add, it has really slowed down. When you first open the form it is quite quick but after that it slows down. Its as if there is something running in the background. Not sure if my...
37
1832
by: xyz | last post by:
I have a string 16:23:18.659343 131.188.37.230.22 131.188.37.59.1398 tcp 168 for example lets say for the above string 16:23:18.659343 -- time 131.188.37.230 -- srcaddress 22 --srcport 131.188.37.59 --destaddress 1398 --destport tcp --protocol
0
8367
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8279
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8811
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8467
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8589
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4145
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4291
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.