in reply to Pattern matching and deriving the data between the "(double quotes) in HTML tag
Hello sp4rperl, and welcome to the Monastery!
To elaborate a little on tybalt89’s answer: By declaring $endTime with my, you make it a lexical variable whose scope is limited to the enclosing block. So when the print statement is reached, $endTime no longer refers to that lexical variable, but rather to an (undeclared) package global of the same name. If you begin your script with:
use strict;
then Perl will give you an error message describing the problem. It’s also a very good idea to add:
use warnings;
to the top of every script. Note also that the /g modifiers on your regular expressions do nothing useful (Update: thanks to AnomalousMonk for the correction below), as in each case you’re looking for a single match only. And you need only one regular expression for the endTime match:
use strict; use warnings; my $timeLimit = '<timeLimit endTime="2016-12-28T23:59:59" startTime="2 +016-09-30T00:00:00"></timeLimit>'; my ($startTime) = $timeLimit =~ /startTime="(.*?)"/; chomp($startTime); if (my ($endTime) = $timeLimit =~ /endTime="(.*?)"/) { chomp($endTime); print "[$startTime],[$endTime]\n"; } else { print "[$startTime]\n"; }
(I’m assuming that chomp($timeLimit); is a mistake for chomp($startTime);.)
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|