[29186] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 430 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat May 12 09:09:59 2007

Date: Sat, 12 May 2007 06:09:06 -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           Sat, 12 May 2007     Volume: 11 Number: 430

Today's topics:
    Re: Any way to access global variable in Perl script fr <nobull67@gmail.com>
    Re: Identify if a scalar is int, double or text <klaus03@gmail.com>
    Re: Identify if a scalar is int, double or text <rvtol+news@isolution.nl>
    Re: Identify if a scalar is int, double or text <klaus03@gmail.com>
    Re: Please give me a good "rule-of-thumb" for back-slas <dalessio@motorola.NOSPAM.com>
    Re: Please give me a good "rule-of-thumb" for back-slas <jaford@watford53.freeserve.co.uk>
        Regular Expression Question jonasforssell@yahoo.se
    Re: Regular Expression Question <nobull67@gmail.com>
    Re: Regular Expression Question <m@rtij.nl.invlalid>
    Re: Solve a statistics problem sl123@netherlands.area
    Re: Solve a statistics problem <bik.mido@tiscalinet.it>
        Trouble with printing newlines to a file <smanek@gmail.com>
    Re: Trouble with printing newlines to a file <wyzelli@yahoo.com>
    Re: Trouble with printing newlines to a file <rvtol+news@isolution.nl>
    Re: Trouble with printing newlines to a file <smanek@gmail.com>
    Re: Trouble with printing newlines to a file sl123@netherlands.area
    Re: Trouble with printing newlines to a file <tadmc@augustmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 12 May 2007 00:31:01 -0700
From: Brian McCauley <nobull67@gmail.com>
Subject: Re: Any way to access global variable in Perl script from one module file?
Message-Id: <1178955061.720192.187800@n59g2000hsh.googlegroups.com>

