views:

88

answers:

2

Is there an easier way to grab only one element out of a match other than doing the followng:

my $date = ($xml_file =~ m/(\d+)-sys_char/)[0];
# or
my $date = $1 if $xml_file =~ /(\d+)-sys_char/;

Is there a flag to specify that m doesn't return an array but just one concatenated value of all the $# matches, so I can do:?

my $date = ($xml_file =~ mSOMEOPT/(\d+)-sys_char/);

removing the 0 from the end?

+3  A: 
my ($date) = $xml_file =~ m/(\d+)-sys_char/;

if ( defined $date ) {

}
Sinan Ünür
+8  A: 

You want:

my ($date) = ($xml_file =~ m/(\d+)-sys_char/);

This will get you $1 in $date. As for the second part of your question, there's no way to get all of the numbered matches in a single variable, but you can get them all into an array like this:

my @matches = ($xml_file =~ m/(\d+)-sys_char/);

These are actually the same syntax: when the left hand side of a match like this is an array, then an array containing all of the submatches is returned. The first version make ($date) into a one-element array, throwing away the rest of the sub-matches.

JSBangs
ahh, i see, just needed the () around my $date, thx.
Note that the second part just amounts to wanting `my $date = join '', $xml_file =~ m/(\d+)-sys_char/`.
darch