[32806] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4070 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Nov 5 16:09:35 2013

Date: Tue, 5 Nov 2013 13:09:09 -0800 (PST)
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, 5 Nov 2013     Volume: 11 Number: 4070

Today's topics:
    Re: Can this be combined into one statement? <rweikusat@mobileactivedefense.com>
    Re: Can this be combined into one statement? <cwilbur@chromatico.net>
    Re: Can this be combined into one statement? <derykus@gmail.com>
    Re: Can this be combined into one statement? <gamo@telecable.es>
    Re: Can this be combined into one statement? <gamo@telecable.es>
    Re: Can this be combined into one statement? <rweikusat@mobileactivedefense.com>
    Re: Can this be combined into one statement? <gamo@telecable.es>
    Re: Can this be combined into one statement? <rweikusat@mobileactivedefense.com>
    Re: Can this be combined into one statement? <gamo@telecable.es>
    Re: PDF, Excel, LaTeX, and possibly R and sweave <cartercc@gmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Mon, 04 Nov 2013 12:14:49 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Can this be combined into one statement?
Message-Id: <874n7ss6rq.fsf@sable.mobileactivedefense.com>

"C.DeRykus" <derykus@gmail.com> writes:
> On Friday, November 1, 2013 2:34:10 PM UTC-7, Rainer Weikusat wrote:

[...]

>> 	  rere => sub {
>> 	     reverse($_) =~ /^(\S+)/ and $x = reverse($1) for @lines;
>> 	 }});
>
>
> 'rere'  proves going 'reverse' can be a great idea.
>
> (even does well as MAX_LENGTH grows... of course
> split wins if you get crazy big)

It is mostly a demonstration that at least some matches anchored at the
end of the string are theoretically trivial to optimize -- revert the
pattern and match backwards from the end.


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

Date: Mon, 04 Nov 2013 13:27:00 -0500
From: Charlton Wilbur <cwilbur@chromatico.net>
Subject: Re: Can this be combined into one statement?
Message-Id: <87bo206n0r.fsf@new.chromatico.net>

>>>>> "RW" == Rainer Weikusat <rweikusat@mobileactivedefense.com> writes:

[wrapping a function in parentheses to induce list context]

    RW> this will look very much like a spurious pair of parentheses to
    RW> someone who is not familiar with the split
    RW> documentation. 

[indexing a list with -1]

    RW> All of this combined doesn't exactly strike me as a particularly
    RW> clear way to express 'get the last word from a string'. 

My audience, when am writing Perl, is generally people who are competent
in Perl.  If I insisted on avoiding constructions that depended on
context or on Perl's builtin data structures, I might as well be writing
in Java.  

At a former place of employment, writing in C, I wrote the comment 

   /* breadth-first search rather than depth-first search because
      average-case performance is the same but worst-case performance is
      far better */

and this was called out as needing more explanation by a (thoroughly
incompetent) manager who had never encountered the terms "breadth-first"
and "depth-first."[1] I retorted "A programmer who has never encountered
the concept 'breadth-first' or 'depth-first' and who can't be bothered
to look them up and learn has no business modifying this code."

    RW> Imagine these were words written on paper and you were asked to
    RW> cut the last word off the sentence with a pair of scissors. The
    RW> idea to start cutting the whole strip of paper into pieces and
    RW> drop them all on the floor except the last one could be made
    RW> into a nice sketch (skit?) if the piece of paper was long enough
    RW> and most people watching this would consider the person doing it
    RW> less-than-intellectually-gifted

Imagine that you put a bunch of words on index cards and told a person
to sort them.  He would not bother with mergesort or quicksort or bubble
sort, but would use insertion sort; and if you explained that quicksort
would sort the cards in-place while mergesort had better average-case
performance, he would look at you as less-than-intellectually-gifted.
While he may be justified in this, the simple fact is that physical
solutions have different constraints than digital solutions, and
attempting to construct an argument by analogy where you pretend that
the digital solution has the same constraints as the physical solution
is, frankly, moronic.

Charlton


[1] His degree was in music, which explains his ignorance but not his
incompetence; my degree is also in music.

-- 
Charlton Wilbur
cwilbur@chromatico.net


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

Date: Mon, 4 Nov 2013 13:52:47 -0800 (PST)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: Can this be combined into one statement?
Message-Id: <9b72e7cd-09cf-46bb-a285-69c9d5cdc7c4@googlegroups.com>