[Micheal - please don't put all your comments at the end of the quoted
material. I've tried to intersperse your responses in context. ]

On May 11, 9:15 am, Michael Yang <Michael....@gmail.com> wrote:
> On May 11, 2:43 pm, Brian McCauley <nobul...@gmail.com> wrote:
> > On 11 May, 06:03, Michael Yang <Michael....@gmail.com> wrote:
> > > On May 11, 1:07 am, Brian McCauley <nobul...@gmail.com> wrote:
> > > > On May 10, 1:38 pm, Michael Yang <Michael....@gmail.com> wrote:

> > > > > How can I call Testrunner.pl from main.pl without spawning a separate
> > > > > child process?

> > > > You could do() it. However this would possibly give odd effects if you
> > > > try to call Testrunner.pl more than once from main.pl.

> > > I am thinking about using 'require' to do this:

> > Yes that's what I said. Well I said do() not require(). But the only
> > difference in this conext is that main.pl _cannot_ run Testrunner.pl
> > more than once.

> Thanks for your pointing it out, Brian.

Is a single run of main.pl do()ing Testrunner.pl more than once?

As I mentioned before this could give odd effects. Since you are
getting odd effects maybe this is the reason.

> > > But I found it slows the harness greatly, the performance is lowered.
>
> > That seems highly counter-intuative. I would expect it to be a little
> > faster as you are only loading perl once.
>
> > How are you observing this?

This is the key question. You need to be sure if it's really running
slowly.

> > Is it possible that it's not the tests
> > that are running slowly but rather something like an output buffering
> > issue?

> Since it's a harness containing serveral large scripts, that I can't
> paste each piece of the codes, so I only put the key parts of the
> codes to simply illustrate what I am doing. Also the codes that might
> have relationship with output buffering.

Output buffering issues are unlikely to be responsible for a real drop
in performance so before you start looking for buffering issues you
need to be sure the apparent loss of performance is illusory. If the
loss of performance is real then you should look elsewhere for the
cause.




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

Date: 12 May 2007 02:53:04 -0700
From: Klaus <klaus03@gmail.com>
Subject: Re: Identify if a scalar is int, double or text
Message-Id: <1178963584.870621.72830@h2g2000hsg.googlegroups.com>

On May 11, 11:07 pm, anno4...@radom.zrz.tu-berlin.de wrote:
> Klaus  <klau...@gmail.com> wrote in comp.lang.perl.misc:
>
> > On May 11, 2:49 pm, anno4...@radom.zrz.tu-berlin.de wrote:
>
> > > Klaus  <klau...@gmail.com> wrote in comp.lang.perl.misc:
> > > > I have a list of 4 scalars
> > > > my @L = (3, '3', 3.0, '3.0');
> > > > I want to write a subroutine type_id which returns either 'int',
> > > > 'double', 'text' (or '?') for each of the scalars
> > > > I have found a solution where I use Devel::Peek, call Dump(), redirect
> > > > STDERR into an "in-memory" file ( \$data ) and analyse the "in-memory"
> > > > content:
>
> > [snip]
>
> > > The B::* set of modules should have the means to get the info
> > > without catching printed output.
>
> > That's it - it's the B module which I was looking for (I knew it
> > existed, but I never thought I would ever use it).
>
> > Thanks a million !!
>
> Glad I could help, but I'm asking myself why you need to
> know these differences.  Perl works very hard to let us deal with
> integers, floats, strings and references in a unified way.  In
> particular, the difference of a float and an int is usually
> irrelevant.  If it matters to your program, you are probably
> not doing something the Perl way.

I agree with that.

But I have a case where I need more control over the way Perl manages
numerical values.

At one point in the future, I will certainly have to read "perldoc
perlxstut" to gain understanding of how I can use C to control
numerical values, but being on Windows XP / Activestate Perl 5.8.8
with next to no background in C, I thought I tackle the problem first
with whatever tool / module / function Perl is giving me.

Here is my case:

I have written a Perl program where some of the variables have a
special requirement, that is they deal exclusively with monetary Euro
values (i.e. the requirement is that calculations happens in normal
IEEE float/double arithmetic, and assignment to any special "monetary"
variable must be rounded to the 2nd decimal).

My first approach was to use sprintf("%.2f", $var) whenever I assign
values to a "monetary Euro" variable, but this falls foul of the way
numerical values are stored internally (i.e. 0.10 can not be
represented as an exact double).

I worked around that by storing integer "cents" rather than "Euros": I
use sprintf("%.0f", $var) when I assign "monetary" values.

That works fine, but I found out that a "monetary" value above 20
million Euros ( > 2^31 cents ) is not stored as an integer, but as a
double. As I said, it seems to work and the double seems to stick to
its "integer" properties, but I want to monitor very closely what
happens with that "double" if I grow bigger and bigger values.

My final solution would be to tie variables to some C-subroutines that
(efficiently) do 4 things when I assign values to that variable:

1. Round values to the nearest integer (same as sprintf "%.0f", but
more efficient).
2. If that value fits into an int (i.e. -2^31 <= val <= 2^31-1), then
it must always be stored as an int.
3. If it is stored as a double, then make sure that double has no
decimal.
4. If a value is too big to be represented as a double with no
decimals, then
      4a.) either set the variable to the maximum integral "double"
value,
      4b.) or, depending on a compile time option, die.

-- Klaus


>
> Anno




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

Date: Sat, 12 May 2007 12:09:55 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: Identify if a scalar is int, double or text
Message-Id: <f24at7.1ho.1@news.isolution.nl>

Klaus schreef:

> That works fine, but I found out that a "monetary" value above 20
> million Euros ( > 2^31 cents ) is not stored as an integer, but as a
> double. As I said, it seems to work and the double seems to stick to
> its "integer" properties, but I want to monitor very closely what
> happens with that "double" if I grow bigger and bigger values.

Never heared of bigints? 
http://search.cpan.org/search?query=bigint&mode=all 

-- 
Affijn, Ruud

"Gewoon is een tijger."


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

Date: 12 May 2007 05:29:52 -0700
From: Klaus <klaus03@gmail.com>
Subject: Re: Identify if a scalar is int, double or text
Message-Id: <1178972992.713281.61640@q75g2000hsh.googlegroups.com>

On May 12, 12:09 pm, "Dr.Ruud" <rvtol+n...@isolution.nl> wrote:
> Klaus schreef:
>
> > That works fine, but I found out that a "monetary" value above 20
> > million Euros ( > 2^31 cents ) is not stored as an integer, but as a
> > double. As I said, it seems to work and the double seems to stick to
> > its "integer" properties, but I want to monitor very closely what
> > happens with that "double" if I grow bigger and bigger values.
>
> Never heared of bigints?http://search.cpan.org/search?query=bigint&mode=all

Yes, I heard of the bigint::* modules, they construct a replacement of
all mathematical operations, but that carries an overhead and it lacks
control:

  ==> time/memory overhead: bigint variables are objects.
  ==> lack of control: there is no logical limit to how big a bigint
can get.

I want to stay with the standard built-in "int / double" arithmetic,
but I want additional control of the transition between the (as I see
it) 3 types of numerical values:

type 1. (int)
type 2. (double, with only integral values)
type 3. (double, with free decimal values)

This control should be implemented efficiently, hence my preference
for subroutines written in C.

--
Klaus



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

Date: Fri, 11 May 2007 11:39:00 -0500
From: "Mario D'Alessio" <dalessio@motorola.NOSPAM.com>
Subject: Re: Please give me a good "rule-of-thumb" for back-slashing in character classes
Message-Id: <f226ic$ca8$1@newshost.mot.com>


"Mr P" <MisterPerl@gmail.com> wrote in message 
news:1178723192.645442.41390@o5g2000hsb.googlegroups.com...
 ...
> Sort of along the lines of : AIEOU and sometimes Y and W (is there
> really a case when W is a vowel I never did see one, but I digress)..

The word where W is a vowel is cwm:

http://www.m-w.com/dictionary/cwm


Mario




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

Date: Sat, 12 May 2007 13:05:07 GMT
From: Jim Ford <jaford@watford53.freeserve.co.uk>
Subject: Re: Please give me a good "rule-of-thumb" for back-slashing in character classes
Message-Id: <7Yi1i.5954$r4.4960@newsfe1-gui.ntli.net>

Mario D'Alessio wrote:
> "Mr P" <MisterPerl@gmail.com> wrote in message 
> news:1178723192.645442.41390@o5g2000hsb.googlegroups.com...
> ...
>> Sort of along the lines of : AIEOU and sometimes Y and W (is there
>> really a case when W is a vowel I never did see one, but I digress)..
> 
> The word where W is a vowel is cwm:

Forget it - it's Welsh! If you want to cater for the Welsh language, 
you'll meet all sorts of horrors!
;^)

Jim Ford


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

Date: 12 May 2007 05:08:02 -0700
From: jonasforssell@yahoo.se
Subject: Regular Expression Question
Message-Id: <1178971682.001726.309150@e51g2000hsg.googlegroups.com>

Hello,


I have a file like this:

2 45 3
3 44 2 65

In other words, the first number states how many follwing numbers
there will be.

^([0-9]) ([0-9]){$1}$

The above regexp does not seem to work for parsing this. I guess the
$1 parameter is not accepted.
Any ideas of an alternative?

Thanks
/Jonas



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

Date: 12 May 2007 05:35:21 -0700
From: Brian McCauley <nobull67@gmail.com>
Subject: Re: Regular Expression Question
Message-Id: <1178973321.088011.217370@p77g2000hsh.googlegroups.com>

On May 12, 1:08 pm, jonasforss...@yahoo.se wrote:

> Subject:  Regular Expression Question

[ snip: question about using a backreference as a repeat count in
regex ]

Please put the subject of your post in the Subject of your post.

If in doubt try this simple test. Imagine you had done a search before
you posted. Then imagine that your subject line came up in the list of
hits. Would you have been able to guess with any degree of certainly
that it was the same question? If the answer is no then your subject
line is not good enough.

Remember: Just because you come here to get help not give it you
should not have the mindset that you are only here to take and have no
moral duty to give anything back to the community.

For example when poncenby asked this question on 6th Oct 2006 his
subject was "backreference oddity"[1]. Still not good but at least it
mentions that this is a question about backreferences.

A good subject would have been "Using a backreference as a repeat
count in regex".

[1] Looking up this thread to see the answer is left as an exercise
for the reader.



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

Date: Sat, 12 May 2007 14:35:54 +0200
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: Regular Expression Question
Message-Id: <pan.2007.05.12.12.36.03@rtij.nl.invlalid>

On Sat, 12 May 2007 05:08:02 -0700, jonasforssell wrote:

> Hello,
> 
> 
> I have a file like this:
> 
> 2 45 3
> 3 44 2 65
> 
> In other words, the first number states how many follwing numbers there
> will be.
> 
> ^([0-9]) ([0-9]){$1}$
> 
> The above regexp does not seem to work for parsing this. I guess the $1
> parameter is not accepted.
> Any ideas of an alternative?

I think $1 is substituted before matching against the regexp, so you get 
the value that was in $1 before the match. I guess something like the 
following (untested!) should work.

if (/^(\d+)/) {
  if (/(\d+) (?:(\d+) ?){$1}$/) {
    # we have a match
  }
}

HTH,
M4


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

Date: Sat, 12 May 2007 03:30:28 -0700
From: sl123@netherlands.area
Subject: Re: Solve a statistics problem
Message-Id: <pm5b43tplmfrabccss9gt1e2aedrui8d1f@4ax.com>

On Fri, 11 May 2007 10:47:46 -0400, "Seth Brundle" <brundlefly76@hotmail.com> wrote:

>Say I have a hash with peoples names and a quality score, like their free 
>throw percentage.
>
>Mathematically what is the most accurate way to divide the list into the two 
>most balanced teams?
>
>Obviously you could do something simple like alternate in descending order, 
>but this isnt guaranteed to produce the most accurate result.
>
>Ideally, I would like to be able to balance based on the mean, median, or 
>both.
>
>Code isnt necessary but of course appreciated - but if you have a link to 
>the fundementals of solving such a problem thats fine to - I want to learn 
>to do it for myself.
>
>
I would say you have a HASH. You can pay me to tell you how to kill yourself
but I would want the money in advance !!! Period !!!




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

Date: Sat, 12 May 2007 14:20:38 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: Solve a statistics problem
Message-Id: <cdbb435mg593agkf1mpij00m2esb7h06n7@4ax.com>

On Fri, 11 May 2007 18:03:38 +0200, Michele Dondi
<bik.mido@tiscalinet.it> wrote:

>See if any module can help you. Other than this, I could only write
>some brute force approach for your, which I suppose is not what you're
>after.

After all, you got me intrigued so I cooked up a quick'n'dirty brute
force attempt. Please note that this lacks even the obvious
optimization of only searching half of the actual search space. I was
about to add that, but then I realized how really ugly and messy the
thingie is: it was not false modesty, it would reallty rather need a
complete rewrite, probably in terms of OO code. Anyway, hereafter it
is. Use at your own risk!


  #!/usr/bin/perl -l
  
  use strict;
  use warnings;
  use List::Util qw/sum min max/;
  
  my @names=qw/alice bob carl darla eddie fred
              gina hank kate jay larry mandy nick 
              noah pete randy stu terry/;
  my %score=map { $_ => rand 1000_000} @names;
  my $len=@names;
  
  {
      my $maxi=$#names;
      my $verytot=sum values %score;
  
      my ($t1,$t2);
      sub preprocess {
          my $v=shift;
          ($t1,$t2)=();
          my $num=0;
          for (0..$maxi) {
              my $vec=vec $v, $_, 1;
              push @{ $vec ? $t1 : $t2}, $names[$_];
              $num += $vec;
          }
          $num;
      }
      
      my ($othertot,$othernum);
      sub avg {
          my $num=preprocess(shift);
          my $tot=sum @score{@$t1};
          ($othernum,$othertot)=($len-$num,$verytot-$tot);
          $tot/$num;
      }
      
      sub oavg () { $othertot/$othernum }
      
      sub teams(\@\@) { 
          @{ $_[0] }=@$t1;
          @{ $_[1] }=@$t2;
      }
  }
  
  my $diff=abs(max(values %score)-min(values %score));
  for (1..2**$len-2) {
      my $v=pack 'C*', do { 
          my @v;
          while ($_) {
              push @v, $_%256;
              $_ >>= 8;
          }
          @v;
      };
      my $avg1=avg $v;
      my $avg2=oavg;
      my (@t1,@t2);
      teams(@t1,@t2);
      next unless (my $tdiff=abs($avg1-$avg2))<$diff;
      $diff=$tdiff;
      print '-' x 72, "\n";
      print <<"EOT";
  Team #1: [@t1]
  Team #2: [@t2]
  Avg #1: $avg1 - Avg #2: $avg2 - Diff: $diff
  EOT
  }
  
  __END__


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


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

Date: 12 May 2007 02:54:24 -0700
From: shaneal <smanek@gmail.com>
Subject: Trouble with printing newlines to a file
Message-Id: <1178963664.482660.43580@w5g2000hsg.googlegroups.com>

I want to be able to print just an "LF" to a file.

First, to clear up any ambiguity, I'm going to call the ASCII
character with a hex code of 0A and a decimal code of 10 an "LF". I'm
also going to call the ASCII character with a hex code of 0D and a
decimal code of 15 a "CR".

As a simple test case:

open TEST, ">", "test.data";
select TEST;
print "A" . chr(hex("0A")) . "B" . "\n" . "C" . "\r" . "D";
close TEST;

Logically, when I look at the test.data file with a hex editor I'd
expect just a "LF" between the A and B, but instead there are is a
"CR" followed by an "LF."

Here is what a hex editor shows is in "test.data" (first line is hex,
second line are chars):

41 0D 0A 42 0D 0A 43 0D 44
A  CR LF B  CR LF C  CR D

I was expecting only a "LF" (i.e., 0A) between the "A" (41) and
"B" (42) ...

If it helps this is Perl 5.8.8 (ActiveState Perl Build 820) on Windows
XP.

Thanks for any advice you may be able to give me.



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

Date: Sat, 12 May 2007 10:23:14 GMT
From: "Peter Wyzl" <wyzelli@yahoo.com>
Subject: Re: Trouble with printing newlines to a file
Message-Id: <mAg1i.37026$M.19931@news-server.bigpond.net.au>

"shaneal" <smanek@gmail.com> wrote in message 
news:1178963664.482660.43580@w5g2000hsg.googlegroups.com...
>I want to be able to print just an "LF" to a file.
>
> First, to clear up any ambiguity, I'm going to call the ASCII
> character with a hex code of 0A and a decimal code of 10 an "LF". I'm
> also going to call the ASCII character with a hex code of 0D and a
> decimal code of 15 a "CR".
>
> As a simple test case:
>
> open TEST, ">", "test.data";
> select TEST;
> print "A" . chr(hex("0A")) . "B" . "\n" . "C" . "\r" . "D";
> close TEST;
>
> Logically, when I look at the test.data file with a hex editor I'd
> expect just a "LF" between the A and B, but instead there are is a
> "CR" followed by an "LF."
>
> Here is what a hex editor shows is in "test.data" (first line is hex,
> second line are chars):
>
> 41 0D 0A 42 0D 0A 43 0D 44
> A  CR LF B  CR LF C  CR D
>
> I was expecting only a "LF" (i.e., 0A) between the "A" (41) and
> "B" (42) ...
>
> If it helps this is Perl 5.8.8 (ActiveState Perl Build 820) on Windows
> XP.

Given that it's Windows, you may need to binmode the file so the OS doesn't 
make wrong assumptions about your file.

open TEST, ">", "test.data";
select TEST;
binmode TEST;

perldoc binmode

P
Of course I could be on the wrong track entirely....




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

Date: Sat, 12 May 2007 12:19:45 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: Trouble with printing newlines to a file
Message-Id: <f24bg2.v4.1@news.isolution.nl>

shaneal schreef:

> I want to be able to print just an "LF" to a file.
> [...] Windows 

perldoc -f binmode

(search on 'Windows' in there) 

-- 
Affijn, Ruud

"Gewoon is een tijger."


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

Date: 12 May 2007 04:13:47 -0700
From: shaneal <smanek@gmail.com>
Subject: Re: Trouble with printing newlines to a file
Message-Id: <1178968427.444709.185780@e65g2000hsc.googlegroups.com>

thanks you both very much.

i wasn't familiar with binmode. After reading some docs, it worked
flawlessly



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

Date: Sat, 12 May 2007 04:45:28 -0700
From: sl123@netherlands.area
Subject: Re: Trouble with printing newlines to a file
Message-Id: <75ab43pgsejo76k9todp0h7e5ia3hdifhd@4ax.com>

On 12 May 2007 04:13:47 -0700, shaneal <smanek@gmail.com> wrote:

>thanks you both very much.
>
>i wasn't familiar with binmode. After reading some docs, it worked
>flawlessly
bin mode is not a gay term like "flawless" its a neutral eunich term!



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

Date: Sat, 12 May 2007 07:51:03 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Trouble with printing newlines to a file
Message-Id: <slrnf4be1n.bg3.tadmc@tadmc30.august.net>

Pinocchio <sl123@netherlands.area> wrote:
> On 12 May 2007 04:13:47 -0700, shaneal <smanek@gmail.com> wrote:


>>i wasn't familiar with binmode.

> bin mode is 
 
 [snip] 

> a neutral eunich term!


binmode is not a eunich's term, it is a Perl term.

It is the same term when used on other "operating systems"
too (such as the OP's).


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

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


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