in reply to Obtain information from a string

Use a regular expression. If you don't understand why this works, see perlre. Is "X" a dollar amount, an integer, or some other number format? This will work for integers:
my $string = "foo bar baz blah Total Cost: 480 foo foo foo"; my ($cost) = $string =~ /Total\s+Cost:\s*(\d+)/; $cost ||= 0;