[17777] in Perl-Users-Digest
Perl-Users Digest, Issue: 5197 Volume: 9
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Dec 26 00:05:31 2000
Date: Mon, 25 Dec 2000 21:05:10 -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: <977807110-v9-i5197@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Mon, 25 Dec 2000 Volume: 9 Number: 5197
Today's topics:
A better explanation of what I'm attempting <leekembel@hotmail.com>
Choose which DBM by trial and error? <RonaldWS@software-path.com>
Re: convert string to command? <leekembel@hotmail.com>
Re: convert string to command? <leekembel@hotmail.com>
Re: convert string to command? <uri@sysarch.com>
Re: Copy data from outside page <jhelman@wsb.com>
CPAN Eof error <johngros@Spam.bigpond.net.au>
Re: CPAN Eof error <bwalton@rochester.rr.com>
Re: Error Message with Perl Code <johngros@Spam.bigpond.net.au>
Re: Error Message with Perl Code (Martien Verbruggen)
Re: heredoc within heredoc <johnlin@chttl.com.tw>
Re: heredoc within heredoc (Martien Verbruggen)
How to accept a DCC connection? lynton@iname.com
Re: list first n lines of matching paragraphs <monty@primenet.com>
Re: Mail::Sendmail and HTML-messages (Create232)
Re: making unique list of words matching a pattern (Tad McClellan)
Re: making unique list of words matching a pattern <JD@Tallorno.net>
Re: my cgi scripts starts a download? urgent help neede (Create232)
Re: newbie randomize question <iltzu@sci.invalid>
Re: newbie randomize question (Tad McClellan)
Re: newbie randomize question (Tad McClellan)
Re: newbie randomize question <timallen449@my-deja.com>
Re: Parsing (Tad McClellan)
Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 26 Dec 2000 04:32:42 GMT
From: "Studio 51" <leekembel@hotmail.com>
Subject: A better explanation of what I'm attempting
Message-Id: <KbV16.152869$_5.34070669@news4.rdc1.on.home.com>
Ok, I have a database of directory names. Every entry has 2 important
columns:
NAME
PATH
It's set up like a directory structure, NAME is the name of the directory,
PATH is the path to it. So /usr/local/bin would be:
NAME = bin
PATH = /usr/local
I want to read the whole database into an array or a hash so it can be
sorted and printed. That's what I'm trying to do.
MERRY XMAS!
LKembel
------------------------------
Date: Mon, 25 Dec 2000 21:50:00 -0500
From: Ronald Schmidt <RonaldWS@software-path.com>
Subject: Choose which DBM by trial and error?
Message-Id: <3A480758.A8DF8355@software-path.com>
I am working on a programming that will try to deal with DBMs
generically. I want a user to be able to address a DBM file, preferably
without the need for the user to specify which kind of DBM file it is
(DB_File or NDBM_File etc.) Is it safe to approach this by trying to
tie the file O_RDONLY with several different DBM packages and see which
one gives a good return value from tie? If tie gives a good return code
O_RDONLY will it then be safe to tie the file name for read/write and do
updates? I have appended some sample code to illustrate the question.
Thanks,
Ronald Schmidt
The Software Path
use Fcntl;
use Getopt::Long;
use strict;
my (%avail_dbm, @sort_avail_dbm);
my (%dbm_opts);
my ($tie_class, $tie_rc);
my %h;
BEGIN {
foreach my $db_class (qw(DB_File GDBM_File NDBM_File SDBM_File
ZDBM_File)) {
eval {require "${db_class}.pm";};
unless ($@) {
import $db_class;
$avail_dbm{$db_class} = 1;
}
}
@sort_avail_dbm = sort keys %avail_dbm;
}
my @cmd_opts = map(lc $_, @sort_avail_dbm);
grep s/_file//, @cmd_opts;
&GetOptions(\%dbm_opts, @cmd_opts);
my $dbm_fname = shift || die "Expect path name of dbm file as
argument\n";
if (scalar(keys %dbm_opts) == 1) {
my ($selected_dbm) = keys %dbm_opts;
$tie_class = uc($selected_dbm) . '_File';
unless($tie_rc = tie(%h, $tie_class, $dbm_fname, O_RDONLY, 0)) {
die "Could not dbm with $tie_class: $!";
}
}
elsif (scalar(keys %dbm_opts) > 1) {
die "Must select just one dbm package for use with dbm file\n";
}
else {
print "No specified dbm type. Will guess (experimental).\n",
"Type no or ^C to exit, enter to continue? ";
exit(-1) if (<> =~ /no/i);
foreach my $test_class (@sort_avail_dbm) {
if ($tie_rc = tie(%h, $test_class, $dbm_fname, O_RDONLY, 0)){
$tie_class = $test_class;
last;
}
}
unless ($tie_rc) {
die "Could not open dbm file with any of ",
join(', ', @sort_avail_dbm), ".\n";
}
}
untie %h;
undef $tie_rc;
if (tie(%h, $tie_class, $dbm_fname, O_RDWR, 0777)) {
$h{'test_key'} = 'test_value';
}
untie %h;
------------------------------
Date: Tue, 26 Dec 2000 04:23:57 GMT
From: "Studio 51" <leekembel@hotmail.com>
Subject: Re: convert string to command?
Message-Id: <x3V16.152864$_5.34062734@news4.rdc1.on.home.com>
"Tad McClellan" <tadmc@metronet.com> wrote in message
news:slrn94elmv.98d.tadmc@magna.metronet.com...
> Can we then assume that you have looked at the standard Perl docs
> that are germane to multi-level data structures?
Those and many more.
> I'm not seeing what the problem is. Can you eventually get to
> having all of the keys in variables?
Yes, but some of them are only accessable from within it's own block. (see
code at bottom)
> eval() is dangerous and should be avoided when possible.
I've come to agree, and I've come up with a workaround that doesn't use
eval() below.
> Enable warnings.
I have.
> Use strictures.
I did, but it caused another problem I couldn't get around so I took them
off. The following doesn't work under use strict:
&{$field{'action'}};
I have a name stored in $field{'action'}, and I want to run a subroutine
with the same name. I have a LOT of possible values for for
$field{'action'}, and the only way I could figure to handle this with use
strict was a gigantic if..then..elsif mess that looks for every possible
value of $field{'action'}. If there are better ways I'd love to hear them!
> If you are working with multi-level data structures, know and use
> the Data::Dumper module for debugging:
Thanks, I checked it out and it makes my whole hash very readable, and it
showed me what I already knew. But this is a useful tool anyway, one that I
wasn't aware of before.
> It helps us help you if we have code *that we can run* that illustrates
> the problem that you are having.
Unless you have access to an SQL database with similar structure you can't
run my code.
> I am sure what you wanted to say was "for example" (e.g.).
I am sure you know what I meant.
> > eval ( "\$cat".$dirref."{'auctions'} = {"auctions" =>
$auctions}" );
> ^^^^^^^
>
> It looks to me like you have a variable part of the *name* of
> the hash, implying that there is more than one such hash.
It is a hash of hashes. i.e. a hash that contains other hashes, that contain
other hashes...
> That (adding another "level" of hash) is a classic way of
> avoiding evil symbolic references.
The entire concept of references in perl escapes me. I've spent a lot of
time looking for explanations of references online, the only ones I've found
are the perldoc ones you mentioned, and they assume a level of understanding
I don't have. Any "references in perl basics" links would be appreciated.
> Since everybody with a lick of sense always runs with strictures
> turned on, you won't be allowed to even _aim_ the gun at your foot,
> avoiding the potential for experiencing a painful figurative wound.
I'd really like to use strictures, I want to do this right. But I can't
figure out the problem with my subroutine (above). I did turn strictures on
and remove the bit it didn't like, and it ran ok.
> >Or are
> >there better ways to read this category structure into memory?
> This seems likely in the extreme...
I was hoping for a hint as to what those ways might be, not just a
confirmation of what I already knew.
I'm sure you don't want to whole 400+ lines of code in my program, so below
is the part that is the trouble. It prints out:
Antiques: 0
Fiction: 0
Antiques:
Fiction: 0
In other words (i.e.) $cat{'Books'}{'Antiques'}{'auctions'} exists within
the "for each" block, and not outside it. Also my data is structured in the
SQL server like this:
NAME | PATH | na | auctions
Antiques | /Books | | 0
Fiction | /Books | | 0
&connect; #connects to MySQL
$sql = "select * from categories order by name";
$sth = $dbh->prepare($sql);
$sth->execute() || die $sth->errstr;
@arrayref = $sth->fetchall_arrayref;
foreach $rowref (@arrayref) {
foreach $row (@$rowref) {
$name = @$row[0];
$path = @$row[1];
$auctions = @$row[3];
if ($path eq "") {
$cat{$name} = {auctions => $auctions};
} else {
my @path = split ('/', $path);
shift @path;
#This next part seems real messy, which is why I was using eval() before
if (@path == 1) {
$cat{$path[0]}{$name} = {auctions => $auctions};
} elsif (@path == 2) {
$cat{$path[0]}{$path[1]}{$name} = {auctions => $auctions};
} elsif (@path == 3) {
$cat{$path[0]}{$path[1]}{$path[2]}{$name} = {auctions => $auctions};
} elsif (@path == 4) {
$cat{$path[0]}{$path[1]}{$path[2]}{$path[3]}{$name} = {auctions =>
$auctions};
} elsif (@path == 5) {
$cat{$path[0]}{$path[1]}{$path[2]}{$path[3]}{$path[4]}{$name} =
{auctions => $auctions};
}
if ($name eq "Antiques") {
print "Antiques: ".$cat{'Books'}{'Antiques'}{'auctions'}."<BR>";
}
if ($name eq "Fiction") {
print "Fiction: ".$cat{'Books'}{'Fiction'}{'auctions'}."<BR>";
}
}
}
}
print "Antiques: ".$cat{'Books'}{'Antiques'}{'auctions'}."<BR>";
print "Fiction: ".$cat{'Books'}{'Fiction'}{'auctions'}."<BR>";
&disconnect;
LKembel
------------------------------
Date: Tue, 26 Dec 2000 04:27:59 GMT
From: "Studio 51" <leekembel@hotmail.com>
Subject: Re: convert string to command?
Message-Id: <j7V16.152866$_5.34067237@news4.rdc1.on.home.com>
"Walt Mankowski" <waltman@netaxs.com> wrote in message
news:m3ito8sdpa.fsf@netaxs.com...
> Instead of storing your data as a hash of hashes, try storing it as a
> reference to a hash of hashes. E.g., instead of $cat{foo}{bar}{baz},
> use $cat->{foo}{{bar}{baz}. Then you can do something like this:
>
> @path = split ('/', $path);
> $catref = $cat;
I don't have $cat in my program, I have %cat. ?
> foreach $dir (@path) {
> $catref = $catref->{$dir};
> }
@path contains the entire path, in other words /$path[0]/$path[1]/$path[2].
It would seem the above would only work if the path where only 1 deep?
Or maybe I'm wrong, I have a poor understanding of references, if you have
any links to tutorials on this subject they'd b appreciated (I read the the
applicable perldocs).
LKembel
------------------------------
Date: Tue, 26 Dec 2000 05:03:26 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: convert string to command?
Message-Id: <x7puifbwpu.fsf@home.sysarch.com>
>>>>> "S5" == Studio 51 <leekembel@hotmail.com> writes:
S5> I did, but it caused another problem I couldn't get around so I took them
S5> off. The following doesn't work under use strict:
S5> &{$field{'action'}};
so don't do that.
S5> I have a name stored in $field{'action'}, and I want to run a
S5> subroutine with the same name. I have a LOT of possible values for
S5> for $field{'action'}, and the only way I could figure to handle
S5> this with use strict was a gigantic if..then..elsif mess that
S5> looks for every possible value of $field{'action'}. If there are
S5> better ways I'd love to hear them!
use a dispatch table which is a hash with keys of your function names
and values of references their associated subs. then you don't have
dangerous symbolic refs. don't try to bypass strict unless you know what
you are doing.
S5> The entire concept of references in perl escapes me. I've spent a
S5> lot of time looking for explanations of references online, the
S5> only ones I've found are the perldoc ones you mentioned, and they
S5> assume a level of understanding I don't have. Any "references in
S5> perl basics" links would be appreciated.
there are many docs in the perl man pages on refs. have you read them
all?
perlref
perlreftut
perllol
perldsc
the perl cookbook has some good stuff on them too.
S5> I'd really like to use strictures, I want to do this right. But I
S5> can't figure out the problem with my subroutine (above). I did
S5> turn strictures on and remove the bit it didn't like, and it ran
S5> ok.
see my comments above.
S5> #This next part seems real messy, which is why I was using eval() before
S5> if (@path == 1) {
S5> $cat{$path[0]}{$name} = {auctions => $auctions};
S5> } elsif (@path == 2) {
S5> $cat{$path[0]}{$path[1]}{$name} = {auctions => $auctions};
S5> } elsif (@path == 3) {
S5> $cat{$path[0]}{$path[1]}{$path[2]}{$name} = {auctions => $auctions};
S5> } elsif (@path == 4) {
S5> $cat{$path[0]}{$path[1]}{$path[2]}{$path[3]}{$name} = {auctions =>
S5> $auctions};
S5> } elsif (@path == 5) {
S5> $cat{$path[0]}{$path[1]}{$path[2]}{$path[3]}{$path[4]}{$name} =
S5> {auctions => $auctions};
S5> }
gawd, that is awful looking.
there are many ways of improving that. here is some rough and untested
code:
my $path_ref = \%cat ;
while ( my $part = shift @path ) {
$path_ref = \$path_ref->{$part} ;
}
$path_ref->{$name} = { 'auctions' => $auctions } ;
you just have to stop thinking eval and if/else like you do and it all
becomes clear. refs are good for you.
uri
--
Uri Guttman --------- uri@sysarch.com ---------- http://www.sysarch.com
SYStems ARCHitecture, Software Engineering, Perl, Internet, UNIX Consulting
The Perl Books Page ----------- http://www.sysarch.com/cgi-bin/perl_books
The Best Search Engine on the Net ---------- http://www.northernlight.com
------------------------------
Date: Tue, 26 Dec 2000 03:43:08 GMT
From: Jeff Helman <jhelman@wsb.com>
Subject: Re: Copy data from outside page
Message-Id: <3A4813E9.CFFF2A1C@wsb.com>
Tad McClellan wrote:
> Victor <a@a> wrote:
> >
> >Could anyone teach me how to use cgi to copy information/data from an
> ^^^^^^^
> >outside html page.
>
> I'll show you how to do it *without* CGI though:
>
> use LWP::Simple;
> my $weather = get 'http://weather.yahoo.com/forecast/Hong_Kong_CI_c.html';
See Tad's answer on how to do this. You have, of course, received
permission to do this from the appropriate content provider, right? You
do realize that people do put a great deal of time and money into
generating such information? And you wouldn't want to violate a
copyright and get yourself sued...
JH
------------------------------
Date: Tue, 26 Dec 2000 02:17:57 GMT
From: "John Boy Walton" <johngros@Spam.bigpond.net.au>
Subject: CPAN Eof error
Message-Id: <pdT16.32437$xW4.255486@news-server.bigpond.net.au>
I am trying to use CPAN to update all the modules I have installed the CPAN
docs say
perl -MCPAN -e 'CPAN::Shell->install(CPAN::Shell->r)'
will do this.
But I am getting
Can't find string terminator "'" anywhere before EOF at -e line 1.
This is reminiscent of the EOF error people get when using the print
<<"HTMLDATA";
I have tried adding a space at the end of the line to no avail. Does anyone
know what I am doing wrong?
------------------------------
Date: Tue, 26 Dec 2000 04:24:38 GMT
From: Bob Walton <bwalton@rochester.rr.com>
Subject: Re: CPAN Eof error
Message-Id: <3A481DFB.DB8BA8B5@rochester.rr.com>
John Boy Walton wrote:
>
> I am trying to use CPAN to update all the modules I have installed the CPAN
> docs say
> perl -MCPAN -e 'CPAN::Shell->install(CPAN::Shell->r)'
> will do this.
>
> But I am getting
> Can't find string terminator "'" anywhere before EOF at -e line 1.
>
> This is reminiscent of the EOF error people get when using the print
> <<"HTMLDATA";
> I have tried adding a space at the end of the line to no avail. Does anyone
> know what I am doing wrong?
Bet you're trying to do that on Windoze, right? If so, you should
probably be getting your modules from ActiveState via PPM (note: only a
limited selection is available). You might get something out of CPAN,
though, if you change your quoting to be compatible with Windoze (that
is, " instead of ' ). Note that CPAN is pretty much oriented toward Unix
type systems. Running Linux instead of Windoze would make it work.
--
Bob Walton
------------------------------
Date: Tue, 26 Dec 2000 01:25:55 GMT
From: "John Boy Walton" <johngros@Spam.bigpond.net.au>
Subject: Re: Error Message with Perl Code
Message-Id: <DsS16.32329$xW4.255011@news-server.bigpond.net.au>
"Edd" <edd@texscene.com> wrote in message
news:3a47b199@news-uk.onetel.net.uk...
> When I run my perl code which sends email, I get the following error
> message. What does this message mean? Can somebody help please.
>
>
> Name "main::to" used only once: possible typo at line:
> $to="info\@testing.com";
> Name "main::to" used only once: possible typo at line:
> $from="info\@testing.com";
It means the variables $to and $from are only used once.
Perl warns you about this because it is a sign there may be an error. It may
still run fine, does the mail get sent successfully? If so just ignore the
warning.
------------------------------
Date: Tue, 26 Dec 2000 13:42:34 +1100
From: mgjv@tradingpost.com.au (Martien Verbruggen)
Subject: Re: Error Message with Perl Code
Message-Id: <slrn94g1cq.8i7.mgjv@martien.heliotrope.home>
On Tue, 26 Dec 2000 01:25:55 GMT,
John Boy Walton <johngros@Spam.bigpond.net.au> wrote:
>
> "Edd" <edd@texscene.com> wrote in message
> news:3a47b199@news-uk.onetel.net.uk...
>> When I run my perl code which sends email, I get the following error
>> message. What does this message mean? Can somebody help please.
>>
>>
>> Name "main::to" used only once: possible typo at line:
>> $to="info\@testing.com";
>> Name "main::to" used only once: possible typo at line:
>> $from="info\@testing.com";
>
> It means the variables $to and $from are only used once.
No, it doesn't. The warnings, both, refer to $main::to, but the second
line quoted doesn't contain that variable. The OP is not showing us the
real output. Besides that, the program that the OP presents uses
$main:to in more than one place, so the error isn't from the program the
presented.
I think the OP should be more accurate in reporting these things. As it
stands now, there is nothing anyone can do to help, apart from pointing
out ow the program can be improved. The error messages are clearly not
from the program presented, and are not even consistent with themselves.
Martien
--
Martien Verbruggen | Since light travels faster than
Interactive Media Division | sound, isn't that why some people
Commercial Dynamics Pty. Ltd. | appear bright until you hear them
NSW, Australia | speak?
------------------------------
Date: Tue, 26 Dec 2000 11:16:51 +0800
From: "John Lin" <johnlin@chttl.com.tw>
Subject: Re: heredoc within heredoc
Message-Id: <9292n9$d0f@netnews.hinet.net>
"Martien Verbruggen" wrote
> Do you have an actual need for this,
Well the actual case is that my code
parse(<<HTML,<<RULE,<<EMAIL);
works, but
parse(
<<HTML, # my boss asked me to separate
<<RULE, # them into 3 lines and add
<<EMAIL # comments to explain them
);
fails. Of course I have fixed it, but uglier.
> or are you just academically interested?
Yes. :)
Thank you.
John Lin
------------------------------
Date: Tue, 26 Dec 2000 15:41:13 +1100
From: mgjv@tradingpost.com.au (Martien Verbruggen)
Subject: Re: heredoc within heredoc
Message-Id: <slrn94g8b9.8i7.mgjv@martien.heliotrope.home>
On Tue, 26 Dec 2000 11:16:51 +0800,
John Lin <johnlin@chttl.com.tw> wrote:
> "Martien Verbruggen" wrote
>
>> Do you have an actual need for this,
>
> Well the actual case is that my code
>
> parse(<<HTML,<<RULE,<<EMAIL);
>
> works, but
>
> parse(
> <<HTML, # my boss asked me to separate
> <<RULE, # them into 3 lines and add
> <<EMAIL # comments to explain them
> );
That is interesting. I didn't know about this particular oddness.
It is explainable though, although not entirely [1]. The documentation
states that:
A line-oriented form of quoting is based on the shell
"here-document" syntax. Following a `<<' you specify a
string to terminate the quoted material, and all lines
following the current line down to the terminating string
are the value of the item.
If the start of the various here-docs is on the same line, they should
all start on the line after that. If you split it out over multiple
lines, that breaks.
The fact that you can stack multiple ones on one line is also
documented, albeit only by example. From perldata:
print <<"foo", <<"bar"; # you can stack them
I said foo.
foo
I said bar.
bar
myfunc(<<"THIS", 23, <<'THAT');
Here's a line
or two.
THIS
and here's another.
THAT
Impied is that there is a special case made for stacked here-docs on one
line. There is no special case for stacked here-docs in a single
statement, split over several lines. This is just one of these places
where perl's DWIMmery does something partways. I doubt it would be easy
to allow the special case for multiple here-docs within the same
statement to work as it does within the same line.
It is not implausible to call this a bug. The fact that multiple
here-docs on one line works is one of those special cases that Perl has
so many of. That this special case has limitations is not that great.
You could consider writing up a bug report, and ask them to at least fix
the documentation, or extend the ehaviour of here-docs to allow multiple
here-docs in the same statement (this last one is probably not trivial
to implement).
> fails. Of course I have fixed it, but uglier.
I guess the neatest way to do this is to have a comment before the parse()
statement, and an uglier one is to do what I suggested in an earlier
post, and inline the here-docs in the sub call.
Martien
[1] Not entirely because the documentation doesn't explicilty explain
that, and how, here-docs can be stacked. It just gives two examples. It
also doesn't explain explicitly what happens when here-docs are stacked,
and in fact, what happens is contradictory to the general explanation of
what a here-doc does. Stated is that a here-doc's content starts at the
next line, but in the case of stacked here-docs that is only true for
the left-most one. The others start after the terminating string of the
previous here-doc.
--
Martien Verbruggen |
Interactive Media Division | +++ Out of Cheese Error +++ Reinstall
Commercial Dynamics Pty. Ltd. | Universe and Reboot +++
NSW, Australia |
------------------------------
Date: Tue, 26 Dec 2000 01:50:21 GMT
From: lynton@iname.com
Subject: How to accept a DCC connection?
Message-Id: <928tgs$fs7$1@nnrp1.deja.com>
When someone attempts to make a connection via DCC how to accept their
connection?
I have been using the script 'irctest' that came with module
Net::IRC
Is there a way to accept the DCC connection? (with or, perhaps without
using the Net::IRC module)
The following is the part of the 'irctest' file that responds to
an attempt for a DCC connection from another party, but whenever
someone tries to connect it gives the error message 'Unknown DCC type: '
Any solution?
# What to do when we receive a DCC SEND or CHAT request.
sub on_dcc {
my ($self, $event) = @_;
my $type = ($event->args)[1];
if (uc($type) eq 'SEND') {
open TEST, ">/tmp/net-irc.dcctest"
or do { warn "Can't open test file: $!"; return; };
$self->new_get($event, \*TEST);
print "Saving incoming DCC SEND to /tmp/net-irc.dcctest\n";
} elsif(uc($type) eq 'CHAT') {
$self->new_chat($event);
} else {
print STDERR ("Unknown DCC type: " . $type);
}
}
Sent via Deja.com
http://www.deja.com/
------------------------------
Date: 26 Dec 2000 04:30:44 GMT
From: Jim Monty <monty@primenet.com>
Subject: Re: list first n lines of matching paragraphs
Message-Id: <9296tk$flp$1@nnrp1.phx.gblx.net>
Jim Monty <monty@primenet.com> wrote:
> Vivekvr <vivekvr@aol.com> wrote:
> > Has anyone written a Perl script to list the first n (or all, if
> > no number specified) lines of all the paragraphs in a file which
> > contain a specified regular expression. Paragraphs are separated
> > by blank lines (perhaps spaces or tabs, but no alphanumeric
> > characters).
> >
> > I have a list of references with abstracts, separated by spaces,
> > and I would like, for example to list the first 4 lines of all
> > references containing "Smith".
>
> Carving up multiline records is trivial in both awk and Perl:
>
> $ cat records
> Adam Smith
> 1234 Wall St., Apt. 5C
> New York, NY 10021
> 212 555-4321
>
> David W. Copperfield
> 221 Dickens Lane
> Monterey, CA 93940
> 408 555-0041
> work phone 408 555-6532
> Mary, birthday January 30
>
> Canadian Consulate
> 555 Fifth Ave
> New York, NY
> 212 586-2400
> $ awk 'BEGIN { RS = ""; FS = "\n" } /Smith/ { print $1 }' records
> Adam Smith
> $ perl -ne 'BEGIN { $/ = ""; $\ = "\n" }
> > print +(split /\n/)[0] if /Smith/' records
> Adam Smith
> $ awk 'BEGIN { RS = ""; FS = "\n" } /New York/ { print $1, $2 }' records
> Adam Smith 1234 Wall St., Apt. 5C
> Canadian Consulate 555 Fifth Ave
> $ perl -ne 'BEGIN { $/ = ""; $\ = "\n"; $, = " " }
> > print +(split /\n/)[0,1] if /New York/' records
> Adam Smith 1234 Wall St., Apt. 5C
> Canadian Consulate 555 Fifth Ave
> $
>
> [This example data comes from p. 83 of _The AWK Programming Language_
> (ISBN 0-201-07981-X).]
And I should've mentioned that you can use Perl's autosplit mode,
too:
$ perl -anF'\n' -e 'BEGIN { $/ = "";
> $\ = "\n";
> $, = "; "; }
> print @F[0..2] if /\bSmith\b/;
> ' records
Adam Smith; 1234 Wall St., Apt. 5C; New York, NY 10021
$
--
Jim Monty
monty@primenet.com
Tempe, Arizona USA
------------------------------
Date: 26 Dec 2000 02:44:42 GMT
From: create232@aol.com (Create232)
Subject: Re: Mail::Sendmail and HTML-messages
Message-Id: <20001225214442.18096.00012739@ng-fi1.aol.com>
There is a site out there with a bunch of scripts that do this. Some are even
free, it is www.worldwidecreations.com
Hope it helps
------------------------------
Date: Mon, 25 Dec 2000 15:34:46 -0500
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: making unique list of words matching a pattern
Message-Id: <slrn94fbr6.a92.tadmc@magna.metronet.com>
Ben Okopnik <fuzzybear@pocketmail.com> wrote:
>>Golf, anyone?
>
>Odin~$> perldoc Abigail::golf
>No documentation found for "Abigail::golf"
>Odin~$>
>
>Rats, it's undocumented.
It is documented (but it does not "belong" to Abigail (some
may argue that though)).
"Welcome to The Perl Golf Page":
http://www.sysarch.com/perl/golf/
(this was the very first hit from a search for "Perl golf" at Google...)
><Grin> Sorry, I miss your meaning.
^^^^^^
You whiffed (in golf parlance).
>> perl -walne'map/\.cpp$/&&print,@F' file
>
>Whew. Left me behind; I'm just starting to understand 'map', and that went
>clean over...
Unraveling it is a truly great way of picking up some more Perl.
(and even, some would say, some _bad_ Perl)
Anything that helps you win is what you use. Sometimes you see
more succinct ways of "saying something" in Perl. Sometimes
the ways are just too darn succinct for easy maintenance in
real programs.
But we're not talking about real programs. We're playing golf.
1) figure out what all the switches do (perldoc perlrun)
2) spaces (strokes) make you lose. Put them back in.
map /\.cpp$/ && print, @F
3) hmmm. Must be using the "map EXPR,LIST" form (perldoc -f map),
'cause I don't see a BLOCK. Where is the EXPR part?
map ( /\.cpp$/ && print), @F
4) note the "funny way to write" bit in perlfunc. rewrite it
the "serious" way.
5) understand how the short-circuit behavior of the && operator
is used as a flow control structure, and now how the program
works is clear. (if not, go to step one, check the docs again.
if you get to step #5 three times, post a question to clpmisc :-)
--
Tad McClellan SGML consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: Tue, 26 Dec 2000 01:39:33 +0000
From: John Delacour <JD@Tallorno.net>
Subject: Re: making unique list of words matching a pattern
Message-Id: <v03130301b66da5b27419@e>
At 1:12 am +0000 25/12/00, Vivekvr wrote:
>For example, in the input
>
>boy cat girl
>foo.c boo.cpp goo.c hoo.cpp
>
>I want to list strings matching "*.cpp", which would be
>
>boo.cpp
>hoo.cpp
I think this will do it (works for any filename containing
word characters)
@list = split /\s/, qq~
boy cat girl 1_2_3.cpp
\tfile123.cpp foo.c boo.cpp goo.c hoo.cpp
~;
@hits = grep(/[0-9A-Za-z_]+\.cpp$/, @list);
print join $/, @hits;
JD
------------------------------
Date: 26 Dec 2000 02:50:23 GMT
From: create232@aol.com (Create232)
Subject: Re: my cgi scripts starts a download? urgent help needed
Message-Id: <20001225215023.18096.00012740@ng-fi1.aol.com>
This sounds like a permissions problem. You must make sure the script(s) are
set as executable..
------------------------------
Date: 25 Dec 2000 23:16:02 GMT
From: Ilmari Karonen <iltzu@sci.invalid>
Subject: Re: newbie randomize question
Message-Id: <977785721.3515@itz.pp.sci.fi>
In article <Pine.GSO.4.21.0012251632320.25445-100000@crusoe.crusoe.net>, Jeff Pinyan wrote:
>
>It is allowed (especially since a lot of the code in the cookbook can be
>found in other places, like the FAQ). To not be allowed to use code from
>a book created expressly for the purpose of providing code to use in
>certain situations would be silly. And since much code is merely an
Agreed. The FAQ, at least, states this explicitly. I'd rather assume
the book would do so as well.
>implementation of an algorithm in a given language, I don't think you
>could claim ownership to a bit of Perl code that performs a Fisher-Yates
>shuffle on an array.
This, however, might not be true. A particular piece of source code
could well be copyrighted even if the task it accomplishes is trivial.
This should be particularly true with Perl, which offers so many ways
to do any given task. One could certainly claim copyright to a JAPH,
at least a sufficiently elaborate one.
--
Ilmari Karonen -- http://www.sci.fi/~iltzu/
"Get real! This is a discussion group, not a helpdesk. You post
something, we discuss its implications. If the discussion happens to
answer a question you've asked, that's incidental." -- nobull in clpm
------------------------------
Date: Mon, 25 Dec 2000 15:55:50 -0500
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: newbie randomize question
Message-Id: <slrn94fd2m.a92.tadmc@magna.metronet.com>
tim allen <timallen449@my-deja.com> wrote:
>In article <3A479F93.35215236@qwest.net>,
> Jimmy <studdstr@qwest.net> wrote:
>> I am looking to display the lines from a file, in a random order.
>Looks like your idea of using an array is a good one. The Perl Cookbook
>(Tom Christiansen & Nathan Torkinton)
You have never noticed the last paragraph in each of the
perlfaq[1-9].pod files?
Those are the guys who wrote the Perl FAQs.
Wonder if there is anything about "random" in there...
>I don't know if I'm allowed by copyright to send this code out on this
>forum. Is that kosher?
BTW, there is a newsgroup for discussing intellectual property rights:
misc.int-property
--
Tad McClellan SGML consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: Mon, 25 Dec 2000 15:57:55 -0500
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: newbie randomize question
Message-Id: <slrn94fd6j.a92.tadmc@magna.metronet.com>
Jimmy <studdstr@qwest.net> wrote:
>I am looking to display the lines from a file, in a random order. I have
>not found anything on how I should do this.
^^^^^^^^^
Most of us are going to find it awfully hard to believe you...
perldoc -q random
"How do I shuffle an array randomly?"
"How do I select a random element from an array?"
"How do I select a random line from a file?"
>Could someone point me in the right direction?
The right direction would be to check the Perl FAQ *before*
posting to the Perl newsgroup.
--
Tad McClellan SGML consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: Tue, 26 Dec 2000 03:19:24 GMT
From: tim allen <timallen449@my-deja.com>
Subject: Re: newbie randomize question
Message-Id: <9292nq$jbb$1@nnrp1.deja.com>
In article <977785721.3515@itz.pp.sci.fi>,
Ilmari Karonen <usenet11316@itz.pp.sci.fi> wrote:
> In article <Pine.GSO.4.21.0012251632320.25445-
100000@crusoe.crusoe.net>, Jeff Pinyan wrote:
> >
> >It is allowed (especially since a lot of the code in the cookbook
can be
> >found in other places, like the FAQ).
>
> Agreed. The FAQ, at least, states this explicitly. I'd rather assume
> the book would do so as well.
Al right, it sounds like it is okay to post this (my sincere apologies
to Messrs Christiansen and Torkington if not). I will try to put it
exactly as it is in the book, but I do not have perl at the machine
where I'm typing from, so if I make an error, I can't detect it. Note
that not all the code is there (e.g. you have to put in code to open
the INPUT and OUTPUT files). Here goes:
sub fisher_yates_shuffle {
my $array = shift;
my $i;
for ($i = @$array; --$i; ) {
my $j = int rand ($i+1);
next if $i == $j;
@$array[$i,$j] = @$array[$j,$i];
}
}
while (<INPUT>) {
push (@lines, $_);
}
fisher_yates_shuffle(\@lines);
foreach (@lines) {
print OUTPUT $_;
}
Sent via Deja.com
http://www.deja.com/
------------------------------
Date: Mon, 25 Dec 2000 15:39:16 -0500
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: Parsing
Message-Id: <slrn94fc3k.a92.tadmc@magna.metronet.com>
Simon Stiefel <SiStie@nuclear-network.de> wrote:
>
>I want to parse the output of "finger".
>So, uhm, does anybody have an idea how to realize that? =)
perldoc -f unpack
--
Tad McClellan SGML consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
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 5197
**************************************