I have a long regular expression that parses a text file into various match variables.
For robustness, the match variables are likely to contain white space. I'd like to remove the whitespace in a systematic way by iterating over the match variables.
For example, I have match variables $2
through $14
that contain some whitespace.
I could do:
my @columns = my ($serNum, $helixID, $initResName, $initChainID,
$initSeqNum, $initIcode, $endResName, $endChainID, $endSeqNum,
$endICode, $helixClass, $comment, $length) =
($2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14);
### Remove whitespace
foreach my $element (0..$#columns) {
$columns[$element] =~ s/^\s+//;
$columns[$element] =~ s/\s+$//;
}
But this only removes the white space in the elements in @column
, and leaves the properly named scalars, $serNum
, $helixID
, etc., untouched.
Is there a way to remove the white space in each of the match variables before I copy them to more well-named scalars, or is there a way to iterate over these well-named scalars themselves and remove the whitespace from there?
I presume there might be some way to do this with references.