Here's one way using a subroutine and the ternary operator. It will return $str if $str isn't empty, and if it is, returns 0.
#!/usr/bin/perl use warnings; use strict; my $string = "data.csv"; my $return = num($string); sub num { my $str = shift; $str =~ s/\D//g; return $str ? $str : 0; }
Or without the sub:
my $str = "data.csv"; $str =~ s/\D//g; $str = $str ? $str : 0; print $str;
-stevieb
In reply to Re: How to remove non-numbers from string
by stevieb
in thread How to remove non-numbers from string
by kgherman
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |