Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have data that I do not want to print out:
aaa Anything with a "/" in it. Anthing beginning with a "[". Anything with the word "and".


I do not want these outputs (as an example):
aaa Name1/Name2 [output here etc word and word
My rookie script (with only the 'aaa' part working):
foreach (keys %data) { next if (($_ eq 'aaa') || ($_ m/\[/) || ($_ m/\/) || $_ m/and/)); print "$_\n"; }

Replies are listed 'Best First'.
Re: Req Expresson on my output
by BrowserUk (Patriarch) on Nov 14, 2002 at 16:37 UTC

    Close, but you needn't keep repeating the $_. Try.

    foreach (keys %data) { next if m/^aaa$/ || m/^\[/ || m/\// || m/and/; print "$_\n"; }

    The match operator m// will match against $_ unless you use the bind operator =~. The above is equivalent to

    foreach (keys %data) { next if $_ =~ m/^aaa$/ || $_ =~ m/^\[/ || $_ =~ m/\// || $_ =~ m/and/; print "$_\n"; }

    Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
    Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
    Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
    Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

Re: Req Expresson on my output
by Three (Pilgrim) on Nov 14, 2002 at 16:39 UTC
    Give this a try.
    foreach (keys %data) { next if (/^aaa$/or /^\[/ or /and/i or /\//); print "$_\n"; }
Re: Req Expresson on my output
by dingus (Friar) on Nov 14, 2002 at 16:43 UTC
    Use a single regex and the | operator.
    foreach (keys %data) { next if ($_ =~ m!(?:^aaa$|/|^\(|and)! ); print "$_\n"; }
    Note: I'm grouping using (?: ) rather than plain ( ) because its more efficient when you don't want the contents of the group.

    PS: are you sure you don't want $data{$_} instead of $_ in either the compare or the print?
    PPS: If you want to use $data{$_} in both the compare and the print then you should use values %data instead.

    Dingus


    Enter any 47-digit prime number to continue.