M
mynews
Whis is difference between map and for-each?
Why the map function is fast than for-each? <== is it for all case?
Why the map function is fast than for-each? <== is it for all case?
Whis is difference between map and for-each?
Why the map function is
fast than for-each? <== is it for all case?
mynews said:Whis is difference between map and for-each?
Why the map function is fast than for-each? <== is it for all case?
Gerry Ford said:Jürgen Exner said:They have very little in comon except that both loop over the elements
of a list. map() is a function, foreach is a statement modifier or a
compound (loop) statement.
While sometimes map() can be used to achive similar results as a
foreach, usually that's not a good idea because you create a return
value only to throw it away.
Vice-versa you can use map() to return a completely different list than
its argument while modifying the list of a foreach loop is strongly
discouraged and may lead to very unexpected results.
Also closures are more natural with map().
It is? That would surprise me, but I haven't run any benchmarks.
I think of foreach as a loop and map as a function like this:
map ($_->[$subject_offset].' from '.$_->[$from_offset], @xover)
They don't seem like comparables to me.
Is this still true? I thought map in void context was optimized.They have very little in comon except that both loop over the elements
of a list. map() is a function, foreach is a statement modifier or a
compound (loop) statement.
While sometimes map() can be used to achive similar results as a
foreach, usually that's not a good idea because you create a return
value only to throw it away.
Is this still true? I thought map in void context was optimized.
Uri Guttman said:map is such a basic concept i wonder why it seems to be a stumbling
block for so many newbies.
GF> I tried both versions of this without any success whatsoever:GF> "Uri Guttman said:untested:
return unless open(my $file, "<$killrc") ;
return map {
/^([^#]+)#?$/ &&
eval{ qr/$1/} || prompt $@, ()
} <$file> ;
my @list1 = qw( Mon Tu Wed);
print @list1;
Perl is so idiomatic, it makes me scream. The syntax for the print
statement is:
print LIST
How this doesn't fit the bill is beyond me.
print @list1;
I simply can't see what's wrong with this:
#!/usr/bin/perl -w
use strict;
my $killrc = "sample.killrc";
my @filter;
my @list1 = qw( Mon Tu Wed);
open(FILE, "<$killrc") and do {
@filter = ();
foreach (<FILE>) {
chomp; length or next; /^#/o and next;
my $pat; eval '$pat = qr/$_/' or do {prompt $@; next};
push @filter, $pat;
}
close(FILE);
Change:
}
print @filter;
print @list1;
# perl mats5.pl 2>text50.txt >text51.txt
__END__
Perl.exe says:
syntax error at mats5.pl line 18, near "print"
Gerry Ford said:How does a person invoke
the debugger?
Perl.exe says:
syntax error at mats5.pl line 18, near "print"
Execution of mats5.pl aborted due to compilation errors.
I simply can't see what's wrong with this:
#!/usr/bin/perl -w
use strict;
my $killrc = "sample.killrc";
my @filter;
my @list1 = qw( Mon Tu Wed);
open(FILE, "<$killrc") and do {
@filter = ();
foreach (<FILE>) {
chomp; length or next; /^#/o and next;
my $pat; eval '$pat = qr/$_/' or do {prompt $@; next};
push @filter, $pat;
}
close(FILE);
}
print @filter;
print @list1;
Jürgen Exner said:Man, dude!!! How are we supposed to guess??? You could have told us
that your beef is not with the functionality of print() but with a
syntax error in that program! Thank you very much for throwing around
red herrings.
You got some 'intesting' ways of doing things. I know, there is more
than one way to do things, but your style is kind of pushing it
Better to use
use warnings;
The more standard way would be
open (FILE, "<", $killrc) or die ("Cannot open $killrc: $!";
Probably it is just me, but I have an strong adversion against mixing
expressions and flow controll in such a way. I would write it as
next unless length;
next if /^#/o;
IMNSHO this is _much_ easier to read and understand.
And this is where your unorthodox style bites you. Did you really mean
to eval() the whole rest of the line? Because the argument to eval is
indeed
qr/$_/' or do {prompt $@; next}
I suppose you meant to eval only the
qr/$_/'
Solution: enclose the argument to eval in paranthesis and the mysterious
syntax error is gone:
^^^^my $pat; eval ('$pat = qr/$_/') or do {prompt $@; next};
However, I wonder what you are trying to achive by using eval in the
first place. I don't see any need for it.
Thanks, Jim. Missing semicolons were the culprit:
#!/usr/bin/perl -w
use strict;
use warnings;
my $killrc = "sample.killrc";
my @filter;
my @list1 = qw( Mon Tu Wed);
open(FILE, "<$killrc") and do {
@filter = ();
foreach (<FILE>) {
chomp; length or next; /^#/o and next;
my $pat; eval '$pat = qr/$_/' or do {prompt $@; next};
push @filter, $pat;
};
close(FILE);
};
print @filter;
print @list1;
# perl mats5.pl 2>text50.txt >text51.txt
__END__
#end script begin output
(?-xism:^From:.*<[email protected]>)(?-xism:^Subject:.*MONEY)(?-xism:^Message-ID:.*googlegroups)MonTuWed
#end output show sample.killrc
^From:.*<[email protected]>
^Subject:.*MONEY
^Message-ID:.*googlegroups
Any ideas where the ?-xism is coming from?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.