[29668] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 912 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Oct 6 18:09:39 2007

Date: Sat, 6 Oct 2007 15:09:05 -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, 6 Oct 2007     Volume: 11 Number: 912

Today's topics:
    Re: FAQ 5.5 How can I copy a file? <brian.d.foy@gmail.com>
        How to make klick after put value in TextBox <tena@makniovotht.hr>
    Re: Load file into a hash <nobull67@gmail.com>
    Re: Load file into a hash <nobull67@gmail.com>
    Re: Load file into a hash <m@rtij.nl.invlalid>
    Re: Load file into a hash <tadmc@seesig.invalid>
    Re: Load file into a hash <tadmc@seesig.invalid>
    Re: More math than perl... QoS@domain.invalid
    Re: More math than perl... (Doug Miller)
    Re: More math than perl... <bik.mido@tiscalinet.it>
    Re: newbie question: find index of substr using regexp <rvtol+news@isolution.nl>
    Re: newbie question: find index of substr using regexp <tadmc@seesig.invalid>
    Re: newbie question: find index of substr using regexp <rvtol+news@isolution.nl>
        Open a file from PERL / ASP ? <jack_posemsky@yahoo.com>
    Re: perl join on a non printable variable character ? <nobull67@gmail.com>
    Re: perl join on a non printable variable character ? <nobull67@gmail.com>
    Re: perl join on a non printable variable character ? <brian.d.foy@gmail.com>
    Re: perl join on a non printable variable character ? <jack_posemsky@yahoo.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Sat, 06 Oct 2007 13:54:45 -0500
From: brian d  foy <brian.d.foy@gmail.com>
Subject: Re: FAQ 5.5 How can I copy a file?
Message-Id: <061020071354458677%brian.d.foy@gmail.com>

In article <fe6fvn.j0.1@news.isolution.nl>, Dr.Ruud
<rvtol+news@isolution.nl> wrote:

> PerlFAQ Server schreef:
> 
> >             use File::Copy;
> 
> But not before reading some of 
> http://www.google.co.uk/search?q=file.copy+abigail 
> 
> http://perl.abigail.be/Talks/FC/HTML/


Indeed, although I think we should fix File::Copy so it doesn't have
those problems. I'm not sure why nobody hasn't.


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

Date: Sun, 7 Oct 2007 00:01:50 +0200
From: "Tena" <tena@makniovotht.hr>
Subject: How to make klick after put value in TextBox
Message-Id: <fe90ki$sb1$1@localhost.localdomain>

How to make click on form after I put value in TextBox.
I tray everything but unsuccessful.

Please help me.

*****************************************

use warnings;
use Win32::IEAutomation;

my $ie = Win32::IEAutomation->new( visible => 1, maximize => 1 );
$ie->gotoURL('http://www.prva-generacija.hr/');
$ie->getTextBox('name:', "txtUserName")->SetValue('user1');
$ie->getTextBox('name:', "txtPassword")->SetValue('passwd1');


******************************************




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

Date: Sat, 06 Oct 2007 14:06:18 -0000
From:  Brian McCauley <nobull67@gmail.com>
Subject: Re: Load file into a hash
Message-Id: <1191679578.825266.173520@o3g2000hsb.googlegroups.com>

On Oct 6, 12:19 pm, Mark Clements <mark.clementsREMOVET...@wanadoo.fr>
wrote:
> my %hash = map { chomp; split /\t/ } <$fh>;

That works but is very fragile - one bad line can screw all your data
from then on.

I prefer (the canonical idiom)

my %hash = map { /(.*?)\t(.*)/ } <$fh>;

This will ignore lines with no "\t" in them. Do something vaguely
reasonable with lines containing more than one "\t". Oh, and it's
shorter too.



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

Date: Sat, 06 Oct 2007 14:11:56 -0000
From:  Brian McCauley <nobull67@gmail.com>
Subject: Re: Load file into a hash
Message-Id: <1191679916.465855.73040@r29g2000hsg.googlegroups.com>

On Oct 6, 3:06 pm, Brian McCauley <nobul...@gmail.com> wrote:
>
> my %hash = map { /(.*?)\t(.*)/ } <$fh>;

Oh, and since we're slurping the file anyhow we can save a few lines
by using File::Slurp

use File::Slurp;
my %hash = map { /(.*?)\t(.*)/ } read_file('test.txt');



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

Date: Sat, 6 Oct 2007 22:36:07 +0200
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: Load file into a hash
Message-Id: <pan.2007.10.06.20.33.08@rtij.nl.invlalid>

On Sat, 06 Oct 2007 14:06:18 +0000, Brian McCauley wrote:

> On Oct 6, 12:19 pm, Mark Clements <mark.clementsREMOVET...@wanadoo.fr>
> wrote:
>> my %hash = map { chomp; split /\t/ } <$fh>;
> 
> That works but is very fragile - one bad line can screw all your data
> from then on.
> 
> I prefer (the canonical idiom)
> 
> my %hash = map { /(.*?)\t(.*)/ } <$fh>;
> 
> This will ignore lines with no "\t" in them. Do something vaguely
> reasonable with lines containing more than one "\t". Oh, and it's
> shorter too.

Doesn't that include the newline on any line in the second field?

M4


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

Date: Sat, 06 Oct 2007 21:36:40 GMT
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: Load file into a hash
Message-Id: <slrnfgfvkn.eum.tadmc@tadmc30.sbcglobal.net>

Martijn Lievaart <m@rtij.nl.invlalid> wrote:
> On Sat, 06 Oct 2007 14:06:18 +0000, Brian McCauley wrote:


>> my %hash = map { /(.*?)\t(.*)/ } <$fh>;


> Doesn't that include the newline on any line in the second field?


Which part of the regex can match those newlines?


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


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

Date: Sat, 06 Oct 2007 21:36:41 GMT
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: Load file into a hash
Message-Id: <slrnfgfvqd.eum.tadmc@tadmc30.sbcglobal.net>

Bill H <bill@ts1000.us> wrote:
> Is there a "perl" way of loading a file directly into a hash instead
> of using something like this quick example:
>
> open(FILE,"test.txt");


You should always, yes *always*, check the return value from open().


> while(<FILE>)
> {
> $line = $_;


If you want it in $line, then put it there, rather than put it
somewhere else and then move it there:

   while ( my $line = <FILE> )


> chop $line;


You should use chomp() to remove newlines.


   my %myhash = split /[\t\n]/, do{ local $/; <FILE>};


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


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

Date: Sat, 06 Oct 2007 13:31:24 GMT
From: QoS@domain.invalid
Subject: Re: More math than perl...
Message-Id: <M6MNi.1261$rD1.419@trnddc01>


Bill H <bill@ts1000.us> wrote in message-id:  <1191675981.873467.299980@w3g2000hsg.googlegroups.com>

> 
> On Oct 6, 7:01 am, Bill H <b...@ts1000.us> wrote:
> > On Oct 6, 1:56 am, "Mumia W." <paduille.4061.mumia.w
> >
> >
> >
> >
> >
> > +nos...@earthlink.net> wrote:
> > > On 10/05/2007 09:55 PM, l v wrote:
> >
> > > > Bill H wrote:
> > > >> [ problem calculating the median without using too much memory ]
> > > >> Bill H
> >
> > > > use strict;
> > > > use warnings;
> > > > @ARRAY = (0,0,0,1,3,3,3,3,4,5,5);
> >
> > > This kind of array is what Bill wanted to avoid creating.
> >
> > > > # using the same array since you are concerned about memory.
> > > > # need to load the array to handle sorting of 2 digit numbers.
> > > > @ARRAY = sort map {sprintf "%05d", $_} @ARRAY;
> >
> > > How is that simpler than this?
> >
> > > @ARRAY = sort { $a <=> $b } @ARRAY;
> >
> > > > $midPoint = $#ARRAY / 2;
> > > > $median = $ARRAY[int $midPoint];
> >
> > > > if ($midPoint != int $midPoint) {
> > > >     $upperPoint = $midPoint +1;
> > > >     $median = ($median + $ARRAY[int $upperPoint]) / 2;
> > > > }
> >
> > > > print "median = $median\n";
> >
> > > use POSIX 'ceil';
> > > print "median = ", $ARRAY[ceil(@ARRAY/2)], "\n";
> >
> > > > But this is why I use the Statistics::Descriptive::Discrete module to
> > > > calculate medians.
> >
> > > Bill said he didn't want to use any modules.
> >
> > Thanks for the help guys. I ended up using a combination of the
> > examples given:
> >
> > sub getMedian
> > {
> >     my @values = @_;
> >     my @median = map { ($_) x $values[$_] } (0..5);
> >     my $m = int(@median / 2);
> >     if ($m != @median / 2)
> >     {
> >         $m = int(($median[$m] + $median[$m + 1]) / 2);
> >     }
> >     else
> >     {
> >         $m = $median[$m];
> >     }
> >     return ($m);
> >
> > }
> >
> > where I call it with:
> >
> > $median = getMedian(@RATE);
> >
> > I do end up creating the array, but I think it will be ok.
> >
> > Bill H- Hide quoted text -
> >
> > - Show quoted text -
> 
> After playing with it for awhile I wonder if median is what I really
> need. Logically, if you have 60 people rate the page at 0 and 30
> people rate it at 5 then the page rating should be somewhere between 1
> and 2, but using a median it would still be ranked at a 0 (middle
> element in the array would be a 0). I know it aint strictly perl, but
> any thoughts?
> 
> Bill H

Perhaps then you need the average; first create a total for each of
your possible values and then add these totals together, then divide
by six for the average.

Here is a simple example which needs some hardening but may show the
concept fairly clearly.

#!/usr/bin/perl/
use strict;
use warnings;

my @values = (2, 4, 3, 9, 4, 16);
my $total = 0;
my $average;

foreach my $i (0..5) {
  $total += $values[$i] || 0;
}
$average = $total / 6;
print "The average response is:  [$average]\n";




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

Date: Sat, 06 Oct 2007 14:05:52 GMT
From: spambait@milmac.com (Doug Miller)
Subject: Re: More math than perl...
Message-Id: <RCMNi.425$LD2.290@newssvr17.news.prodigy.net>

In article <1191675981.873467.299980@w3g2000hsg.googlegroups.com>, Bill H <bill@ts1000.us> wrote:

>After playing with it for awhile I wonder if median is what I really
>need. 

Probably not.

>Logically, if you have 60 people rate the page at 0 and 30
>people rate it at 5 then the page rating should be somewhere between 1
>and 2, 

((60 * 0) + (30 * 5)) / (60 + 30) = 150/90 = 1.667

>but using a median it would still be ranked at a 0 (middle
>element in the array would be a 0). I know it aint strictly perl, but
>any thoughts?

Use the mean instead. Or you could display a full statistical report: mean, 
median, mode, and standard deviation. :-)

-- 
Regards,
        Doug Miller (alphageek at milmac dot com)

It's time to throw all their damned tea in the harbor again.


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

Date: Sat, 06 Oct 2007 17:59:16 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: More math than perl...
Message-Id: <v1cfg3l9biigqq4lb4tbkik7nruvdtoss2@4ax.com>

On Fri, 05 Oct 2007 16:49:31 -0500, "Mumia W."
<paduille.4061.mumia.w+nospam@earthlink.net> wrote:

>I know the mean can be calculated "on the fly"--without storing all of 
>the values to be examined, but I can't see how this is to be done with 
>the median; I don't think it's possible.

Sure it is possible:

  #!/usr/bin/perl
  
  use strict;
  use warnings;
  use List::Util 'sum';
  use constant TESTS => 20;
  use Test::More tests => TESTS;
  
  sub naive {
      my @arr = map +($_) x $_[$_], 0..$#_;
      @arr % 2 ? 
        @arr[(@arr-1)/2] :
        (@arr[@arr/2 - 1] + @arr[@arr/2])/2;
  }
  
  sub findidx {
      my $i=shift;
      ($i -= $_[$_])<0 and return $_ for 0..$#_;
  }
  
  sub smart {
      my $t=sum @_;
      $t%2 ?
        findidx +($t-1)/2, @_ :
        (findidx($t/2-1, @_) + findidx($t/2, @_))/2;
  }
  
  for (1..TESTS) {
      my @a=map int rand 10, 0..5;
      is smart(@a), naive(@a), "Test @a";
  }
  
  __END__


Note: it is to be noted here that smart() is not very smart because I
feel the calculations performed by (findidx($t/2-1, @_) and
findidx($t/2, @_) are very much the same, but in the first attempt
with no helper sub I always got some failing error, so this one
however bloated at least shows as a proff of concept that it is not
necessary to go brute force.

>PS.
>I would have given this post a more descriptive subject line like: 
>calculating median without using too much memory.

Seconded.


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: Sat, 6 Oct 2007 16:56:12 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: newbie question: find index of substr using regexp
Message-Id: <fe8ep3.148.1@news.isolution.nl>

Mintcake schreef:

> remove leading whitespace from the string
> 
> $a =~ s/\s*//;

No, s/\s+//

-- 
Affijn, Ruud

"Gewoon is een tijger."


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

Date: Sat, 6 Oct 2007 06:02:54 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: newbie question: find index of substr using regexp
Message-Id: <slrnfgeqqu.71c.tadmc@tadmc30.sbcglobal.net>

Mintcake <tony@skelding.co.uk> wrote:
> On Oct 6, 2:53 am, Jim Gibson <jgib...@mail.arc.nasa.gov> wrote:
>> In article
>> <Pine.WNT.4.64.0710050944340.3...@ctfanxnfba.unjnvvnaryrpgevp.arg>,
>>
>> Sean Nakasone <seannakas...@yahoo.com> wrote:
>> > ok i guess i'll be using this.  thanks for your help
>>
>> > use strict;
>> > use warnings;
>>
>> > my $a = "  quick brown";
>> > $a =~ /[^ ]/;
>> > print (length($`));
>>
>> Don't use any of the RE special variables unless the match succeeded:
>>
>> if( $a =~ /[^ ]/ ) {
>>   print (length($`));
>>
>> }
>>
>> --
>> Jim Gibson
>>
>>  Posted Via Usenet.com Premium Usenet Newsgroup Services
>> ----------------------------------------------------------
>>     ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
>> ----------------------------------------------------------        
>>                http://www.usenet.com


[ It is bad 'net manners to quote .sigs ]


> This is just a shot in the dark 


It sure is.

It does not answer the question that was asked.

It does not correctly answer the wrong question either...


> but it sounds to me like you just want
> to remove leading whitespace from the string - 


No, the OP wants to "find index of substr using regexp" just
like the Subject header says.

Nowhere in this thread is there anything about removing any characters.


> in which case...
>
> $a =~ s/\s*//;


What is the point of replacing zero characters with zero characters?

That removes the first run of whitespace characters, whether they
are "leading" or not.

   $a =~ s/^\s+//; # remove leading whitespace


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


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

Date: Sat, 6 Oct 2007 17:21:23 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: newbie question: find index of substr using regexp
Message-Id: <fe8ge8.1lg.1@news.isolution.nl>

Dr.Ruud schreef:
> Mintcake:

>> remove leading whitespace from the string
>> 
>> $a =~ s/\s*//;
> 
> No, s/\s+//

And s/^\s+// is better altogether. 

-- 
Affijn, Ruud

"Gewoon is een tijger."


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

Date: Sat, 06 Oct 2007 21:52:55 -0000
From:  Jack <jack_posemsky@yahoo.com>
Subject: Open a file from PERL / ASP ?
Message-Id: <1191707575.542082.258380@g4g2000hsf.googlegroups.com>

Hi  I use this all the time with .pl programs  and it works great but
as soon as I put in ASP it blows up b/c it cant find the file ;
however same code works as tested in a .pl and the file DOES exist !
Any code that works for within an ASP file would be appreciated !
<%@Language="PerlScript"

if ($var ne '') {
$filename1 = 'e:\temp2\searchlog.txt';
open(OUTFILE,">>$filename1")|| die 'ERROR : external table not
found :'.$filename1."\n";
}
%>

thanks in advance, Jack



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

Date: Sat, 06 Oct 2007 16:53:47 -0000
From:  Brian McCauley <nobull67@gmail.com>
Subject: Re: perl join on a non printable variable character ?
Message-Id: <1191689627.684691.213080@19g2000hsx.googlegroups.com>

On Oct 6, 11:51 am, Brian McCauley <nobul...@gmail.com> wrote:
> On Oct 5, 2:14 am, "John W. Krahn" <du...@example.com> wrote:
>
> > You are passing the string '\034' to your program from the command line so
> > your $delimiter variable will contain the string consisting of the four
> > characters '\', '0', '3' and '4'.
>
> So, the OP is probably really trying to get around to asking...
>
> FAQ: How do I unescape a string?

Look you babbling moron, just because the OP is _asking_ a FAQ, it
doesn't mean the answer there is really going to help. The answer in
the FAQ only deals with normal character escapes and shies away from
the general case because it's just too complicated.

Next time, you[1] may like to look what you're pointing people to
before crying RTFFAQ.

The answer given in the FAQ "How can I expand variables in text
strings?" is actually closer.

Be aware, however, that the FAQ answer to "How can I expand variables
in text strings?" is full of half-truths and misdirection. See
numerous previous threads on the subject "How can I expand variables
in text strings?" for details.


[1] Er, "I"



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

Date: Sat, 06 Oct 2007 17:46:15 -0000
From:  Brian McCauley <nobull67@gmail.com>
Subject: Re: perl join on a non printable variable character ?
Message-Id: <1191692775.588599.327190@w3g2000hsg.googlegroups.com>

On Oct 6, 5:53 pm, Brian McCauley <nobul...@gmail.com> wrote:
> On Oct 6, 11:51 am, Brian McCauley <nobul...@gmail.com> wrote:

> > FAQ: How do I unescape a string?
>
> Next time, you[1] may like to look what you're pointing people to
> before crying RTFFAQ.

OK, OK! How's about...

s/(?<!\\)(?:\\)*(["\@\$])/\\$1/g; # Prevent interpolation
$_ = eval qq{"$_"};
die $@ if $@;

Now, that would really be the right answer to have in the FAQ.

Unless, that is, anyone can see a way to circumvent the check for
characters that may give access to more than just escapes.

Are there any \X{....} constructs that could be dangerous?



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

Date: Sat, 06 Oct 2007 14:18:01 -0500
From: brian d  foy <brian.d.foy@gmail.com>
Subject: Re: perl join on a non printable variable character ?
Message-Id: <061020071418012430%brian.d.foy@gmail.com>

In article <1191689627.684691.213080@19g2000hsx.googlegroups.com>,
Brian McCauley <nobull67@gmail.com> wrote:


> Be aware, however, that the FAQ answer to "How can I expand variables
> in text strings?" is full of half-truths and misdirection. See
> numerous previous threads on the subject "How can I expand variables
> in text strings?" for details.

I thought we'd fixed the problems you had. Now you're calling me a
liar? What's the problem now?


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

Date: Sat, 06 Oct 2007 21:48:07 -0000
From:  Jack <jack_posemsky@yahoo.com>
Subject: Re: perl join on a non printable variable character ?
Message-Id: <1191707287.820122.206430@57g2000hsv.googlegroups.com>

On Oct 6, 12:18 pm, brian d  foy <brian.d....@gmail.com> wrote:
> In article <1191689627.684691.213...@19g2000hsx.googlegroups.com>,
>
> Brian McCauley <nobul...@gmail.com> wrote:
> > Be aware, however, that the FAQ answer to "How can I expand variables
> > in text strings?" is full of half-truths and misdirection. See
> > numerous previous threads on the subject "How can I expand variables
> > in text strings?" for details.
>
> I thought we'd fixed the problems you had. Now you're calling me a
> liar? What's the problem now?

Thanks but still dont have an answer to the question - thanks for all
the other pointers, but how do I change my code to join on the
argument I passed in - specifically on the join call if possible.

Thanks in advance, Jack



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

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


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