Home: Perl Programming Help: Beginner:
Cut string

New User

Mar 13, 2014, 4:18 AM

Views: 2643
Cut string

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

Views: 2639
Re: [w0nz3r] Cut string

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

Views: 2636
Re: [BillKSmith] Cut string

Thank you Bill,

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

Have a nice day!