The map function
am 19.04.2008 05:30:05 von mynewsWhis is difference between map and for-each?
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?
On Sat, 19 Apr 2008 11:30:05 +0800, mynews wrote:
> Whis is difference between map and for-each?
perldoc -f map
> Why the map function is
> fast than for-each? <== is it for all case?
Could you post an example piece of code where the map function is faster
than foreach?
"mynews"
>Whis is difference between map and for-each?
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().
>Why the map function is fast than for-each? <== is it for all case?
It is? That would surprise me, but I haven't run any benchmarks.
jue
"Jürgen Exner"
news:b7qi04hcsmlovev452suvispr820t1u9n0@4ax.com...
> "mynews"
>>Whis is difference between map and for-each?
>
> 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().
>
>>Why the map function is fast than for-each? <== is it for all case?
>
> 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. Of course, there could be a lot
more happening in the above line than I realize. I haven't encountered
foreach as a statement modifier yet.
--
"A belief in a supernatural source of evil is not necessary; men alone
are quite capable of every wickedness."
~~ Joseph Conrad (1857-1924), novelist
_
mynews (sonet.all@gmail.com) wrote on VCCCXLV September MCMXCIII in
@@ Whis is difference between map and for-each?
map is a function, with a return value. foreach is a statement.
@@ Why the map function is fast than for-each? <== is it for all case?
Huh?
Abigail
--
perl -wle 'eval {die ["Just another Perl Hacker"]}; print ${$@}[$#{@${@}}]'
Gerry Ford
>
> "Jürgen Exner"
> news:b7qi04hcsmlovev452suvispr820t1u9n0@4ax.com...
>> "mynews"
>>>Whis is difference between map and for-each?
>>
>> 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().
>>
>>>Why the map function is fast than for-each? <== is it for all case?
>>
>> 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)
You didn't show where the return value from map() is going, I'll assume:
@list = map ($_->[$subject_offset]...
> They don't seem like comparables to me.
@list = ();
foreach ( @xover ) { # untested
push @list, $_->[$subject_offset].' from '.$_->[$from_offset];
}
The contents of @list are the same either way.
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
On Apr 18, 11:49=A0pm, Jürgen Exner
> "mynews"
> >Whis is difference between map and for-each?
>
> They have very little in comon except that both loop over the elements
> of a list. =A0map() 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.
"Tad J McClellan"
news:slrng0mhgh.ujm.tadmc@tadmc30.sbcglobal.net...
> Gerry Ford
>>
>> I think of foreach as a loop and map as a function like this:
>> map ($_->[$subject_offset].' from '.$_->[$from_offset], @xover)
>
>
> You didn't show where the return value from map() is going, I'll assume:
>
> @list = map ($_->[$subject_offset]...
>
>
>> They don't seem like comparables to me.
>
>
> @list = ();
> foreach ( @xover ) { # untested
> push @list, $_->[$subject_offset].' from '.$_->[$from_offset];
> }
>
>
> The contents of @list are the same either way.
Maybe we can use this script to compare and contrast:
sub read_killrc {
open(FILE, "<$killrc") and do {
@filter = ();
foreach (
chomp; length or next; /^#/o and next;
my $pat; eval '$pat = qr/$_/' or do {prompt $@; next};
push @filter, $pat;
}
close(FILE);
};
}
You usually tell me to read when I ask a question. I rebound the camel book
today with plywood strips and tie wire: as good as it was before I tore it
apart and flung it at the wall. I simply *didn't get* some of the examples.
Would you expect the @filter to be a list of regex's here?
What would this syntax look like with a map function?
--
"A belief in a supernatural source of evil is not necessary; men alone
are quite capable of every wickedness."
~~ Joseph Conrad (1857-1924), novelist
>>>>> "GF" == Gerry Ford
GF> Maybe we can use this script to compare and contrast:
GF> sub read_killrc {
GF> open(FILE, "<$killrc") and do {
GF> @filter = ();
not needed in the map version
GF> foreach (
GF> chomp; length or next; /^#/o and next;
GF> my $pat; eval '$pat = qr/$_/' or do {prompt $@; next};
GF> push @filter, $pat;
GF> }
GF> close(FILE);
GF> What would this syntax look like with a map function?
untested:
return unless open(my $file, "<$killrc") ;
return map {
/^([^#]+)#?$/ &&
eval{ qr/$1/} || prompt $@, ()
} <$file> ;
maybe the return failure value should be grepped out but that is a minor
style change.
map makes tight loops like that much cleaner. they remove the collecting
array and its push and uses less perl which generally makes it faster
(in the right conditions, especially with shorter arrays)
map is such a basic concept i wonder why it seems to be a stumbling
block for so many newbies. year after year we get the same map
queries. what use is it? how do i convert loops to maps? is it faster or
slower? void map is ok? some are in the faq but there must be something
missing if this perl learning speed bump always seems to hit so many.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
On Sun, 20 Apr 2008 15:30:06 -0700, nolo contendere wrote:
> On Apr 18, 11:49 pm, Jürgen Exner
>> 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.
There was a discussion about this topic a month ago (started March 22).
Perhaps reading this might help:
http://groups.google.co.jp/group/comp.lang.perl.misc/search? hl=en&q=map
+void+context&start=0&scoring=d&hl=en&
>>>>> "UG" == Uri Guttman
UG> map is such a basic concept i wonder why it seems to be a
UG> stumbling block for so many newbies. year after year we get
UG> the same map queries.
Because most of the time the newbies have run into the other basic
concepts elsewhere -- for loops, if/then, subroutines, etc., even
object orientation -- but it's very likely that Perl is their first
exposure to map and functional programming.
Charlton
--
Charlton Wilbur
cwilbur@chromatico.net
Uri Guttman
>map is such a basic concept i wonder why it seems to be a stumbling
>block for so many newbies.
Actually map() is not as trivial, because it is a higher order function,
i.e. a function that takes a function (or code block) as argument.
I've seen this again and again when we tought functional programming
that there are otherwise smart students, who simply couldn't grasp the
concept that a function can be data (i.e. argument to another function),
too.
And considering that not many students are tought functional programming
at all and that most well-known languages don't have higher-order
functions at all it is not surprising that map() is a speed bump for
many.
Another nice example is sort(). I wonder how many implemented their own
sorting algorithm because they couldn't figure out how to write the
comparision function for non-trivial sorts.
jue
>>>>> "JE" == Jürgen Exner
JE> Uri Guttman
>> map is such a basic concept i wonder why it seems to be a stumbling
>> block for so many newbies.
JE> Actually map() is not as trivial, because it is a higher order
JE> function, i.e. a function that takes a function (or code block) as
JE> argument. I've seen this again and again when we tought
JE> functional programming that there are otherwise smart students,
JE> who simply couldn't grasp the concept that a function can be data
JE> (i.e. argument to another function), too.
coming from assembler where i passed around pointers to code all the
time and similarly i did that in c and i knew list, yes, functional
programming is a core thing with me. but if you teach references first
including dispatch tables, then shifting to functional programming and
map should be easy. i do training too and i don't find it hard to teach
map/grep but they do need some handholding.
JE> Another nice example is sort(). I wonder how many implemented
JE> their own sorting algorithm because they couldn't figure out how
JE> to write the comparision function for non-trivial sorts.
that is one reason i wrote sort::maker! :)
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
"Uri Guttman"
news:x7fxtfeojz.fsf@mail.sysarch.com...
>>>>>> "GF" == Gerry Ford
>
> GF> Maybe we can use this script to compare and contrast:
> GF> sub read_killrc {
> GF> open(FILE, "<$killrc") and do {
> GF> @filter = ();
>
> not needed in the map version
>
> GF> foreach (
> GF> chomp; length or next; /^#/o and next;
> GF> my $pat; eval '$pat = qr/$_/' or do {prompt $@; next};
> GF> push @filter, $pat;
> GF> }
> GF> close(FILE);
>
> GF> What would this syntax look like with a map function?
>
> untested:
>
> return unless open(my $file, "<$killrc") ;
> return map {
> /^([^#]+)#?$/ &&
> eval{ qr/$1/} || prompt $@, ()
> } <$file> ;
>
> maybe the return failure value should be grepped out but that is a minor
> style change.
>
> map makes tight loops like that much cleaner. they remove the collecting
> array and its push and uses less perl which generally makes it faster
> (in the right conditions, especially with shorter arrays)
>
> map is such a basic concept i wonder why it seems to be a stumbling
> block for so many newbies. year after year we get the same map
> queries. what use is it? how do i convert loops to maps? is it faster or
> slower? void map is ok? some are in the faq but there must be something
> missing if this perl learning speed bump always seems to hit so many.
I tried both versions of this without any success whatsoever:
#!/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 (
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__
#!/usr/bin/perl -w
use strict;
my $killrc = "sample.killrc";
return unless open(my $file, "<$killrc") ;
return map {
/^([^#]+)#?$/ &&
eval{ qr/$1/} || prompt $@, ()
} <$file> ;
# perl mats4.pl 2>text50.txt >text51.txt
__END__
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 find no example of printing a list in the camel book. It does have
print reverse sort map (1c) keys %hash;
--
"A belief in a supernatural source of evil is not necessary; men alone
are quite capable of every wickedness."
~~ Joseph Conrad (1857-1924), novelist
>>>>> "GF" == Gerry Ford
GF> "Uri Guttman"
>> untested:
>>
>> return unless open(my $file, "<$killrc") ;
>> return map {
>> /^([^#]+)#?$/ &&
>> eval{ qr/$1/} || prompt $@, ()
>> } <$file> ;
>>
GF> I tried both versions of this without any success whatsoever:
how about showing your output?
GF> #!/usr/bin/perl -w
use warnings is better than -w
GF> use strict;
GF> my $killrc = "sample.killrc";
GF> my @filter;
GF> my @list1 = qw( Mon Tu Wed);
where did that come from?
GF> open(FILE, "<$killrc") and do {
GF> @filter = ();
GF> foreach (
GF> chomp; length or next; /^#/o and next;
GF> my $pat; eval '$pat = qr/$_/' or do {prompt $@; next};
GF> push @filter, $pat;
GF> }
GF> close(FILE);
GF> }
GF> print @filter;
GF> print @list1;
and the results?
GF> # perl mats5.pl 2>text50.txt >text51.txt
why the redirect of stderr? there is no stderr output. and what is in
the prompt sub?
GF> __END__
GF> #!/usr/bin/perl -w
GF> use strict;
GF> my $killrc = "sample.killrc";
my return calls IMPLY A SUB is surrounding it. of course it won't work
as shown. jeez, i can't hold your hand and also blow your nose. think
about the code and don't just copy/run it.
GF> return unless open(my $file, "<$killrc") ;
GF> return map {
GF> /^([^#]+)#?$/ &&
GF> eval{ qr/$1/} || prompt $@, ()
GF> } <$file> ;
GF> # perl mats4.pl 2>text50.txt >text51.txt
GF> __END__
GF> Perl is so idiomatic, it makes me scream. The syntax for the print
GF> statement is:
GF> print LIST
GF> How this doesn't fit the bill is beyond me.
GF> print @list1;
huh? show a complete runnable example of why you think that doesn't
work. my guess is that @list1 has no newlines and so doesn't get printed
until the program ends. this is a well know buffering issue and isn't a
bug in perl.
GF> I find no example of printing a list in the camel book. It does have
GF> print reverse sort map (1c) keys %hash;
and in the FAQ? on cpan? the camel book is just a book. it can't have
every possible example you want. and also note that the perl docs are
the actually final language description, not the camel books.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
"Gerry Ford"
[shortened to key lines]
>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;
Huh??? What do you mean?
That print statement prints exactly the elements of that list. What did
you expect it to print?
jue
>
>I find no example of printing a list in the camel book. It does have
>print reverse sort map (1c) keys %hash;
>>>>> "JE" == Jürgen Exner
JE> "Gerry Ford"
JE> [shortened to key lines]
>> 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;
JE> Huh??? What do you mean?
JE> That print statement prints exactly the elements of that list. What did
JE> you expect it to print?
see my recent reply to this. my bet is it is the typical missing
newline/stdout buffering issue. note that @list1 has no newlines nor
does the built up array. and he never showed the output, just how he
collected it.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
"Uri Guttman"
news:x78wz6tscj.fsf@mail.sysarch.com...
>>>>>> "JE" == Jürgen Exner
>
> JE> "Gerry Ford"
> JE> [shortened to key lines]
> >> 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;
>
> JE> Huh??? What do you mean?
> JE> That print statement prints exactly the elements of that list. What
> did
> JE> you expect it to print?
>
> see my recent reply to this. my bet is it is the typical missing
> newline/stdout buffering issue. note that @list1 has no newlines nor
> does the built up array. and he never showed the output, just how he
> collected it.
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 (
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__
Perl.exe says:
syntax error at mats5.pl line 18, near "print"
Execution of mats5.pl aborted due to compilation errors.
There's no output to show. Braces look matched. How does a person invoke
the debugger?
--
"Life in Lubbock, Texas, taught me two things: One is that God loves you
and you're going to burn in hell. The other is that sex is the most
awful, filthy thing on earth and you should save it for someone you love."
~~ Butch Hancock
On 2008-04-22, Gerry Ford
>
> "Uri Guttman"
> news:x78wz6tscj.fsf@mail.sysarch.com...
>>>>>>> "JE" == Jürgen Exner
>>
>> JE> "Gerry Ford"
>> JE> [shortened to key lines]
>> >> 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;
>>
>> JE> Huh??? What do you mean?
>> JE> That print statement prints exactly the elements of that list. What
>> did
>> JE> you expect it to print?
>>
>> see my recent reply to this. my bet is it is the typical missing
>> newline/stdout buffering issue. note that @list1 has no newlines nor
>> does the built up array. and he never showed the output, just how he
>> collected it.
>
> 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 (
> chomp; length or next; /^#/o and next;
> my $pat; eval '$pat = qr/$_/' or do {prompt $@; next};
> push @filter, $pat;
> }
> close(FILE);
>
Change:
> }
to:
};
(i.e., missing a ';' - and your code could be formatted better)
> 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"
The compiler was pointing pretty much right at the problem.
--
Gerry Ford
> How does a person invoke
> the debugger?
perldoc -q debug
How do I debug my Perl programs?
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
"Gerry Ford"
[Partially rearragned for better reading flow]
>"Uri Guttman"
>>>>>>> "JE" == Jürgen Exner
>> JE> "Gerry Ford"
>> JE> [shortened to key lines]
>> >> 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;
>>
>> JE> Huh??? What do you mean?
>> JE> That print statement prints exactly the elements of that list. What
>> did
>> JE> you expect it to print?
>>
>> see my recent reply to this. my bet is it is the typical missing
>> newline/stdout buffering issue. note that @list1 has no newlines nor
>> does the built up array. and he never showed the output, just how he
>> collected it.
>Perl.exe says:
>syntax error at mats5.pl line 18, near "print"
>Execution of mats5.pl aborted due to compilation errors.
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.
>I simply can't see what's wrong with this:
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
>#!/usr/bin/perl -w
Better to use
use warnings;
>use strict;
>
>my $killrc = "sample.killrc";
>my @filter;
>my @list1 = qw( Mon Tu Wed);
> open(FILE, "<$killrc") and do {
The more standard way would be
open (FILE, "<", $killrc) or die ("Cannot open $killrc: $!";
> @filter = ();
> foreach (
> chomp; length or next; /^#/o and next;
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.
> my $pat; eval '$pat = qr/$_/' or do {prompt $@; next};
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.
> push @filter, $pat;
> }
> close(FILE);
>
>}
>print @filter;
>print @list1;
jue
>>>>> "JE" == Jürgen Exner
>> my $pat; eval '$pat = qr/$_/' or do {prompt $@; next};
JE> And this is where your unorthodox style bites you. Did you really mean
JE> to eval() the whole rest of the line? Because the argument to eval is
JE> indeed
JE> qr/$_/' or do {prompt $@; next}
JE> I suppose you meant to eval only the
JE> qr/$_/'
or has a very low precedence and that should parse as he intended. sure
it is bad style in other ways but or will bind lower than a call to eval
and its single EXPR.
JE> However, I wonder what you are trying to achive by using eval in the
JE> first place. I don't see any need for it.
i said that before and showed how to just use the qr// directly in my
map example. which he then didn't wrap in a sub as i only showed the map
call.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
Jürgen Exner wrote:
> "Gerry Ford"
>>=20
>> Perl.exe says:
>> syntax error at mats5.pl line 18, near "print"
>> Execution of mats5.pl aborted due to compilation errors.
>=20
> 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.
>=20
>> I simply can't see what's wrong with this:
>=20
> 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
> =20
>> #!/usr/bin/perl -w
>=20
> Better to use=20
> use warnings;
>> use strict;
>>
>> my $killrc =3D "sample.killrc";
>> my @filter;
>> my @list1 =3D qw( Mon Tu Wed);
>> open(FILE, "<$killrc") and do {
>=20
> The more standard way would be=20
> open (FILE, "<", $killrc) or die ("Cannot open $killrc: $!";
>=20
>> @filter =3D ();
>> foreach (
>> chomp; length or next; /^#/o and next;
>=20
> 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;
Also the /o option is superfluous there as there are no variables in the =
pattern.
> IMNSHO this is _much_ easier to read and understand.
>=20
>> my $pat; eval '$pat =3D qr/$_/' or do {prompt $@; next};
>=20
> 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=20
> qr/$_/' or do {prompt $@; next}
No it isn't:
$ perl -MO=3DDeparse -e'my $pat; eval q[$pat =3D qr/$_/] or do {prompt $@=
;=20
next};'
my $pat;
unless (eval '$pat =3D qr/$_/') {
do {
$@->prompt;
next;
};
}
-e syntax OK
> I suppose you meant to eval only the=20
> qr/$_/'=20
It looks like he meant to eval the string '$pat =3D qr/$_/'.
> Solution: enclose the argument to eval in paranthesis and the mysteriou=
s
> syntax error is gone:
The syntax error is not related at all to this statement. Jim Cochrane=20
posted the correct solution. The problem is that the do {} block=20
following the open() does not end with a semicolon.
> my $pat; eval ('$pat =3D qr/$_/') or do {prompt $@; next};
>=20
> However, I wonder what you are trying to achive by using eval in the
> first place. I don't see any need for it.
>=20
>> push @filter, $pat;
>> }
>> close(FILE);
>>
>> }
^^^^
Missing ; for do {} block.
>> print @filter;
>> print @list1;
John
--=20
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
>>>>> "JWK" == John W Krahn
JWK> The syntax error is not related at all to this statement. Jim Cochrane
JWK> posted the correct solution. The problem is that the do {} block
JWK> following the open() does not end with a semicolon.
>>> }
JWK> ^^^^
JWK> Missing ; for do {} block.
another reason to avoid do blocks. in a 10k line system i wrote i found
2 uses of do blocks. and in the OP's case the normal open or die/return
is of course the better style. having a large do block after an or is
just insane. an if/then would do the same and be much more normal and
readable if you couldn't use statement modifiers or such.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
"Jim Cochrane"
news:slrng0r3ie.j1a.allergic-to-spam@no-spam-allowed.org...
> On 2008-04-22, Gerry Ford
> Change:
>> }
>
> to:
> };
>
> (i.e., missing a ';' - and your code could be formatted better)
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 (
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:.*
#end output show sample.killrc
^From:.*
^Subject:.*MONEY
^Message-ID:.*googlegroups
Any ideas where the ?-xism is coming from?
>> Perl.exe says:
>> syntax error at mats5.pl line 18, near "print"
>
> The compiler was pointing pretty much right at the problem.
I beg to differ. I snipped this from an actual, working perl program. How
do you know which braces need a semicolon after?
sub read_killrc {
open(FILE, "<$killrc") and do {
@filter = ();
foreach (
chomp; length or next; /^#/o and next;
my $pat; eval '$pat = qr/$_/' or do {prompt $@; next};
push @filter, $pat;
}
close(FILE);
};
}
The error had nothing to do with the print statements, but with a missing
token several lines before. At least now I've got output, so I can waddle
along.
--
"Life in Lubbock, Texas, taught me two things: One is that God loves you
and you're going to burn in hell. The other is that sex is the most
awful, filthy thing on earth and you should save it for someone you love."
~~ Butch Hancock
On Apr 22, 5:26=A0pm, "Gerry Ford"
> "Jim Cochrane"
>
> news:slrng0r3ie.j1a.allergic-to-spam@no-spam-allowed.org...
>
> > On 2008-04-22, Gerry Ford
> > Change:
> >> }
>
> > to:
> > };
>
> > (i.e., missing a ';' - and your code could be formatted better)
>
> Thanks, Jim. =A0Missing semicolons were the culprit:
>
> #!/usr/bin/perl -w
>
> use strict;
> use warnings;
>
> my $killrc =3D "sample.killrc";
> my @filter;
> my @list1 =3D qw( Mon Tu Wed);
> =A0 =A0 open(FILE, "<$killrc") and do {
> =A0 =A0 =A0 =A0 @filter =3D ();
> =A0 =A0 =A0 =A0 foreach (
> =A0 =A0 =A0 =A0 =A0 =A0 chomp; length or next; /^#/o and next;
> =A0 =A0 =A0 =A0 =A0 =A0 my $pat; eval '$pat =3D qr/$_/' or do {prompt $@; =
next};
> =A0 =A0 =A0 =A0 =A0 =A0 push @filter, $pat;
> =A0 =A0 =A0 =A0 };
> =A0 =A0 =A0 =A0 close(FILE);
>
> };
>
> print @filter;
> print @list1;
>
> # =A0perl mats5.pl 2>text50.txt >text51.txt
> __END__
> #end script begin output
>
> (?-xism:^From:.*
ssage-ID:.*googlegroups)MonTuWed
>
> #end output show sample.killrc
> ^From:.*
> ^Subject:.*MONEY
> ^Message-ID:.*googlegroups
>
> Any ideas where the ?-xism is coming from?
those are from the compiled regex. check out the effect of qr// on a
simple regex by printing it.
On 2008-04-22, Gerry Ford
>
> "Jim Cochrane"
> news:slrng0r3ie.j1a.allergic-to-spam@no-spam-allowed.org...
>> On 2008-04-22, Gerry Ford
>
>> Change:
>>> }
>>
>> to:
>> };
>>
>> (i.e., missing a ';' - and your code could be formatted better)
> 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 (
> 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:.*
>
> #end output show sample.killrc
> ^From:.*
> ^Subject:.*MONEY
> ^Message-ID:.*googlegroups
>
> Any ideas where the ?-xism is coming from?
>
>>> Perl.exe says:
>>> syntax error at mats5.pl line 18, near "print"
>>
>> The compiler was pointing pretty much right at the problem.
> I beg to differ. I snipped this from an actual, working perl program. How
> do you know which braces need a semicolon after?
If I remember correctly, line 18 (from your error message) was either
the offending line with the close curly brace missing the ';' or the
line (print ...) below it. So you know where to look because of the
line # in the error message. Of course, now that you know that 'do {}'
blocks need a semicolon after them (right?), you know to look for that
now, too. (Of course, compile error messages will not always report a
line number that is at or very close to the actual problem in the code.)
>
> sub read_killrc {
> open(FILE, "<$killrc") and do {
> @filter = ();
> foreach (
> chomp; length or next; /^#/o and next;
> my $pat; eval '$pat = qr/$_/' or do {prompt $@; next};
> push @filter, $pat;
> }
> close(FILE);
> };
> }
>
> The error had nothing to do with the print statements, but with a missing
> token several lines before. At least now I've got output, so I can waddle
> along.
--
>>>>> "JC" == Jim Cochrane
>> do you know which braces need a semicolon after?
JC> If I remember correctly, line 18 (from your error message) was either
JC> the offending line with the close curly brace missing the ';' or the
JC> line (print ...) below it. So you know where to look because of the
JC> line # in the error message. Of course, now that you know that 'do {}'
JC> blocks need a semicolon after them (right?), you know to look for that
actually do blocks are just expressions and the statement they might be
in will need a ; (and of course in perl ; is a statement separator and
not a terminator). in this case the do block was the end of the
statement and needed a ; before the print. an if block (or other flow
control block isn't a statement so they don't need ; after them.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
Uri Guttman
>>>>>> "JC" == Jim Cochrane
>
> >> do you know which braces need a semicolon after?
>
> JC> If I remember correctly, line 18 (from your error message) was either
> JC> the offending line with the close curly brace missing the ';' or the
> JC> line (print ...) below it. So you know where to look because of the
> JC> line # in the error message. Of course, now that you know that 'do {}'
> JC> blocks need a semicolon after them (right?), you know to look for that
>
>actually do blocks are just expressions and the statement they might be
>in will need a ; (and of course in perl ; is a statement separator and
>not a terminator). in this case the do block was the end of the
>statement and needed a ; before the print. an if block (or other flow
>control block isn't a statement so they don't need ; after them.
Another reason to avoid do{} blocks that are dozens of lines long. At
the end of such a block you surely have forgotten, if it is a flow
controll block or an expression.
jue
"Jürgen Exner"
news:21mr04pro5k3sqjbebv5g2jq5i0doq42t3@4ax.com...
> "Gerry Ford"
>>Perl.exe says:
>>syntax error at mats5.pl line 18, near "print"
>>Execution of mats5.pl aborted due to compilation errors.
>
> 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.
My problem was that I thought I had a working script before I added the
print statements. At this point, I'm simply very error-prone. This is my
current version of this script, that I think addresses your criticisms:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper qw( Dumper);
my $killrc = "kill.rc";
my @filter;
open (FILE, "<", $killrc) or die ("Cannot open $killrc: $!");
@filter = ();
foreach (
chomp;
next unless length;
next if /^#/o;
my $pat; eval ('$pat = qr/$_/') or do {prompt $@; next};
push @filter, $pat;
close(FILE);
}
print @filter;
#print STDERR "The list is " . Dumper( @list ) . "\n";
__END__
How would you indent this?
> However, I wonder what you are trying to achive by using eval in the
> first place. I don't see any need for it.
I could just omit it?
--
"Life in Lubbock, Texas, taught me two things: One is that God loves you
and you're going to burn in hell. The other is that sex is the most
awful, filthy thing on earth and you should save it for someone you love."
~~ Butch Hancock
"Jim Cochrane"
news:slrng0t2vg.sbv.allergic-to-spam@no-spam-allowed.org...
>> I beg to differ. I snipped this from an actual, working perl program.
>> How
>> do you know which braces need a semicolon after?
>
> If I remember correctly, line 18 (from your error message) was either
> the offending line with the close curly brace missing the ';' or the
> line (print ...) below it. So you know where to look because of the
> line # in the error message. Of course, now that you know that 'do {}'
> blocks need a semicolon after them (right?), you know to look for that
> now, too. (Of course, compile error messages will not always report a
> line number that is at or very close to the actual problem in the code.)
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper qw( Dumper);
my $killrc = "kill.rc";
my @filter;
open (FILE, "<", $killrc) or die ("Cannot open $killrc: $!");
@filter = ();
foreach (
chomp;
next unless length;
next if /^#/o;
my $pat; eval ('$pat = qr/$_/') or do {prompt $@; next};
push @filter, $pat;
close(FILE);
}
print @filter;
#print STDERR "The list is " . Dumper( @list ) . "\n";
__END__
I'm still confused on the whole }; thing. With this version, I've
eliminated the the do{}; If I put a semicolon after the foreach close curly
brace, I get no change in behavior in the program. ??
--
"Life in Lubbock, Texas, taught me two things: One is that God loves you
and you're going to burn in hell. The other is that sex is the most
awful, filthy thing on earth and you should save it for someone you love."
~~ Butch Hancock
"Tad J McClellan"
news:slrng0rkck.h5g.tadmc@tadmc30.sbcglobal.net...
> Gerry Ford
>
>> How does a person invoke
>> the debugger?
>
>
> perldoc -q debug
>
> How do I debug my Perl programs?
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper qw( Dumper);
my $killrc = "kill.rc";
my @filter;
open (FILE, "<", $killrc) or die ("Cannot open $killrc: $!");
@filter = ();
foreach (
chomp;
next unless length;
next if /^#/o;
my $pat; eval ('$pat = qr/$_/') or do {prompt $@; next};
push @filter, $pat;
close(FILE);
};
print @filter;
#print STDERR "The list is " . Dumper( @list ) . "\n";
__END__
In perldoc -q debug, they commend the use of Data::Dumper. I mimicked the
syntax in the but perlexe thinks I need to have an explicit package instead.
What gives?
--
"Life in Lubbock, Texas, taught me two things: One is that God loves you
and you're going to burn in hell. The other is that sex is the most
awful, filthy thing on earth and you should save it for someone you love."
~~ Butch Hancock
"Uri Guttman"
news:x7bq42v97j.fsf@mail.sysarch.com...
>>>>>> "GF" == Gerry Ford
>
[snipped and re-ordered]
> my return calls IMPLY A SUB is surrounding it. of course it won't work
> as shown. jeez, i can't hold your hand and also blow your nose. think
> about the code and don't just copy/run it.
You wouldn't want to be anywhere near my infirmities today. Stomach flu.
#!/usr/bin/perl
use strict;
use warnings;
my $killrc = "sample.killrc";
sub read_killrc {
return unless open(my $file, "<$killrc") ;
return map {
/^([^#]+)#?$/ &&
eval{ qr/$1/} || prompt $@, ()
} <$file> ;
}
read_killrc;
__END__
So, how do I adapt this now to take advantage of the return values of map?
I no longer have the option of writing
print @filter;
?
> GF> # perl mats5.pl 2>text50.txt >text51.txt
>
> why the redirect of stderr? there is no stderr output. and what is in
> the prompt sub?
# perl mats7.pl 2>text50.txt >text51.txt
This statement is what I call "the goocher." It keeps track of what version
I'm running and has a means to redirect stdout and stderr. I should
capitalize those. I copy and paste these into the command line.
What kind of name is Uri?
--
"Life in Lubbock, Texas, taught me two things: One is that God loves you
and you're going to burn in hell. The other is that sex is the most
awful, filthy thing on earth and you should save it for someone you love."
~~ Butch Hancock
Gerry Ford wrote:
> "Jürgen Exner"
> news:21mr04pro5k3sqjbebv5g2jq5i0doq42t3@4ax.com...
>> However, I wonder what you are trying to achive by using eval in the
>> first place. I don't see any need for it.
>
> I could just omit it?
It's up to you; I for one do see a need for it: It prevents a fatal
error for the case a line is not a valid regular expression.
C:\home>type test.pl
my @filter;
while () {
chomp;
next unless length;
next if /^#/o;
my $pat;
eval '$pat = qr($_)' or do { warn $@; next };
push @filter, $pat;
}
print "$_\n" foreach @filter;
__DATA__
\w+).+?
\s+
C:\home>test.pl
Unmatched ) in regex; marked by <-- HERE in m/\w+) <-- HERE .+?/ at
(eval 1) line 1, line 1.
(?-xism:\s+)
C:\home>
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
"Gerry Ford"
>"Jürgen Exner"
>> 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.
>
>My problem was that I thought I had a working script before I added the
>print statements. At this point, I'm simply very error-prone.
Then it is even more important to follow standard procedures for your
own and others sake:
1: what is the expected behaviour?
2: what is the observed behaviour?
3: what code is producing this behaviour?
4: what data is producing this behaviour?
Those 4 elements are vital for any reasonable investigation.
You supplied 3, were vague about 1, and didn't tell anything about 2 or
4, so people had to use crystal balls and tea leaves to guess. And of
course they guessed wrong.
> This is my
>current version of this script, that I think addresses your criticisms:
[most of the code snipped]
You are still omitting a precise description of what you expect(1), what
you observe (2), and data to run your sample code (4).
>my $pat; eval ('$pat = qr/$_/') or do {prompt $@; next};
You still got an 'interesting' do{} sitting around.
>How would you indent this?
I would open the file in my favourite editor and run a "indent-region"
command.
>> However, I wonder what you are trying to achive by using eval in the
>> first place. I don't see any need for it.
>I could just omit it?
Think! What is it that you are trying to achieve by using the eval()?
You didn't tell us. So how can we answer your question? If you don't
tell us then we cannot know what you are trying to do with that eval and
therefore cannot tell if the eval serves any purpose to achieve that
goal or not.
jue
"Gerry Ford"
[...]
>my $pat; eval ('$pat = qr/$_/') or do {prompt $@; next};
[...]
>I'm still confused on the whole }; thing.
There is no }; thing. Understanding this is your problem.
There are 'blocks' that are enclosed in curly braces {.....}. Period.
And then there are statements. Individual statements are separated by a
; in Perl, i.e. between any two statements you have to write a ;.
In your original progam you had [reformatted to make it more obvious]
open(FILE, "<$killrc") and
do {SomeVeryLongWhatever} print @filter;
What you meant was
open(FILE, "<$killrc") and
do {SomeVeryLongWhatever};
print @filter;
If you want print() to be a new statement, then you have to place a
semicolon between the preceeding statement and the print(). If you don't
then you will get that syntax error because perl had to assume that you
are continuing the preceeding statement. After all, it could have been
that you wanted to add 5 to the result of do, something like
do {SomeVeryLongWhatever} + 5;
which is perfectly legal. The only way for perl to know that a new
statement is starting is if you put that semicolon there.
>With this version, I've eliminated the the do{};
No, you haven't. See the one line of code I quoted above.
> If I put a semicolon after the foreach close curly
>brace, I get no change in behavior in the program. ??
That's just a surplus empty statement. Perl doesn't care if you create a
dozen or a million.
jue
>>>>> "GF" == Gerry Ford
GF> "Uri Guttman"
GF> news:x7bq42v97j.fsf@mail.sysarch.com...
>>>>>>> "GF" == Gerry Ford
>>
GF> my $killrc = "sample.killrc";
GF> sub read_killrc {
GF> return unless open(my $file, "<$killrc") ;
GF> return map {
GF> /^([^#]+)#?$/ &&
GF> eval{ qr/$1/} || prompt $@, ()
GF> } <$file> ;
GF> }
some formatting/indenting will help there.
GF> read_killrc;
great! you just tossed away the results of the call.
GF> So, how do I adapt this now to take advantage of the return values of map?
GF> I no longer have the option of writing
GF> print @filter;
do you think print only takes an array? what does the above sub return?
A LIST generated by the map. so print it! you can print ANY expression
or list of expressions.
print read_killrc() ;
was that so hard? even with the flu you should have gotten that!
GF> # perl mats5.pl 2>text50.txt >text51.txt
>> why the redirect of stderr? there is no stderr output. and what is in
>> the prompt sub?
GF> # perl mats7.pl 2>text50.txt >text51.txt
GF> This statement is what I call "the goocher." It keeps track of what version
GF> I'm running and has a means to redirect stdout and stderr. I should
GF> capitalize those. I copy and paste these into the command line.
that still makes no sense. the script (or at least the code you have
shown) doesn't print to STDERR.
GF> What kind of name is Uri?
my kind.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
>>>>>> "GF" == Gerry Ford
I find this constant changing of posting ids pretty annoying. Please
decide if you are Wade Ward or zaxfux or whatever it is and stick with it.
In any case, you have been re-added to my killfile under this new identity
and I cannot believe I have been fooled into responding to your question.
> GF> What kind of name is Uri?
What is the point of picking on other people's names when you can't even
decide what your own name is?
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
On 2008-04-23, Uri Guttman
>>>>>> "JC" == Jim Cochrane
>
> >> do you know which braces need a semicolon after?
>
> JC> If I remember correctly, line 18 (from your error message) was either
> JC> the offending line with the close curly brace missing the ';' or the
> JC> line (print ...) below it. So you know where to look because of the
> JC> line # in the error message. Of course, now that you know that 'do {}'
> JC> blocks need a semicolon after them (right?), you know to look for that
>
> actually do blocks are just expressions and the statement they might be
> in will need a ; (and of course in perl ; is a statement separator and
> not a terminator). in this case the do block was the end of the
> statement and needed a ; before the print. an if block (or other flow
> control block isn't a statement so they don't need ; after them.
>
> uri
>
Thanks for the clarification.
--
On 2008-04-24, Jürgen Exner
> "Gerry Ford"
> [...]
>>my $pat; eval ('$pat = qr/$_/') or do {prompt $@; next};
> [...]
>
>>I'm still confused on the whole }; thing.
>
> [good explanation deleted]
Also, see Uri Guttman's response to my post for further elaboration
(and correction to my somewhat misleading statement that you need a
semicolon after a do {} block).
--
>>>>> "ASU" == A Sinan Unur <1usa@llenroc.ude.invalid> writes:
ASU> I find this constant changing of posting ids pretty
ASU> annoying. Please decide if you are Wade Ward or zaxfux or
ASU> whatever it is and stick with it. In any case, you have been
ASU> re-added to my killfile under this new identity and I cannot
ASU> believe I have been fooled into responding to your question.
Pay attention to the posting style - it's consistent.
(I suspect his inability to write or program coherently and his
inability to stick to the same posting ID are both symptoms of a
deeper malaise.)
Charlton
--
Charlton Wilbur
cwilbur@chromatico.net