CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Cut string


New User

Mar 13, 2014, 4:18 AM

Post #1 of 3 (2644 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:


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!!!



Mar 13, 2014, 6:26 AM

Post #2 of 3 (2640 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.

$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:

$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,

New User

Mar 13, 2014, 7:00 AM

Post #3 of 3 (2637 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!



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

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