[26857] in Perl-Users-Digest
Perl-Users Digest, Issue: 8873 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Jan 21 06:05:27 2006
Date: Sat, 21 Jan 2006 03:05:05 -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 Sat, 21 Jan 2006 Volume: 10 Number: 8873
Today's topics:
Re: 2 basics questions: 1)'a' < 'b' 2)Run, but is it robic0
Cleaning HTML ;-) <kigar@gmx.net>
gunzip stdin stream? <markus.dehmann@gmail.com>
how to use perl to connect to the web? <jck11@msr.pchome.com.tw>
Re: how to use perl to connect to the web? <sbryce@scottbryce.com>
Re: how to use perl to connect to the web? <tintin@invalid.invalid>
Re: randomly choose some uniq elements of an array <someone@example.com>
Re: Regex to find paired characters only <luoyong_zh@hotmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 20 Jan 2006 22:12:10 -0800
From: robic0
Subject: Re: 2 basics questions: 1)'a' < 'b' 2)Run, but is it ok?
Message-Id: <03k3t1tbdgt81shkfvpl3hcmoaqogr1ds4@4ax.com>
On Wed, 18 Jan 2006 09:13:17 +0100, "Olaf \"El BLanco\"" <winter@yahoo.co.uk> wrote:
>
>1) Why this not work? I need the OK when a < b, B < Z, etc...
>print "First char: "; $c1 = <STDIN>;
>print "Second char: "; $c2 = <STDIN>;
>if ($c1 <= $c2)
> { print "OK"; }
>
>
This works for numbers and chars, but only checks the first character
use strict;
use warnings;
print "First char: "; my $c1 = <STDIN>;
print "Second char: "; my $c2 = <STDIN>;
if (ord($c1) <= ord($c2))
{ print "OK"; }
------------------------------
Date: Sat, 21 Jan 2006 10:20:03 +0100
From: "Reinhard Glauber" <kigar@gmx.net>
Subject: Cleaning HTML ;-)
Message-Id: <43d1fcbf$0$20788$9b4e6d93@newsread4.arcor-online.net>
DQpIaSBQZXJsLUd1cnVzLCANCg0KSSBuZWVkIHRvIGNsZWFuIGEgSFRNTCBmaWxlLCBzbyB0aGF0
IEkgZ2V0IHBsYWluIHRleHQuDQpTbywgbm93IHRoYXQgSSBrbm93IHRoYXQgdGhlcmUgaXMgc29t
ZXRoaW5nIGNhbGxlZCBwZXJsZG9jIEkgc2VhcmNoZWQgYW5kIGZvdW5kDQoNCiRodG1sID1+IHMv
PCg/OltePiciXSp8KFsnIl0pLio/XDEpKj4vL2dzJGh0bWwgPX4gcy9cdC8vZ3M7ICRodG1sID1+
IHMvXHIvL2dzOyBUaGlzIHdvcmtzIGdyZWF0LCBCVVQsIHdoZW4gSSBvcGVuIHRoZSBjbGVhbmVk
IGZpbGUgaW4gdmlJIGdldCBhIGxvdCBvZiBibHVlIF5NIC0gU2lnbnNBbHNvIHRoZXJlIGFyZSB3
YXkgdG9vIG1hbnkgYmxhbmtzIGluIHRoZXJlLkhvdyBkbyBJIGdldCB0aGVtIG91dCA/IEkga25v
dyB0aGlzIHJlYWxseSBzb3VuZHMgbGlrZSBhIGJhZCBOZXdiaWUgUXVlc3Rpb24sIGFuZG9mY291
cnNlIGl0IGlzIDstKSBIb3BlZnVsbHkgaXRzIG5vdCB0b28gYmFkLlNjcmVlbnNob3Q6IGh0dHA6
Ly93d3cuc2FiaW5lc2NodWx0ZS5kZS9wZXJsLmpwZw==
------------------------------
Date: Fri, 20 Jan 2006 22:08:38 -0500
From: Markus Dehmann <markus.dehmann@gmail.com>
Subject: gunzip stdin stream?
Message-Id: <43dmtoF1mp0s9U1@individual.net>
I have a convenient way to open possibly gzip'ed files:
open(F, ($f =~ m/\.gz$/) ? "gunzip -c $f |" : "$f");
So, if the file name ends in .gz I send it through gunzip. So far, so
good. (I don't want to use the PerlIO:Gzip module because it's not
installed by default, so it's a hassle.)
But now, my script should be callable in the following ways:
$ cat data | ./script.pl
$ ./script.pl data.gz
$ ./script.pl data
Usually, I would just use the while loop: while(<>){...}. But that does
not read gzip'ed data.
How would you handle that? I could think of the following code, but it's
long and not nice ...
if(defined $ARGV[0] && -f $ARGV[0]){
readFromFile($ARGV[0]);
}else{
readFromStdin();
}
sub readFromFile{
my ($f) = @_;
open(F, ($f =~ m/\.gz$/) ? "gunzip -c $f |" : "$f")
or die("Could not open $f: $!");
while(<F>){
processLine($_);
}
close F;
}
sub readFromStdin{
while(<>){
processLine($_);
}
}
sub processLine{ ... }
Thanks!
Markus
------------------------------
Date: Sat, 21 Jan 2006 11:36:28 +0800
From: "jck11" <jck11@msr.pchome.com.tw>
Subject: how to use perl to connect to the web?
Message-Id: <dqsa8l$2th$1@netnews.hinet.net>
hi all
when I use IE to connect to the www.google.com
I find that my Request Header sends the data 'cookie' ==>
PREF=ID=66f018844077c8d5:TM=1133289512:LM=1133289512:S=EN29yY8uk1D-VVyS
if I want to use perl to connect to the www.google.com
How to fill the cookie field??
Thanks a lot
$req->header(
'Accept' => 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword,
application/x-shockwave-flash, */*',
'Accept-Language' => 'zh-tw',
'Accept-Encoding' => 'gzip, deflate',
'User-Agent' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;
.NET CLR 1.1.4322; .NET CLR 2.0.50727)',
'Host' => 'www.google.com',
'Connection' => 'Keep-Alive',
'Cookie' => ''); ???? how do I know I must fill the data
'PREF=ID=66f018844077c8d5:TM=1133289512:LM=1133289512:S=EN29yY8uk1D-VVyS' h
------------------------------
Date: Fri, 20 Jan 2006 21:00:54 -0700
From: Scott Bryce <sbryce@scottbryce.com>
Subject: Re: how to use perl to connect to the web?
Message-Id: <KIqdnUJOlbdpLEzenZ2dnUVZ_t-dnZ2d@comcast.com>
jck11 wrote:
> if I want to use perl to connect to the www.google.com
> How to fill the cookie field??
You can connect to google.com without filling in the cookie.
If for some reason you feel that you need to, then we need to know
whether you are asking 1) How do I determine what the cookie value
should be, or 2) How do I send that value to google.com, or 3) some
other question.
If you question is 1, then this is not a Perl question, and you will
have to ask elsewhere.
If your question is 2, then you should post a small but complete script
that shows what you have tried, and tell us what it does that is
different than what you expected.
If your question is 3, then you should re-word your question so that it
is clearer.
In any case, please read the posting guidelines for this group. They are
posted here twice a week.
------------------------------
Date: Sat, 21 Jan 2006 19:58:23 +1300
From: "Tintin" <tintin@invalid.invalid>
Subject: Re: how to use perl to connect to the web?
Message-Id: <UDmAf.18035$vH5.888732@news.xtra.co.nz>
"jck11" <jck11@msr.pchome.com.tw> wrote in message
news:dqsa8l$2th$1@netnews.hinet.net...
> hi all
> when I use IE to connect to the www.google.com
> I find that my Request Header sends the data 'cookie' ==>
> PREF=ID=66f018844077c8d5:TM=1133289512:LM=1133289512:S=EN29yY8uk1D-VVyS
>
> if I want to use perl to connect to the www.google.com
> How to fill the cookie field??
See http://www.google.com/apis/index.html first.
------------------------------
Date: Sat, 21 Jan 2006 10:38:00 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: randomly choose some uniq elements of an array
Message-Id: <caoAf.135976$OU5.73321@clgrps13>
Martin Kissner wrote:
>
> I want to choose a number of files from a directory randomly.
>
> This is what I have so far (after reading "perlodc -f "How do I shuffle
> an array randomly?"").
>
> -------
> #!/usr/bin/perl
>
> use warnings;
> use strict;
>
> my $dir = "/path/to/folder";
> opendir DH, $dir || die "can not open $dir: $!";
The precedence of '||' is too high for that to work correctly (it will never
die.) You have to either use parentheses:
opendir( DH, $dir ) || die "can not open $dir: $!";
Or use the lower precedence 'or' operator:
opendir DH, $dir or die "can not open $dir: $!";
> my @files = grep !/^\.\.?$|/ ,readdir DH;
^
Your regular expression says to match the pattern '^\.\.?$' or alternatively
to match nothing and since the expression is negated that means that @files
will receive nothing!
my @files = grep !/^\.\.?$/, readdir DH;
> closedir DH;
>
> my @shuffled;
> while (@files) {
> # the FAQ says this is bad
> push(@shuffled, splice(@files, rand @files, 1));
> }
>
> for (1..3) {
> print pop @shuffled,"\n";
> }
> -------
>
> I am pretty sure that there is a better an simpler solution.
> Any suggestions will be appreciated.
my @shuffled = map splice( @files, rand @files, 1 ), 1 .. 3;
John
--
use Perl;
program
fulfillment
------------------------------
Date: Sat, 21 Jan 2006 11:18:29 +0800
From: yong <luoyong_zh@hotmail.com>
Subject: Re: Regex to find paired characters only
Message-Id: <dqs93e$fkj$1@news.yaako.com>
Graham Drabble wrote:
> I have the following data in a file
>
> p p p p p p p p
> p b b p p p p p
> b b p p p p p
> s b b p p p p p
> p b b p b b p p
> s b p p p p p p
> s b b b p p p p
> p b b b b b p p
> b b p b p p p
> b b b b p p p
> p p p p p b b
> p s b s p p p b
>
> I'm looking to write a regular expression that will only match if a
> 'b ' are paired together.
>
> I've currently got
>
> use strict;
> use warnings;
>
> open IN, '<', 'pb.txt' or die "Can't open IN: $!";
>
> while (<IN>){
> chomp;
> my $regex;
> if (!/[sp] b [sp]/){
> $regex = 1;
> }else{
> $regex = 0;
> }
>
> print "$_\t $regex\n";
> }
>
> which produces
>
> p p p p p p p p 1
> p b b p p p p p 1
> b b p p p p p 1
> s b b p p p p p 1
> p b b p b b p p 1
> s b p p p p p p 0
> s b b b p p p p 1
> p b b b b b p p 1
> b b p b p p p 0
> b b b b p p p 1
> p p p p p b b 1
> p s b s p p p b 0
>
> It should (if it was doing what I wanted!) produce
>
> p p p p p p p p 1
> p b b p p p p p 1
> b b p p p p p 1
> s b b p p p p p 1
> p b b p b b p p 1
> s b p p p p p p 0
> s b b b p p p p 0 ****
> p b b b b b p p 0 ****
> b b p b p p p 0
> b b b b p p p 1
> p p p p p b b 1
> p s b s p p p b 0
>
> (The **** are not output but are there to indicate the lines that it
> gets wrong.)
>
> The problem is that it treats 'b b b' as good where it shouldn't
> (although b b b b is fine).
>
> Any suggestions?
>
-----------------------------------
use strict;
open(my $fh,"pb.txt") or die "could not open file($!).stop";
while(<$fh>) {
chomp;
my $line_2=$_;
$line_2=~s/b\sb//g;
if ($line_2=~/b/) {
print $_."\t0\n";
}else {
print $_."\t1\n";
}
}
-----------------------------------
------------------------------
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 V10 Issue 8873
***************************************