LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-19-2003, 04:30 AM   #1
farhanali
LQ Newbie
 
Registered: Apr 2003
Location: Pakistan
Distribution: Ubuntu
Posts: 15

Rep: Reputation: 0
Unhappy perl string split problem!


hi all

i have a problem i have a string scalar that may be in the following form;

$mystring = "23+32";

now i want to split this up into two variables and perform addition on the two variable.
The problem is i cant split the variable with the folloing code
($one , $two) = split /'+'/ ,$mystring;

can someone help me please.

please forgive my ignorance as i am a complete newbie at perl.

thanks is advance.
Farhan Ali
 
Old 06-19-2003, 04:48 AM   #2
jharris
Senior Member
 
Registered: May 2001
Location: Bristol, UK
Distribution: Slackware, Fedora, RHES
Posts: 2,243

Rep: Reputation: 47
Re: perl string split problem!

Quote:
Originally posted by farhanali
($one , $two) = split /'+'/ ,$mystring;
Should be
Code:
($one , $two) = split /\+/ ,$mystring;
You need to delimit the + as currently your RegEx is matching "one or more" ' followed by a single '

HTH

Jamie...
 
Old 06-19-2003, 04:49 AM   #3
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
$mystring=~/(\d+)\+(\d+)/;
print $1 + $2;
of course ytou don't need to do that att all, just eval() the string, and it will do the addition by itself.
 
Old 06-19-2003, 02:19 PM   #4
farhanali
LQ Newbie
 
Registered: Apr 2003
Location: Pakistan
Distribution: Ubuntu
Posts: 15

Original Poster
Rep: Reputation: 0
thanks man .... that eval() tip was really neat. i found out that not only does it evaluate arithmatic but also other perl functions and commands .... really neat

thanks once again...

Farhan Ali
 
Old 06-22-2003, 07:08 AM   #5
Salz
LQ Newbie
 
Registered: Jun 2003
Distribution: Debian
Posts: 23

Rep: Reputation: 15
Be careful with what you eval(), especially if it is user input. Or you might end up eval()ing a system "rm -rf /"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Split a string on newlines (bash) rose_bud4201 Programming 7 04-14-2005 01:58 PM
perl string comparison problem AM1SHFURN1TURE Programming 3 03-06-2005 10:29 AM
split string prob izza_azhar Programming 3 02-08-2005 12:11 AM
split string izza_azhar Programming 6 01-18-2005 08:24 PM
Need to split an input string general4172 Linux - Software 6 10-29-2003 11:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration