[30225] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 1468 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Apr 22 17:18:08 2008

Date: Tue, 22 Apr 2008 14:17:37 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Tue, 22 Apr 2008     Volume: 11 Number: 1468

Today's topics:
        The map function <sonet.all@gmail.com>
    Re: The map function <benkasminbullock@gmail.com>
    Re: The map function <jurgenex@hotmail.com>
    Re: The map function <gerry@nowhere.ford>
    Re: The map function <abigail@abigail.be>
    Re: The map function <tadmc@seesig.invalid>
    Re: The map function <simon.chao@gmail.com>
    Re: The map function <gerry@nowhere.ford>
    Re: The map function <uri@stemsystems.com>
    Re: The map function <benkasminbullock@gmail.com>
    Re: The map function <cwilbur@chromatico.net>
    Re: The map function <jurgenex@hotmail.com>
    Re: The map function <uri@stemsystems.com>
    Re: The map function <gerry@nowhere.ford>
    Re: The map function <uri@stemsystems.com>
    Re: The map function <jurgenex@hotmail.com>
    Re: The map function <uri@stemsystems.com>
    Re: The map function <gerry@nowhere.ford>
    Re: The map function <allergic-to-spam@no-spam-allowed.org>
    Re: The map function <jurgenex@hotmail.com>
    Re: The map function <uri@stemsystems.com>
    Re: The map function <someone@example.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Sat, 19 Apr 2008 11:30:05 +0800
From: "mynews" <sonet.all@gmail.com>
Subject: The map function
Message-Id: <fubp0g$aa0$1@netnews.hinet.net>

Whis is difference between map and for-each?
Why the map function is fast than for-each? <== is it for all case? 




------------------------------

Date: Sat, 19 Apr 2008 03:42:31 +0000 (UTC)
From: Ben Bullock <benkasminbullock@gmail.com>
Subject: Re: The map function
Message-Id: <fubpn7$r73$1@ml.accsnet.ne.jp>

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?


------------------------------

Date: Sat, 19 Apr 2008 03:49:07 GMT
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: The map function
Message-Id: <b7qi04hcsmlovev452suvispr820t1u9n0@4ax.com>

"mynews" <sonet.all@gmail.com> wrote:
>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


------------------------------

Date: Sat, 19 Apr 2008 16:40:02 -0500
From: "Gerry Ford" <gerry@nowhere.ford>
Subject: Re: The map function
Message-Id: <1208640762_1353@news.newsgroups.com>


"Jürgen Exner" <jurgenex@hotmail.com> wrote in message 
news:b7qi04hcsmlovev452suvispr820t1u9n0@4ax.com...
> "mynews" <sonet.all@gmail.com> wrote:
>>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 




------------------------------

Date: 19 Apr 2008 22:10:27 GMT
From: Abigail <abigail@abigail.be>
Subject: Re: The map function
Message-Id: <slrng0krei.veg.abigail@alexandra.abigail.be>

                                      _
