in reply to Returning and passing data to and from subroutines

while (@data) { $data[$loop_count] =~ m{s/^\s*//}xms; $data[$loop_count] =~ m{s/\s*$//}xms; $loop_count++; }

In addition to what toolic said, your loop would never end, as the condition tests the number of elements in the array, which doesn't change in the loop never becomes zero/false.

You probably want  (though I'm not really sure what the substitutions are meant to do — what's the idea behind the /xms?)

for (@data) { # remove leading/trailing whitespace s/^\s*//; s/\s*$//; }

The current value being iterated over is aliased to $_, which allows you to change the original values by modifying $_.

Replies are listed 'Best First'.
Re^2: Returning and passing data to and from subroutines
by jethro (Monsignor) on Jul 07, 2010 at 23:45 UTC

    Using xms switches and m{} instead of // with every regex is a suggestion by Damian Conway in his book 'Perl Best Practices'

    So vendion really wanted to write

    s{\A \s* }{}xms

    UPDATE: Removed a \Z that shouldn't have been there

      So vendion really wanted to write ...

      Well, just another speculation :)

      The OP said in the comment "...testing the input for valid alphanumeric characters".  What you're suggesting would remove all whitespace if there's nothing but whitespace in the string.

        Sorry, I added a \Z that wasn't in the source (so it just removes leading spaces). My point was about the style not the contents of the regex.

      Yes I was trying to follow the suggestion in the 'Perl Best Practices' book, but what I was wanting to go for was removing the whitespace before and after any kind of text. Sorry for the confusion