Re: IF NOT! IF NOT!
by abstracts (Hermit) on Aug 03, 2001 at 00:25 UTC
|
my @list = qw/hello hunn I am home homealone myhome myhomealone/;
print "$_\n" for grep {$_ !~ /home/} @list;
# hello
# hunn
# I
# am
Hope this helps,,,
Aziz,,, | [reply] [d/l] |
|
A bit excessive, this will work just fine.
grep{$_!~/home/&&print"$_\n"} @list;
Which results the same as yours.
UPDATE: Changed map to grep so there would be no void context stuff, thanks runrig.
$_.=($=+(6<<1));print(chr(my$a=$_));$^H=$_+$_;$_=$^H;
print chr($_-39); # Easy but its ok.
| [reply] [d/l] [select] |
|
Hello
You can go even with
/home/||print"$_\n"for@list
But this is not golf :-)
Aziz,,, | [reply] [d/l] |
|
And that is using map in a void context (creating an array with map and throwing it away). Better to just use a for loop:
/home/ and print "$_\n" for @list;
Update: Damian's right (in reply below). reverse the logic:/home/ or print "$_\n" for @list;
| [reply] [d/l] [select] |
|
Re: font color="#ff0000"IF NOT! IF NOT!/font
by twerq (Deacon) on Aug 02, 2001 at 22:48 UTC
|
foreach my $word (@words) {
print "$word\n"
if ($word =~ /home/);
}
--twerq | [reply] [d/l] |
|
foreach (@words) {
print "$_\n" unless /home/;
}
| [reply] [d/l] |
Re: font color="#ff0000"IF NOT! IF NOT!/font
by suaveant (Parson) on Aug 02, 2001 at 22:51 UTC
|
Grep returns a list, so I'm not sure what the ! will do...
you probably either want
unless(grep /home/, @words) {
print $words[$x];
}
Your problem is you need to pass grep an array @words, not
part of the array $words[$x]... if $words[$x] contains a sub array you need to dereference it like... @{$words[$x]}
It would help if you said a little more about what the data is
Update Nevermind... from other people responses I see what is going on... yeah, you probably want
print $words[$x] if $words[$x] =~ /home/;
oops :)
- Ant | [reply] [d/l] [select] |
|
As an aside, the construct ! grep EXPR, LIST is used in a boolean context. Either grep returns a list or it doesn't. ! evaluates in a boolean context. A list is TRUE and no list is FALSE.
I've used it a lot in extended EXPR's, so that I don't have to work through the negation of my && or || that sending if to unless (or vice-versa) would do.
--------
/me wants to be the brightest bulb in the chandelier!
| [reply] [d/l] |
|
| [reply] [d/l] |
|
|
|
|
Ahhh... I wasn't sure... the code had me a bit confused anyway... backwards logic does that :)
- Ant
| [reply] |