Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Grep & Regex Question

by jdlev (Scribe)
on Dec 01, 2013 at 08:04 UTC ( #1065121=perlquestion: print w/ replies, xml ) Need Help??
jdlev has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to do something pretty simple, but can't get it to work. I have an array. I want to ask the user if they want to remove an element. If they do, it will delete the element from the array and rebuild the array.

So for instance, if I have the following:

@array = qw(a b c);

And the user enters "b", the array would become:

@array = qw(a c);

Grep doesn't seem too fond of feeding it variables though. Anyways, here's my code:

@priority = qw(RB1 RB2 FL1 FL2 QB1 QB2 TE1 WR1 WR2 DST); $continue = 'Y'; while($continue == 'Y') { @mypri = grep(!/ $position /, @priority); print "Random position iterations in order of priority: \n"; foreach (@mypri) { print "$_\n"; } print "Do you wish to lock another player position? Enter 'Y' to add a +nother player or press Enter to continue"; $continue = <>; }
I love it when a program comes together - jdhannibal

Comment on Grep & Regex Question
Download Code
Re: Grep & Regex Question
by Corion (Pope) on Dec 01, 2013 at 08:27 UTC

    What is the following regular expression supposed to match?

    / $position /

    Where do you set $position? Are there blanks in front and back of your target strings?

    What steps have you taken to debug this problem?

Re: Grep & Regex Question
by atcroft (Monsignor) on Dec 01, 2013 at 08:36 UTC

    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;

    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.

      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.
        لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Grep & Regex Question
by choroba (Abbot) on Dec 01, 2013 at 08:37 UTC
    == compares numbers, use eq to compare strings:
    while ($continue eq 'Y')

    Numerically, 'Y' is zero.

    Moreover, see chomp to fix

    $continue = <>;
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Grep & Regex Question
by ysth (Canon) on Dec 01, 2013 at 09:24 UTC
    Perhaps you mean something like:
    my $position = 'FL1'; my @priority = qw(RB1 RB2 FL1 FL2 QB1 QB2 TE1 WR1 WR2 DST); @priority = grep $_ ne $position, @priority;
    --
    A math joke: r = | |csc(θ)|+|sec(θ)| |-| |csc(θ)|-|sec(θ)| |
Re: Grep & Regex Question
by Kenosis (Priest) on Dec 01, 2013 at 22:53 UTC

    Although ysth has a fix for the following issue, be careful using !/$position/ when grepping to remove unwanted elements, as unintended results may happen:

    use strict; use warnings; my @priority = qw(RB1 RB2 FL1 FL11 FL2 QB1 QB2 TE1 WR1 WR2 DST); my $position = 'FL1'; print "$_\n" for grep !/$position/, @priority;

    Output:

    RB1 RB2 FL2 QB1 QB2 TE1 WR1 WR2 DST

    Note that elements 'FL1' and 'FL11' were removed. To avoid this, use either ysth's $_ ne $position or !/^$position$/ (which forces a complete--not just partial--match) when grepping in your script.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1065121]
Approved by boftx
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (17)
As of 2014-08-28 15:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (264 votes), past polls