mynews (sonet.all@gmail.com) wrote on VCCCXLV September MCMXCIII in
<URL:news:fubp0g$aa0$1@netnews.hinet.net>:
@@  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 ${$@}[$#{@${@}}]'


------------------------------

Date: Sun, 20 Apr 2008 08:33:05 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: The map function
Message-Id: <slrng0mhgh.ujm.tadmc@tadmc30.sbcglobal.net>

Gerry Ford <gerry@nowhere.ford> wrote:
>
> "Jürgen Exner" <jurgenex@hotmail.com> wrote in message 
> news:b7qi04hcsmlovev452suvispr820t1u9n0@4ax.com...
>> "mynews" <sonet.all@gmail.com> wrote:
>>>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/"


------------------------------

Date: Sun, 20 Apr 2008 15:30:06 -0700 (PDT)
From: nolo contendere <simon.chao@gmail.com>
Subject: Re: The map function
Message-Id: <7b0b58dd-9a37-465c-9827-715a210236aa@8g2000hse.googlegroups.com>

On Apr 18, 11:49=A0pm, J=FCrgen Exner <jurge...@hotmail.com> wrote:
> "mynews" <sonet....@gmail.com> wrote:
> >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.


------------------------------

Date: Mon, 21 Apr 2008 00:25:19 -0500
From: "Gerry Ford" <gerry@nowhere.ford>
Subject: Re: The map function
Message-Id: <1208755083_3027@news.newsgroups.com>


"Tad J McClellan" <tadmc@seesig.invalid> wrote in message 
news:slrng0mhgh.ujm.tadmc@tadmc30.sbcglobal.net...
> Gerry Ford <gerry@nowhere.ford> wrote:
>>

>> 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 (<FILE>) {
            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 




------------------------------

Date: Mon, 21 Apr 2008 05:59:13 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: The map function
Message-Id: <x7fxtfeojz.fsf@mail.sysarch.com>

>>>>> "GF" == Gerry Ford <gerry@nowhere.ford> writes:

  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 (<FILE>) {
  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 ---------


------------------------------

Date: Mon, 21 Apr 2008 10:08:42 +0000 (UTC)
From: Ben Bullock <benkasminbullock@gmail.com>
Subject: Re: The map function
Message-Id: <fuhp3a$at6$1@ml.accsnet.ne.jp>

On Sun, 20 Apr 2008 15:30:06 -0700, nolo contendere wrote:

> On Apr 18, 11:49 pm, Jürgen Exner <jurge...@hotmail.com> wrote:

>> 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&


------------------------------

Date: Mon, 21 Apr 2008 11:13:11 -0400
From: Charlton Wilbur <cwilbur@chromatico.net>
Subject: Re: The map function
Message-Id: <86lk37i6m0.fsf@mithril.chromatico.net>

>>>>> "UG" == Uri Guttman <uri@stemsystems.com> writes:

    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


------------------------------

Date: Mon, 21 Apr 2008 16:56:41 GMT
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: The map function
Message-Id: <q9hp04dsato21u3blk8hpo3oofc1lofhfd@4ax.com>

Uri Guttman <uri@stemsystems.com> wrote:
>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


------------------------------

Date: Mon, 21 Apr 2008 17:46:09 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: The map function
Message-Id: <x7k5ir9k4e.fsf@mail.sysarch.com>

>>>>> "JE" == Jürgen Exner <jurgenex@hotmail.com> writes:

  JE> Uri Guttman <uri@stemsystems.com> wrote:
  >> 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 ---------


------------------------------

Date: Mon, 21 Apr 2008 20:47:51 -0500
From: "Gerry Ford" <gerry@nowhere.ford>
Subject: Re: The map function
Message-Id: <1208828437_3041@news.newsgroups.com>


"Uri Guttman" <uri@stemsystems.com> wrote in message 
news:x7fxtfeojz.fsf@mail.sysarch.com...
>>>>>> "GF" == Gerry Ford <gerry@nowhere.ford> writes:
>
>  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 (<FILE>) {
>  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 (<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__

#!/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 




------------------------------

Date: Tue, 22 Apr 2008 03:50:57 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: The map function
Message-Id: <x7bq42v97j.fsf@mail.sysarch.com>

>>>>> "GF" == Gerry Ford <gerry@nowhere.ford> writes:

  GF> "Uri Guttman" <uri@stemsystems.com> wrote in message 

  >> 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 (<FILE>) {
  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 ---------


------------------------------

Date: Tue, 22 Apr 2008 04:32:52 GMT
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: The map function
Message-Id: <4cqq045hepcr8jj6llnrfk8pr7r9rume5i@4ax.com>

"Gerry Ford" <gerry@nowhere.ford> wrote:

[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;


------------------------------

Date: Tue, 22 Apr 2008 04:40:29 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: The map function
Message-Id: <x78wz6tscj.fsf@mail.sysarch.com>

>>>>> "JE" == Jürgen Exner <jurgenex@hotmail.com> writes:

  JE> "Gerry Ford" <gerry@nowhere.ford> wrote:
  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 ---------


------------------------------

Date: Mon, 21 Apr 2008 23:56:17 -0500
From: "Gerry Ford" <gerry@nowhere.ford>
Subject: Re: The map function
Message-Id: <1208839744_3044@news.newsgroups.com>


"Uri Guttman" <uri@stemsystems.com> wrote in message 
news:x78wz6tscj.fsf@mail.sysarch.com...
>>>>>> "JE" == Jürgen Exner <jurgenex@hotmail.com> writes:
>
>  JE> "Gerry Ford" <gerry@nowhere.ford> wrote:
>  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 (<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__

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 




------------------------------

Date: Tue, 22 Apr 2008 09:05:50 +0200 (CEST)
From: Jim Cochrane <allergic-to-spam@no-spam-allowed.org>
Subject: Re: The map function
Message-Id: <slrng0r3ie.j1a.allergic-to-spam@no-spam-allowed.org>

On 2008-04-22, Gerry Ford <gerry@nowhere.ford> wrote:
>
> "Uri Guttman" <uri@stemsystems.com> wrote in message 
> news:x78wz6tscj.fsf@mail.sysarch.com...
>>>>>>> "JE" == Jürgen Exner <jurgenex@hotmail.com> writes:
>>
>>  JE> "Gerry Ford" <gerry@nowhere.ford> wrote:
>>  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 (<FILE>) {
>             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.


-- 



------------------------------

Date: Tue, 22 Apr 2008 12:42:52 GMT
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: The map function
Message-Id: <21mr04pro5k3sqjbebv5g2jq5i0doq42t3@4ax.com>

"Gerry Ford" <gerry@nowhere.ford> wrote:
[Partially rearragned for better reading flow]
>"Uri Guttman" <uri@stemsystems.com> wrote in message 
>>>>>>> "JE" == Jürgen Exner <jurgenex@hotmail.com> writes:
>>  JE> "Gerry Ford" <gerry@nowhere.ford> wrote:
>>  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 (<FILE>) {
>            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


------------------------------

Date: Tue, 22 Apr 2008 18:41:56 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: The map function
Message-Id: <x77ieplojw.fsf@mail.sysarch.com>

>>>>> "JE" == Jürgen Exner <jurgenex@hotmail.com> writes:


  >> 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 ---------


------------------------------

Date: Tue, 22 Apr 2008 18:57:41 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: The map function
Message-Id: <FyqPj.1277$PM5.173@edtnps92>

J=FCrgen Exner wrote:
> "Gerry Ford" <gerry@nowhere.ford> wrote:
>>=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 (<FILE>) {
>>            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


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 1468
***************************************


home help back first fref pref prev next nref lref last post