in reply to Parsing with Regular Expressions
Always check whether your regular expression matched before accessing the capture variables $1, $2 ...:
#!/usr/contrib/bin/perl # Parses a string in the format: # REFDATA_ERROR[type=<1>,system=<2>,category=<3>,code=<4>] # Prints out the four parameters, <1>, <2>, <3>, <4> sub parse_error { $params =~ m/REFDATA_ERROR\[type=(.*),system=(.*),category=(.*),code +=(.*)\]/ or die "The input string was invalid: '$params'"; print "$params\n"; print "$1\n"; print "$2\n"; print "$3\n"; print "$4\n"; }; # Main parse_error("REFDATA_ERROR[type=val1,system=val2,category=val3,code=va +l4]");
If you had used strict, Perl would have told you that your variable $params is undefined, as you seem to have a misconception on how parameters are passed around in Perl. The input parameters to your function live in the @_ array. Also, you are using a prototype on your sub declaration, which is in most cases a bad thing to do unless you know what you're doing (you don't).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Parsing with Regular Expressions
by ysth (Canon) on Nov 17, 2004 at 16:51 UTC |