[17756] in Perl-Users-Digest
Perl-Users Digest, Issue: 5176 Volume: 9
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Dec 21 21:10:24 2000
Date: Thu, 21 Dec 2000 18:10:13 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <977451012-v9-i5176@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Thu, 21 Dec 2000 Volume: 9 Number: 5176
Today's topics:
Re: regexp question (Richard J. Rauenzahn)
Re: regexp question (Tad McClellan)
Re: Searching for a Value in an Array to remove (Richard J. Rauenzahn)
Re: Searching for a Value in an Array to remove <jhelman@wsb.com>
Re: Searching for a Value in an Array to remove <revjack@revjack.net>
Re: Searching for a Value in an Array to remove (Richard Zilavec)
Re: Searching for a Value in an Array to remove <abe@ztreet.demon.nl>
Re: Searching for a Value in an Array to remove <bart.lateur@skynet.be>
Re: Sorting hash <jhelman@wsb.com>
Re: Sorting hash eggrock@my-deja.com
Re: Why are multiple zeroes true? miko@idocs.com
Re: Why are multiple zeroes true? miko@idocs.com
Re: Why are multiple zeroes true? (Tom Christiansen)
Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 21 Dec 2000 23:08:37 GMT
From: nospam@hairball.cup.hp.com (Richard J. Rauenzahn)
Subject: Re: regexp question
Message-Id: <977440116.623830@hpvablab.cup.hp.com>
"Robert Basil" <robert@no-spam.basil.com> writes:
>I am trying to start an archive of a mailing list and I need to remove a
>part of the message after it is saved to a text file i.e
[summary...]
<MULTILINE BLOCK OF TEXT A>
<MULTILINE BLOCK OF TEXT B>
<MULTILINE BLOCK OF TEXT C>
Where A and C are the same and you want to remove C?
>So when I try and remove them, my regexp also removes the middle part of the
>message that I want to keep. I have triend about 20 different regexp's with
>no luck so far. Any hints?
You haven't defined the problem well enough for me yet -- What's after
the block of text 'C'? The end of the file? What's before text 'A'?
The message headers and the beginning of the file, or another message?
What RE's have you tried so far? (At least show what you consider the
best ones are so far...)
Rich
--
Rich Rauenzahn ----------+xrrauenza@cup.hp.comx+ Hewlett-Packard Company
Technical Consultant | I speak for me, | 19055 Pruneridge Ave.
Development Alliances Lab| *not* HP | MS 46TU2
ESPD / E-Serv. Partner Division +--------------+---- Cupertino, CA 95014
------------------------------
Date: Thu, 21 Dec 2000 18:17:22 -0500
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: regexp question
Message-Id: <slrn9453s2.6dd.tadmc@magna.metronet.com>
Robert Basil <robert@no-spam.basil.com> wrote:
>I am trying to start an archive of a mailing list and I need to remove a
>part of the message after it is saved to a text file i.e
>So when I try and remove them, my regexp also removes the middle part of the
>message that I want to keep. I have triend about 20 different regexp's with
>no luck so far. Any hints?
s/SUBSCRIBE
If this newsletter has been forwarded to you, and
you wish to subscribe, simply send a blank e-mail to:
mailto:listname\@domain.com
UNSUBSCRIBE
You are currently subscribed to mailing-line-name:
myname\@domain.com
To unsubscribe send a blank email to
mailto:leave-listname\@domain.com
//g;
--
Tad McClellan SGML consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: 21 Dec 2000 23:03:29 GMT
From: nospam@hairball.cup.hp.com (Richard J. Rauenzahn)
Subject: Re: Searching for a Value in an Array to remove
Message-Id: <977439808.746864@hpvablab.cup.hp.com>
rzilavec@tcn.net writes:
>@array = (".", "..", "filename1", "filename2", "filename3");
>for(@array) {
> unless($_ =~ /\./ || $_ =~ /\.\./) {
shortcut hint: by default m// binds with $_, so you could just write
unless(/\./ || /\.\./)
but...
> push(@tmparray, $_);
> }
>}
You really don't want to use unanchored RE's instead of an equality
operator. Your example will remove "file.txt" from the list if it
occurred.
Here's your example rewritten...
#!/usr/bin/perl -wl
use strict;
my @array = (".", "..", "filename1", "filename2", "filename3");
# using eq
my @tmparray;
for(@array) {
push @tmparray, $_ unless($_ eq '.' || $_ eq '..');
}
print @tmparray;
# using RE
@tmparray = ();
for(@array) {
push @tmparray, $_ unless(/^\.\.?$/);
}
print @tmparray;
####
Or just use grep...
@array = grep (!/^\.\.?$/, @array);
Rich
--
Rich Rauenzahn ----------+xrrauenza@cup.hp.comx+ Hewlett-Packard Company
Technical Consultant | I speak for me, | 19055 Pruneridge Ave.
Development Alliances Lab| *not* HP | MS 46TU2
ESPD / E-Serv. Partner Division +--------------+---- Cupertino, CA 95014
------------------------------
Date: Thu, 21 Dec 2000 23:11:06 GMT
From: Jeff Helman <jhelman@wsb.com>
Subject: Re: Searching for a Value in an Array to remove
Message-Id: <3A428E60.DC266AB4@wsb.com>
Some_Indiana_Guy wrote:
> I am not wanting to email the files "." and ".." Is there maybe a way
> i can filter this out before it even gets into the array?? That might be
> a better way to approach it.
This would almost certainly be a better approach. Why go through
copying the entire array if we can eliminate the problem at the outset?
> my ($path,@filelist);
> $path="/some/unix/path";
> opendir(PATH,$path) or die "Can't open dir: $!\n";
This is fine. Replace the following line:
> @filelist=readdir(PATH);
with this:
while ($_ = readdir(PATH)) {
push(@filelist, $_) unless (/^\.{1,2}$/);
}
> closedir PATH;
That'll do it.
JH
------------------------------
Date: 21 Dec 2000 23:12:58 GMT
From: revjack <revjack@revjack.net>
Subject: Re: Searching for a Value in an Array to remove
Message-Id: <91u2pq$5fp$2@news1.Radix.Net>
Keywords: Hexapodia as the key insight
Some_Indiana_Guy <!spammed@aye.net!> wrote:
: I am not wanting to email the files "." and ".." Is there maybe a way
: i can filter this out before it even gets into the array?? That might be
: a better way to approach it.
: my ($path,@filelist);
: $path="/some/unix/path";
: opendir(PATH,$path) or die "Can't open dir: $!\n";
: @filelist=readdir(PATH);
: closedir PATH;
The Camel Book suggests:
@filelist = grep !/^\.\.?$/, readdir PATH;
--
___________________
revjack@revjack.net
------------------------------
Date: Thu, 21 Dec 2000 23:09:01 GMT
From: rzilavec@tcn.net (Richard Zilavec)
Subject: Re: Searching for a Value in an Array to remove
Message-Id: <3a468b37.29848643@news.tcn.net>
On Thu, 21 Dec 2000 17:57:41 -0500, "Some_Indiana_Guy"
<!spammed@aye.net!> wrote:
>my ($path,@filelist);
> $path="/some/unix/path";
> opendir(PATH,$path) or die "Can't open dir: $!\n";
perldoc -f readdir
This shows an example using grep as a filter. You could reverse the
matches with an !.
perldoc -f grep
--
Richard Zilavec
rzilavec@tcn.net
------------------------------
Date: Fri, 22 Dec 2000 01:32:24 +0100
From: Abe Timmerman <abe@ztreet.demon.nl>
Subject: Re: Searching for a Value in an Array to remove
Message-Id: <vr754t4v5d60mheihlht9i9piv6meqqpuh@4ax.com>
On Thu, 21 Dec 2000 17:57:41 -0500, "Some_Indiana_Guy"
<!spammed@aye.net!> wrote:
> This is the code (borrowed from someone else) that creates the array.
> As you can see it puts a directory listing into the array. I want to
> use this array as a list of files to email to an internet email address.
> I am not wanting to email the files "." and ".." Is there maybe a way
> i can filter this out before it even gets into the array?? That might be
> a better way to approach it.
<code snipped>
If you want files, test for files.
#!/usr/bin/perl -w
use strict;
my $path="./testfiles"; # change for your situation
opendir(PATH,$path) or die "Can't opendir '$path': $!\n";
my @filelist = grep -f "$path/$_" => readdir(PATH);
closedir PATH;
print map "$_\n" => @filelist;
__END__
perldoc -f -X
--
Good luck,
Abe
perl -wle '$_=q@Just\@another\@Perl\@hacker@;print qq@\@{[split/\@/]}@'
------------------------------
Date: Fri, 22 Dec 2000 01:55:27 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: Searching for a Value in an Array to remove
Message-Id: <p2d54tod8op5jq0k2f3qitm400o8tttig6@4ax.com>
Some_Indiana_Guy wrote:
>i have an array that looks like this.
>
>@array (".", "..", "filename1", "filename2", "filename3")
>
>i want to search the array for ".." and "." so i can remove them from the
>array
>no matter where they are in the array. Not just in the beginning of the
>array.
>and ideas on how this can be done?
grep()
Don't get these items in the first place (I bet you inted to use
readdir().)
@array = grep { $_ ne '.' and $_ ne '..' }
(".", "..", "filename1", "filename2", "filename3");
--
Bart.
------------------------------
Date: Thu, 21 Dec 2000 23:05:31 GMT
From: Jeff Helman <jhelman@wsb.com>
Subject: Re: Sorting hash
Message-Id: <3A428D19.B1621321@wsb.com>
bkesuma@yahoo.com wrote:
> foreach $email (sort keys %COUNT){
> print "$email = $COUNT{$email}\n";
> }
You could do one of two things here. First, you could just reverse the
hash so that your count was you key and the e-mail was your value. But
that only works if every e-mail address has a different number of posts
(otherwise they'll step all over each other).
So instead, build an intermediate hash with the key being the number of
posts and the value being an anonymous array of users who have that many
posts. If you keep sort in the first foreach, the results will even be
sorted.
my %CountSort;
foreach my $email (sort keys %COUNT) {
my $Posts = $COUNT{$email};
$CountSort{$Posts} = [] unless (defined($CountSort{$Posts}));
push(@{$CountSort{$Posts}}, $email);
}
And then, to print in decending order (most posts first) use the
following:
foreach my $Count (sort { $b <=> $a } keys %CountSort) {
foreach (@{$CountSort{$Count}}) {
print "$_ = $Count\n";
}
}
To print in ascending order, just switch the '$b <=> $a' thing with '$a
<=> $b'.
Hope this helps,
JH
------------------------------
Date: Thu, 21 Dec 2000 23:02:33 GMT
From: eggrock@my-deja.com
Subject: Re: Sorting hash
Message-Id: <91u265$5h$1@nnrp1.deja.com>
> Hi there,
>
> I made a script to check how many times a user has posted to mailing
> list. The problem is, how can I sort by "how many times they post" not
> by name? Here is the code...
>
Don't have time to test, but I think I remember this.
..assume %hash has values already entered
%cmp = %hash;
foreach $key (reverse sort byval keys %hash) {
print "$key: $hash{$key}\n";
}
#or (for the top 10 number of listings)
foreach $key ((reverse sort byvalue keys %hash) [0..10] ) {
print "$key: $hash{$key}\n";
}
sub byval() {
$cmp{$a} <=> $cmp{$b};
}
How this works I have no idea...
Sent via Deja.com
http://www.deja.com/
------------------------------
Date: Fri, 22 Dec 2000 01:38:37 GMT
From: miko@idocs.com
Subject: Re: Why are multiple zeroes true?
Message-Id: <91ubat$7kb$1@nnrp1.deja.com>
In article <3a3a3cc9@cs.colorado.edu>,
tchrist@perl.com (Tom Christiansen) wrote:
> It's cleanest to thing of there being just two false values in Perl:
> zero and the null string--thus, anything that reduces to one of
> these is itself false. I like thinking of it like this because
> it's easier to remember just 2 rules than the 4, 5, or even 6 that
> you often see people cite.
Well, that's exactly the reason that led to me expect "000" to be false:
it fits into the first rule of reducing to zero (as it does in a numeric
context). To make "0" false but "000" true you have use more
complicated rules.
But obviously, my original question has been answered: the existing
rules are obviously What Everyone Else Expects, so they're the best
rules.
-miko
--
Miko O'Sullivan
Author of The Mikodocs Guide to HTML
http://www.mikodocs.com/tags/
Sent via Deja.com
http://www.deja.com/
------------------------------
Date: Fri, 22 Dec 2000 01:42:44 GMT
From: miko@idocs.com
Subject: Re: Why are multiple zeroes true?
Message-Id: <91ubij$7nf$1@nnrp1.deja.com>
In article <x7zohygpod.fsf@home.sysarch.com>,
Uri Guttman <uri@sysarch.com> wrote:
> it all makes sense and you should trust larry's judgement on this.
> it has been this way for years and all experienced perl hackers
> know it and understand it. this question only comes from newbies
> who haven't learned enough perl yet.
I do trust Larry's judgement, but it's hardly the path to the best world
to never question things. As Larry says, "We question most of the
mantras around here periodically."
:-) and peace
-miko
--
Miko O'Sullivan
Author of The Mikodocs Guide to HTML
http://www.mikodocs.com/tags/
Sent via Deja.com
http://www.deja.com/
------------------------------
Date: 21 Dec 2000 18:56:51 -0700
From: tchrist@perl.com (Tom Christiansen)
Subject: Re: Why are multiple zeroes true?
Message-Id: <3a42b4e3@cs.colorado.edu>
In article <91ubat$7kb$1@nnrp1.deja.com>, <miko@idocs.com> wrote:
>Well, that's exactly the reason that led to me expect "000" to be false:
>it fits into the first rule of reducing to zero (as it does in a numeric
>context). To make "0" false but "000" true you have use more
>complicated rules.
>
>But obviously, my original question has been answered: the existing
>rules are obviously What Everyone Else Expects, so they're the best
>rules.
I'm not so sure that that logic is wholly unimpeachable. I also don't
think it happens to be the case.
In fact, I during the perl6 ruminations asked whether we didn't
want to seriously rethink this matter. I'm not sure whether it
made an RFC or not. I think that It was in my "PERL6STORM" thread,
where I wrote:
=item perl6storm #0052
Make "0" (more?) true so that people don't get surprised.
or
Make "0.00" (more?) false so that people don't get surprised.
--tom
------------------------------
Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 16 Sep 99)
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: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.
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 V9 Issue 5176
**************************************