On Monday, November 4, 2013 4:14:49 AM UTC-8, Rainer Weikusat wrote:
> "C.DeRykus" <derykus@gmail.com> writes:
> 
> > On Friday, November 1, 2013 2:34:10 PM UTC-7, Rainer Weikusat wrote:
> ...
> >> 	  rere => sub {
> 
> >> 	     reverse($_) =~ /^(\S+)/ and $x = reverse($1) for @lines;
> 
> >> 	 }});
> > 'rere'  proves going 'reverse' can be a great idea.
> 
> > (even does well as MAX_LENGTH grows... of course
> > split wins if you get crazy big)
> 
> It is mostly a demonstration that at least some matches anchored at the
> end of the string are theoretically trivial to optimize -- revert the
> pattern and match backwards from the end.

Yes, definitely a potential speedup although the more familiar regex anchored at the end is likely the most intuitive and popular solution.  

[A very long time ago, there was a discussion of a nice-to-have regex modifier that'd search backwards 
and handle the messy reversals automatically. IMO that would be potentially faster and an intuitive win too ]

-- 
Charles DeRykus   


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

Date: Tue, 05 Nov 2013 00:08:15 +0100
From: gamo <gamo@telecable.es>
Subject: Re: Can this be combined into one statement?
Message-Id: <l599cv$j0b$2@speranza.aioe.org>

El 01/11/13 22:34, Rainer Weikusat escribió:
> use Benchmark qw(cmpthese);
>
> use constant MAX_WORDS =>	15;
> use constant MAX_LEN =>		20;
> use constant LINES =>		100;
>
> my (@lines, $line, $n, $nw, $x);
>
> srand(0xdeafbabe);			# repeatable
>
> $n = LINES;
> do {
>      $nw = int(rand(MAX_WORDS - 2)) + 2;
>      $line = '';
>      do {
> 	$line .= 'x' x (int(rand(MAX_LEN - 1)) + 1);
> 	$line .= ' ' x (rand(5) + 1) if $nw > 1;
>      } while --$nw;
>
>      push(@lines, $line);
> #    print STDERR ("'$line'\n");
> } while --$n;
>
> cmpthese(-3,
> 	 {
> 	  split => sub {
> 	      $x = (split(/\s+/, $_))[-1] for @lines;
> 	 },
>
> 	  renb => sub {
> 	      /.*\s(\S+)$/ and $x = $1 for @lines;
> 	  },
> 	
> 	  rere => sub {
> 	     reverse($_) =~ /^(\S+)/ and $x = reverse($1) for @lines;
	},
	   gamo => sub {
		s/\s+$//;
		$x = substr($_, rindex($_, " ") );
> 	 }});
>

If this is correct, consider it.

Best regards




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

Date: Tue, 05 Nov 2013 06:54:05 +0100
From: gamo <gamo@telecable.es>
Subject: Re: Can this be combined into one statement?
Message-Id: <l5a15t$15j$1@speranza.aioe.org>

El 05/11/13 00:08, gamo escribió:
> El 01/11/13 22:34, Rainer Weikusat escribió:
>> cmpthese(-3,
>>      {
>>       split => sub {
>>           $x = (split(/\s+/, $_))[-1] for @lines;
>>      },
>>
>>       renb => sub {
>>           /.*\s(\S+)$/ and $x = $1 for @lines;
>>       },
>>
>>       rere => sub {
>>          reverse($_) =~ /^(\S+)/ and $x = reverse($1) for @lines;
>      },
>         gamo => sub {
> ###         s/\s+$//;
> ###         $x = substr($_, rindex($_, " ") );

		$x = substr($_,1+rindex($_," ")) for @lines;

>>      }});
>>
>
> If this is correct, consider it.
>

Of course it's not. It's slower, but if no \s is allowed at end of 
lines, it could be faster. My apologies.

> Best regards
>
>



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

Date: Tue, 05 Nov 2013 11:39:36 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Can this be combined into one statement?
Message-Id: <8761s79ix3.fsf@sable.mobileactivedefense.com>

