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:
Cut string

 



w0nz3r
New User

Mar 13, 2014, 4:18 AM

Post #1 of 3 (1348 views)
Cut string Can't Post

Hello guys,

i need some help for cut a string.
I Have this string in the variable named "perf".

'One=21.00 'Two'=32.00 'Three'=42.00

I need to update the variable "perf" as this:

21.00

What can we do is to cut after the first "=" and to keep the next 5 char.
Is it possibile? Do you have any other advice?

Thank you very much!!!

-w-


BillKSmith
Veteran

Mar 13, 2014, 6:26 AM

Post #2 of 3 (1344 views)
Re: [w0nz3r] Cut string [In reply to] Can't Post

This is a job for a regular expression. If you are certain that you always want the first five characters after the first equal sign, you can do that.

Code
$perf =~ /=(.{5})/; 
$perf = $1;


Your real requirement probably is to get the first number after the equal sign. If we can assume that the number always contains a decimal point, use:

Code
$perf =~ /=(\d*\.\d+)/; 
$perf = $1;


You should try to write your regex to match everything that you do want, and nothing else. In most programs, you should test for a successful match before you overwrite the original string.
Good Luck,
Bill


w0nz3r
New User

Mar 13, 2014, 7:00 AM

Post #3 of 3 (1341 views)
Re: [BillKSmith] Cut string [In reply to] Can't Post

Thank you Bill,

all the two options work well!
I opted for the second one.

Have a nice day!

-w-

 
 


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

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