|
#!/usr/bin/perl -w
# # Splitting a string of format nn-nn-nn-nn-nn-(nn) out into 6 variables # for each nn, where nn is a number from 0-99 (containing 1 or 2 digits). # # Here is an example string. $examplestring = "0-12-34-4-56-(78)"; # We can assign an array to the values that normally go into $1, $2, etc. if (@arrayofvalues = $examplestring =~ /^(\d+)-(\d+)-(\d+)-(\d+)-(\d+)-\((\d+)\)/) { print "@arrayofvalues \n"; } else { # do something sensible in the event of the string being corrupt, e.g. die "error: invalid input string $examplestring\n"; } # Alternatively, we can assign meaningful variable names to the # substrings ($1, etc) directly like this: if (($one, $two, $three, $four, $five, $six) = $examplestring =~ /^(\d+)-(\d+)-(\d+)-(\d+)-(\d+)-\((\d+)\)/) { print "$one, $two, $three, $four, $five, $six\n"; } else { # do something sensible in the event of the string being corrupt, e.g. die "error: invalid input string $examplestring\n"; } |