gamo <gamo@telecable.es> writes:
> El 05/11/13 00:08, gamo escribió:
>> El 01/11/13 22:34, Rainer Weikusat escribió:
>>> cmpthese(-3,
>>>      {
>>>       split => sub {
>>>           $x = (split(/\s+/, $_))[-1] for @lines;
>>>      },
>>>
>>>       renb => sub {
>>>           /.*\s(\S+)$/ and $x = $1 for @lines;
>>>       },
>>>
>>>       rere => sub {
>>>          reverse($_) =~ /^(\S+)/ and $x = reverse($1) for @lines;
>>      },
>>         gamo => sub {
>> ###         s/\s+$//;
>> ###         $x = substr($_, rindex($_, " ") );
>
> 		$x = substr($_,1+rindex($_," ")) for @lines;
>
>>>      }});
>>>
>>
>> If this is correct, consider it.
>>
>
> Of course it's not. It's slower, but if no \s is allowed at end of
> lines, it could be faster. My apologies.

The example code doesn't produce lines with spaces at the end. If it
did, the s/// would be a dubious idea because it would change the input
data. Apart from that, using rindex here is a good idea speedwise
provided that words are really separated by spaces and not by
'whitespace characters'. In an ASCII/ISO8859-1 environment, that would
be [ \t\v] (written as regex character class). I have no idea what
exactly a vertical tab (\v) is supposed to be and until recently, Perl
didn't support it, anyway, but horizontal tabs (\t) are not that
uncommon.



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

Date: Tue, 05 Nov 2013 14:11:24 +0100
From: gamo <gamo@telecable.es>
Subject: Re: Can this be combined into one statement?
Message-Id: <l5aqpr$v03$1@speranza.aioe.org>

El 05/11/13 12:39, Rainer Weikusat escribió:
> gamo <gamo@telecable.es> writes:
>> El 05/11/13 00:08, gamo escribió:
>>> El 01/11/13 22:34, Rainer Weikusat escribió:
>>>> cmpthese(-3,
>>>>       {
>>>>        split => sub {
>>>>            $x = (split(/\s+/, $_))[-1] for @lines;
>>>>       },
>>>>
>>>>        renb => sub {
>>>>            /.*\s(\S+)$/ and $x = $1 for @lines;
>>>>        },
>>>>
>>>>        rere => sub {
>>>>           reverse($_) =~ /^(\S+)/ and $x = reverse($1) for @lines;
>>>       },
>>>          gamo => sub {
>>> ###         s/\s+$//;
>>> ###         $x = substr($_, rindex($_, " ") );
>>
>> 		$x = substr($_,1+rindex($_," ")) for @lines;
>>
>>>>       }});
>>>>
>>>
>>> If this is correct, consider it.
>>>
>>
>> Of course it's not. It's slower, but if no \s is allowed at end of
>> lines, it could be faster. My apologies.
>
> The example code doesn't produce lines with spaces at the end. If it
> did, the s/// would be a dubious idea because it would change the input
> data. Apart from that, using rindex here is a good idea speedwise
> provided that words are really separated by spaces and not by
> 'whitespace characters'. In an ASCII/ISO8859-1 environment, that would
> be [ \t\v] (written as regex character class). I have no idea what
> exactly a vertical tab (\v) is supposed to be and until recently, Perl
> didn't support it, anyway, but horizontal tabs (\t) are not that
> uncommon.
>

So, I win :-)

jesus@casa:~/src$ perl cmp.pl
          Rate split  renb  rere  gamo
split 13395/s    --   -6%  -28%  -80%
renb  14306/s    7%    --  -23%  -79%
rere  18620/s   39%   30%    --  -72%
gamo  66776/s  399%  367%  259%    --

Well, no big deal. I could make a program 15 times _slower_ using threads.




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

Date: Tue, 05 Nov 2013 16:11:23 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Can this be combined into one statement?
Message-Id: <87fvrakevo.fsf@sable.mobileactivedefense.com>

gamo <gamo@telecable.es> writes:
> El 05/11/13 12:39, Rainer Weikusat escribió:
>> gamo <gamo@telecable.es> writes:
>>> El 05/11/13 00:08, gamo escribió:
>>>> El 01/11/13 22:34, Rainer Weikusat escribió:
>>>>> cmpthese(-3,
>>>>>       {
>>>>>        split => sub {
>>>>>            $x = (split(/\s+/, $_))[-1] for @lines;
>>>>>       },
>>>>>
>>>>>        renb => sub {
>>>>>            /.*\s(\S+)$/ and $x = $1 for @lines;
>>>>>        },
>>>>>
>>>>>        rere => sub {
>>>>>           reverse($_) =~ /^(\S+)/ and $x = reverse($1) for @lines;
>>>>       },
>>>>          gamo => sub {
>>>> ###         s/\s+$//;
>>>> ###         $x = substr($_, rindex($_, " ") );
>>>
>>> 		$x = substr($_,1+rindex($_," ")) for @lines;
>>>
>>>>>       }});
>>>>>
>>>>
>>>> If this is correct, consider it.
>>>>
>>>
>>> Of course it's not. It's slower, but if no \s is allowed at end of
>>> lines, it could be faster. My apologies.
>>
>> The example code doesn't produce lines with spaces at the end. If it
>> did, the s/// would be a dubious idea because it would change the input
>> data. Apart from that, using rindex here is a good idea speedwise
>> provided that words are really separated by spaces and not by
>> 'whitespace characters'. In an ASCII/ISO8859-1 environment, that would
>> be [ \t\v] (written as regex character class). I have no idea what
>> exactly a vertical tab (\v) is supposed to be and until recently, Perl
>> didn't support it, anyway, but horizontal tabs (\t) are not that
>> uncommon.
>>
>
> So, I win :-)

rindex doesn't handle 'whitespace', only the exact string passed to
it. Taking this into account, if you're posting was not supposed to
present another useful idea but to communicated something about you, it
would be "you cheat".

HTH.


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

Date: Tue, 05 Nov 2013 21:10:23 +0100
From: gamo <gamo@telecable.es>
Subject: Re: Can this be combined into one statement?
Message-Id: <l5bjbc$78e$1@speranza.aioe.org>

El 05/11/13 17:11, Rainer Weikusat escribió:
> gamo <gamo@telecable.es> writes:

>> So, I win :-)
>
> rindex doesn't handle 'whitespace', only the exact string passed to
> it. Taking this into account, if you're posting was not supposed to
> present another useful idea but to communicated something about you, it
> would be "you cheat".
>
> HTH.
>

I swear that I want to cheat in puzzles as many times as I could. The
only problem is that there are problems in which I don't manage how to
do it. Would you mind to read again the MCE post and help me to cheat
in it? I could post a real problem, with a linear heuristic solution.

Thanks ;-)



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

Date: Mon, 4 Nov 2013 07:47:41 -0800 (PST)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: PDF, Excel, LaTeX, and possibly R and sweave
Message-Id: <e4075f5d-1b31-47d0-88f9-c4510fb87ae6@googlegroups.com>

On Sunday, November 3, 2013 3:29:32 PM UTC-5, Mart van de Wege wrote:
> At my previous employer I used to autogenerate reports for our customers
> using LaTeX::Driver with great success.

Mart,

Thanks very much for your input. Part of my problem is that I haven't figur=
ed out what I'm supposed to be doing. The largest part of my job is to take=
 input (almost always as CSV files) and munge them into some kind of output=
 (mostly these days as Excel, CSV, or PDF). I /think/ that I will be produc=
ing a report with a mixture of file formats, including PDF and Excel, with =
dynamically generated graphical elements -- I'm not sure but it looks like =
it's headed this way.

I'm very comfortable generating Excel files and PDF files (with PDF::API2).=
 I also like using R and LaTex. I've been casting a covetous eye on Sweave =
but haven't actually used it. And of course, I use Perl for the heavy lifin=
g.

> I also used to generate the graphs for those reports using
> SVG::TT::Graph. Not because SVG was easy to bind into LaTeX (that sucked
> actually), but because SVG::TT::Graph had a nice API to generate graphs
> IMO. I used the ImageMagick bindings to convert the output into
> something that LaTeX would like, and Template Toolkit to fill in the
> LaTeX documents and generate the links to the graphs.

I like using SVG when I can. Normally, when using LaTeX, I target my output=
 images to EPS. EPS works well, but I would really like to try SVG. Unfortu=
nately, R is limited in the kinds of graphical output formats it has, and I=
 will definitely NOT be using JPEG, GIF, BMP, or PNG!

> So, use your favourite graphing module to create your graphics, use
> TT LaTeX templates to build your documents, and run the output of the
> processed templates through LaTeX::Driver, that's the way I would do it.

I will give this a great deal of thought. I can auto generate graphics, and=
 I can certainly embed them in a PDF module, but I think that will be a klu=
dge. I like the idea of Sweave because I like the idea of embedding R code =
in LaTeX, and I really like LaTex. I just don't know how this will work wit=
h Perl, and the bottom line is that Perl is essential for the data munging =
that underlies all the output stuff.

CC.



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

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:

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

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#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 4070
***************************************


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