in reply to Grep & Regex Question

The problem is the spaces you have around $position in the grep, as illustrated by the following test code:

my @priority = qw/RB1 RB2 FL1 FL2 QB1 QB2 TE1 WR1 WR2 DST/; my $position = q{QB2}; # Print original list print join q{ }, @priority; # Process using original regex, with spaces @priority = grep {!/ $position / } @priority; print join q{ }, @priority; # Process using regex with spaces removed @priority = grep {!/$position/ } @priority; print join q{ }, @priority;

Output:

RB1 RB2 FL1 FL2 QB1 QB2 TE1 WR1 WR2 DST RB1 RB2 FL1 FL2 QB1 QB2 TE1 WR1 WR2 DST RB1 RB2 FL1 FL2 QB1 TE1 WR1 WR2 DST

An alternative might be the /x regex modifier, which tells the regex parser to ignore most non-backslashed, non-character class whitespace.

Hope that helps.

Replies are listed 'Best First'.
Re^2: Grep & Regex Question
by jdlev (Scribe) on Dec 01, 2013 at 09:41 UTC
    Excellent. Got it to work! I'm just curious about one thing. In order to use the user input prompt to assign the variable, I had to change it to:

    chomp($position = <>);

    Why do you have to add chomp in order to get the user input to work normally? It looks like it adds a new line after a user input's data for some reason if you just do $position = <>;

    I love it when a program comes together - jdhannibal
      It does not add it, the user does: he/she ends his/her input with a newline.
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