[19193] in Perl-Users-Digest
Perl-Users Digest, Issue: 1388 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jul 26 21:06:15 2001
Date: Thu, 26 Jul 2001 18:05:09 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <996195908-v10-i1388@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Thu, 26 Jul 2001 Volume: 10 Number: 1388
Today's topics:
Re: (long) advice needed: how robust is this script ? a (Abigail)
Re: \nhelp <mbudash@sonic.net>
Re: \nhelp <jtjohnston@courrier.usherb.ca>
Re: \nhelp <bart.lateur@skynet.be>
Re: \nhelp <mbudash@sonic.net>
Re: \nhelp <jtjohnston@courrier.usherb.ca>
a* vs .* regex behavior question <gamma@mintaka.iern.disa.mil>
Re: Apache and mod_perl? <ilya@martynov.org>
Re: Apache and mod_perl? (Mike Erickson)
Re: argv or getopt on Windows 2000 pro (Eric Bohlman)
Re: cgi variables in French (Abigail)
Re: CGI:standard <greenbd@u.washington.edu>
Re: CGI:standard <mbudash@sonic.net>
Re: CGI:standard (Eric Bohlman)
Re: DBD:ODBC, Openlink->MSSQL, and LongReadLen <sommar@algonet.se>
Re: don't laugh (case usage for variables) (Abigail)
Re: Extract the relative sorting of items from multiple <bart.lateur@skynet.be>
FAQ: How do I remove consecutive pairs of characters? <faq@denver.pm.org>
Re: Finding a word in a sorted list. (Logan Shaw)
Re: sortlen -- filter to sort text by line length <mischief@velma.motion.net>
Stripping extention from a filename in a string <lapenta_jm@yahoo.com>
Re: What's the idiomatic way for this? slash@dot.c.o.m.org
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 26 Jul 2001 23:30:18 GMT
From: abigail@foad.org (Abigail)
Subject: Re: (long) advice needed: how robust is this script ? and other questions :)
Message-Id: <slrn9m1a1m.d4m.abigail@alexandra.xs4all.nl>
Kev (NOSPAMlekkerinsydney@ozemail.com.au) wrote on MMDCCCLXXX September
MCMXCIII in <URL:news:3b5783f0.1173336306@news.ozemail.com.au>:
-:
-: My questions are the following:
-:
-: 1. Is this script robust so that
-: 10?100?1000?10000? people accessing it at the same
-: tiime wouldn't have a probem using the script ?
-:
-: # Version 1.3 for NT with BLAT
A 1000 concurrent accesses? On *NT*?
Well, I guess the OS will go belly up long before your program croaks.
Abigail
--
sub _ {$_ = shift and y/b-yB-Y/a-yB-Y/ xor !@ _?
exit print :
print and push @_ => shift and goto &{(caller (0)) [3]}}
split // => "KsvQtbuf fbsodpmu\ni flsI " xor & _
------------------------------
Date: Thu, 26 Jul 2001 22:47:14 GMT
From: Michael Budash <mbudash@sonic.net>
Subject: Re: \nhelp
Message-Id: <mbudash-8E05A9.15471726072001@news.sonic.net>
In article <3B608E68.B9EAD097@courrier.usherb.ca>, jtjohnston
<jtjohnston@courrier.usherb.ca> wrote:
> Hi,
> I have a text file I use as a database. I have many instances of lines
> starting with "&&4;" Can someone show me how to re-attach the "&&4;" to
> the previous line?
>
> See following example:
>
> ---snip---
> Record1¶field2¶field3¶field4¶field5¶field6¶field7¶
> Record2¶field2¶field3¶
> &&4; field4¶field5¶field6¶field7¶
> Record3¶field2¶field3¶field4¶field5¶field6¶field7¶
> ---snip---
>
> Becomes:
>
> ----snip---
> Record1¶field2¶field3¶field4¶field5¶field6¶field7¶
> Record2¶field2¶field3¶&&4; field4¶field5¶field6¶field7¶
> Record3¶field2¶field3¶field4¶field5¶field6¶field7¶
> ----snip---
>
>
> What do I do here?
>
> if ( ! open(INPUT1,"<$file1") ) {
> die("Can't open file \"$file1\" : $!\n");
> }
> #$buffer1 = <INPUT1>
> #$ = ~s///g;
>
> close INPUT1;
> }
>
>
> Thanks,
if ( ! open(INPUT1,"<$file1") ) {
die("Can't open file \"$file1\" : $!\n");
}
{ local $/; $buffer1 = <INPUT1>; }
$buffer1 = ~s/\n(&&4;)/$1/g;
close INPUT1;
hth-
--
Michael Budash ~~~~~~~~~~ mbudash@sonic.net
------------------------------
Date: Thu, 26 Jul 2001 22:55:53 GMT
From: jtjohnston <jtjohnston@courrier.usherb.ca>
Subject: Re: \nhelp
Message-Id: <3B60A09D.A4D9013C@courrier.usherb.ca>
> if ( ! open(INPUT1,"<$file1") ) {
> die("Can't open file \"$file1\" : $!\n");
> }
> { local $/; $buffer1 = <INPUT1>; }
> $buffer1 = ~s/\n(&&4;)/$1/g;
> close INPUT1;
Great thanks. I don't have a manual handy. Where does $1 come from? What does
it do? Also why the brackets around the (&&4;)
Thanks,
John
------------------------------
Date: Thu, 26 Jul 2001 22:56:30 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: \nhelp
Message-Id: <eq71mtgg2nr3cr1eiqv0u6lbfjcqu4mlb0@4ax.com>
jtjohnston wrote:
>I have a text file I use as a database. I have many instances of lines
>starting with "&&4;" Can someone show me how to re-attach the "&&4;" to
>the previous line?
>
>See following example:
>
>---snip---
>Record1¶field2¶field3¶field4¶field5¶field6¶field7¶
>Record2¶field2¶field3¶
>&&4; field4¶field5¶field6¶field7¶
>Record3¶field2¶field3¶field4¶field5¶field6¶field7¶
>---snip---
>
>Becomes:
>
>----snip---
>Record1¶field2¶field3¶field4¶field5¶field6¶field7¶
>Record2¶field2¶field3¶&&4; field4¶field5¶field6¶field7¶
>Record3¶field2¶field3¶field4¶field5¶field6¶field7¶
>----snip---
>
>What do I do here?
Don't print the newline yet. Print a newline if no '&&4;' is found (and
deleted), or if you're at the end of the file.
while(<>) {
chomp;
s/^&&4;// or $.==1 or print "\n";
print;
}
print "\n";
--
Bart.
------------------------------
Date: Thu, 26 Jul 2001 23:53:18 GMT
From: Michael Budash <mbudash@sonic.net>
Subject: Re: \nhelp
Message-Id: <mbudash-D3EDCA.16532126072001@news.sonic.net>
In article <3B60A09D.A4D9013C@courrier.usherb.ca>, jtjohnston
<jtjohnston@courrier.usherb.ca> wrote:
> > if ( ! open(INPUT1,"<$file1") ) {
> > die("Can't open file \"$file1\" : $!\n");
> > }
> > { local $/; $buffer1 = <INPUT1>; }
> > $buffer1 = ~s/\n(&&4;)/$1/g;
> > close INPUT1;
>
> Great thanks. I don't have a manual handy. Where does $1 come from? What
> does
> it do? Also why the brackets around the (&&4;)
>
don't thank me until it works! (or does it already?)
anyway, $1 is called a 'backreference', referring back to the first
(hence the '1') parenthesised group in the match portion of the regex,
which is in this case "&&4;". so the regex looks for *every* occurence
(the 'g' flag at the end of the regex stands for 'global') of
newline-&&4; in $buffer1 and replaces each of them with just &&4;
let me know if this works for you - i've been wrong plenty of times, and
i want to know if this is one of those times!
hth-
--
Michael Budash ~~~~~~~~~~ mbudash@sonic.net
------------------------------
Date: Fri, 27 Jul 2001 00:32:57 GMT
From: jtjohnston <jtjohnston@courrier.usherb.ca>
Subject: Re: \nhelp
Message-Id: <3B60B75A.EEE0172F@courrier.usherb.ca>
> let me know if this works for you - i've been wrong plenty of times, and
> i want to know if this is one of those times!
Hi,
THANKS!!
File 3a.txt outputted: "4294967295" Whatever that was? Did I miss something in
your explanation?
Here is my script;
$file3 = "3.txt";
$file3a = "3a.txt";
if ( ! open(INPUT3,"<$file3") ) {
die("Can't open file \"$file1\" : $!\n");
}
if ( ! open(OUTPUT,">$file3a") ) {
die("Can't create file \"$file3a\" : $!\n");
}
{
local $/;
$buffer3 = <INPUT3>;
# print "$buffer3"; #Gives last line of 3.txt only
}
$buffer3 = ~s/\n(&&1;)/$1/g;
$buffer3 = ~s/\n(&&2;)/$1/g;
$buffer3 = ~s/\n(&&3;)/$1/g;
$buffer3 = ~s/\n(&&4;)/$1/g;
$buffer3 = ~s/\n(&&5;)/$1/g;
print OUTPUT "$buffer3";
close OUTPUT;
close INPUT1;
print "Ddone";
exit;
3.txt looks like this:
1¶field21¶field22¶field23¶field24¶field25¶
&&1;blah;
&&2;bleh;field26¶field27
2¶field21¶field22¶field23¶field24¶field25¶
&&4;blah;
&&5;bleh;field26¶field27
3¶field21¶field22¶field23¶field24¶field25¶field26¶field27
4¶field21¶field22¶field23¶field24¶field25¶field26¶field27
5¶field21¶field22¶field23¶field24¶field25¶field26¶field27
6¶field21¶field22¶field23¶field24¶field25¶field26¶field27
7¶field21¶field22¶field23¶field24¶field25¶field26¶field27
8¶field21¶field22¶field23¶field24¶field25¶field26¶field27
9¶field21¶field22¶field23¶field24¶field25¶field26¶field27
10¶field21¶field22¶field23¶field24¶field25¶field26¶field27
------------------------------
Date: Thu, 26 Jul 2001 18:14:41 -0400
From: "W. James Showalter, Jr." <gamma@mintaka.iern.disa.mil>
Subject: a* vs .* regex behavior question
Message-Id: <Pine.GSO.4.32.0107261753170.29264-100000@mintaka.iern.disa.mil>
Hello,
After reading the section on Fancy Patterns and Backtracking, "Perl
Programming, 3rd Ed", pages 203-205, I thought I would try various
patterns to get a better understanding of their behavior. I am using Perl
5.6.0.
Here is the code:
#!/usr/local/bin/perl -w
use strict;
my $foo;
$_ = "aaaaa";
/a*(?{$foo = pos($_);print " " x $foo, "position using a = $foo\n"})[\n]/;
print "\n\n";
/.*(?{$foo = pos($_);print " " x $foo, "position using . = $foo\n"})[\n]/;
Here is the output:
position using a = 5
position using a = 4
position using a = 3
position using a = 2
position using a = 1
position using a = 0
position using a = 5
position using a = 4
position using a = 3
position using a = 2
position using a = 1
position using a = 5
position using a = 4
position using a = 3
position using a = 2
position using a = 5
position using a = 4
position using a = 3
position using a = 5
position using a = 4
position using . = 5
position using . = 4
position using . = 3
position using . = 2
position using . = 1
position using . = 0
Unfortunately, I can't figure out why the following two patterns
backtrack differently. The only difference is the use of 'a' vs '.'
at the beginning of each pattern. In both cases it seems '.*' and
'a*' should match "aaaaa" and neither should match [\n].
I read the section on the regex rules and used the
debug option of the re pragma but that didn't help much.
If one tacks on a newline to $_ or employs the nonbacktracking subpattern
(?>...) then each pattern produces identical amounts of output.
I would certainly appreciate an explanation of why the patterns behave
differently.
Thanks,
Jim
------------------------------
Date: 27 Jul 2001 02:06:54 +0400
From: Ilya Martynov <ilya@martynov.org>
Subject: Re: Apache and mod_perl?
Message-Id: <87elr34a69.fsf@abra.ru>
B> Hi, which newsgroups are best to use to ask questions about:
B> 1. Configuring Apache with mod_perl
B> 2. How to write/convert perl scripts to use mod_perl?
modperl@apache.org maillist is appropriate place for such questions.
See
http://perl.apache.org#maillists
for subscription info.
B> I have been reading perl.apache.org/guide/ and every other document I
B> can find, but I still have questions...
B> for example how do I convert this basic script to work with mod_perl?
B> #!/usr/bin/perl -w
B> use CGI qw(:standard);
B> use CGI::Carp 'fatalsToBrowser';
B> use strict;
B> my $query = new CGI;
B> my $element1 = param("formElement1");
B> my $element2 = param("formElement2");
B> $element1 = "" if !$element1;
B> $element2 = "" if !$element2;
B> print header();
B> print start_html();
B> print "CGI Test page!";
B> print end_html();
B> Is it simply a matter of placing it in the right /perl dir after
B> configuring apache? I dont think so, but Im not clear on what I need to
B> do.
It depends. If you want to run CGI scripts via Apache::Registry often
you should not change anything. Some code do require some changes and
it all described in mod_perl guide:
http://perl.apache.org/guide/porting.html
Looking at your script I don't see anything that should be changed if
you want to run it as Apache::Registry script.
If you want to run your script as Apache handler them there are much
more changes that you should do.
--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
| Ilya Martynov (http://martynov.org/) |
| GnuPG 1024D/323BDEE6 D7F7 561E 4C1D 8A15 8E80 E4AE BE1A 53EB 323B DEE6 |
| AGAVA Software Company (http://www.agava.com/) |
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
------------------------------
Date: 26 Jul 2001 22:37:17 GMT
From: uthinkxxuthink@yahoo.com (Mike Erickson)
Subject: Re: Apache and mod_perl?
Message-Id: <slrn9m179m.uo5.uthinkxxuthink@srv01.datacenterops.com>
* Ilya Martynov <ilya@martynov.org> wrote:
>
> B> Hi, which newsgroups are best to use to ask questions about:
> B> 1. Configuring Apache with mod_perl
> B> 2. How to write/convert perl scripts to use mod_perl?
>
> modperl@apache.org maillist is appropriate place for such questions.
This is a question that can be answered in docs/tutorials. But I know
how hard it is to find good mod_perl tutorials, so I'm not going to
blame you for trying to answer your question here. Instead, let me give
you some helpful links:
http://www.take23.org/articles/
Some great tutorials spanning from newbie (you) to advanced.
http://perl.apache.org/
A lot of links and documentation. Wade through this.
http://stein.cshl.org/~lstein/
Lincoln Stein's Homepage. Lots of helpful articles/tutorials
http://www.mathforum.com/~ken/perl_modules.html#place
A good tutorial on perl module creation.
and of course:
http://www.google.com/
Screw Jeeves.
HTH,
me
--
$email =~ s/xx//;
------------------------------
Date: 26 Jul 2001 22:46:58 GMT
From: ebohlman@omsdev.com (Eric Bohlman)
Subject: Re: argv or getopt on Windows 2000 pro
Message-Id: <9jq6l2$opf$4@bob.news.rcn.net>
Stephen Uitti <suitti@yahoo.com> wrote:
> But under Windows 2000 Pro, from the cmd prompt (they're not calling it DOS,
> I guess), I get:
> C:\>tst.pl --c=12
> 10
> I've tried looking at ARGV[0] directly (there's nothing there under Windows,
> even though the exact same code works under Linux.)
The problem is that in NT/2000, arguments don't get passed to programs
that are run via file associations. If you were to invoke the program as
"perl tst.pl --c=12" you'd get the right results. This isn't a Perl
problem; it's a command-processor problem.
------------------------------
Date: 26 Jul 2001 23:32:24 GMT
From: abigail@foad.org (Abigail)
Subject: Re: cgi variables in French
Message-Id: <slrn9m1a5k.d4m.abigail@alexandra.xs4all.nl>
Christian Caron (chcaron@videotron.ca) wrote on MMDCCCLXXX September
MCMXCIII in <URL:news:3B57939B.18640C6D@videotron.ca>:
"" Hello all,
""
"" don't know if it's the right place to ask this question, but anyhow, I'm
""
"" sure there are experts here...
""
"" I installed Apache and now want to display the last modified date of a
"" page with a SSI. I would like to do it in French (on the fly, without
"" changing the server language).
""
"" In the web page, I tried:
""
"" <!--#exec cmd="LANG=fr_CA.ISO_8859-1;export LANG;echo '$LAST_MODIFIED'"
"" -->
"" (on one line)
""
"" and it's always printing the last modified date in English...
Well, you aren't using Perl, so how can this be a Perl question?
"" (and if someone is aware of a good site where they discuss about
"" cutomization of cgi variables, well... show it to me because I've looked
"" almost everywhere and never found one...)
""
http://www.teensex.com
Abigail
--
split // => '"';
${"@_"} = "/"; split // => eval join "+" => 1 .. 7;
*{"@_"} = sub {foreach (sort keys %_) {print "$_ $_{$_} "}};
%{"@_"} = %_ = (Just => another => Perl => Hacker); &{%{%_}};
------------------------------
Date: Thu, 26 Jul 2001 15:41:43 -0700
From: "Brian D. Green" <greenbd@u.washington.edu>
Subject: Re: CGI:standard
Message-Id: <Pine.A41.4.33.0107261536100.57840-100000@dante10.u.washington.edu>
On 26 Jul 2001, Tina Mueller wrote:
> Brian D. Green <greenbd@u.washington.edu> wrote:
>
> > use CGI::Carp qw(fatalsToBrowser);
> > print header;
>
> > $query = new CGI(\*STDIN);
>
> here you are creating a new CGI object, but as the STDIN is
> read already, the program waits for the next STDIN.
> just do
> $query = new CGI;
Thanks for the help, Tina! The script now puts the data where I want it.
There's still a problem, though: although it does fine with my HTML form's
checkboxes (where the name and value are identical), it only prints the
names of my textarea fields and not their contents. Do you know what I'd
need to change? Here's the script again:
use CGI qw/:standard/;
$query = new CGI;
@items = $query->param;
$netid = $ENV{'REMOTE_USER'};
open(SAVE, ">>$netid.out") || die("Can't open $netid.out for writing:
$!\n");
print SAVE "$date|";
foreach $item (@items) {
chomp ($item);
print SAVE "$item|";
}
print SAVE "\n";
close(SAVE);
Sincerely,
Brian Green http://students.washington.edu/greenbd/
O-----------------------------------------------------------------------O
|"I am not ashamed of the gospel, because it is the power of God for the|
| salvation of everyone who believes: first for the Jew, then for the |
| Gentile." --Romans 1:16, NIV |
O-----------------------------------------------------------------------O
------------------------------
Date: Fri, 27 Jul 2001 00:03:53 GMT
From: Michael Budash <mbudash@sonic.net>
Subject: Re: CGI:standard
Message-Id: <mbudash-67EB96.17035126072001@news.sonic.net>
In article
<Pine.A41.4.33.0107261536100.57840-100000@dante10.u.washington.edu>,
"Brian D. Green" <greenbd@u.washington.edu> wrote:
> On 26 Jul 2001, Tina Mueller wrote:
>
> > Brian D. Green <greenbd@u.washington.edu> wrote:
> >
> > > use CGI::Carp qw(fatalsToBrowser);
> > > print header;
> >
> > > $query = new CGI(\*STDIN);
> >
> > here you are creating a new CGI object, but as the STDIN is
> > read already, the program waits for the next STDIN.
> > just do
> > $query = new CGI;
>
> Thanks for the help, Tina! The script now puts the data where I want it.
> There's still a problem, though: although it does fine with my HTML form's
> checkboxes (where the name and value are identical), it only prints the
> names of my textarea fields and not their contents. Do you know what I'd
> need to change? Here's the script again:
>
> use CGI qw/:standard/;
>
> $query = new CGI;
> @items = $query->param;
you specified "use CGI qw/:standard/;" which eliminates the necessity
for the "$query = new CGI;"... then you can use:
@items =param();
now @items contains a list of the *names* of the input params... note
that you could also eliminate the @items var altogether and simply use
param() wherever you're using @items now...
> $netid = $ENV{'REMOTE_USER'};
>
> open(SAVE, ">>$netid.out") || die("Can't open $netid.out for writing:
> $!\n");
> print SAVE "$date|";
> foreach $item (@items) {
as i mentioned above, here you could simply use:
foreach $item (param()) {
> chomp ($item);
this is unnecessary - there won't be newlines at the ends of the items...
> print SAVE "$item|";
so you have printed the param *name*, but not the *value*... if you want
the value, you'd get at it here using:
param($item)
you can then do with it what you want...
> }
> print SAVE "\n";
>
> close(SAVE);
>
hth-
--
Michael Budash ~~~~~~~~~~ mbudash@sonic.net
------------------------------
Date: 27 Jul 2001 00:57:11 GMT
From: ebohlman@omsdev.com (Eric Bohlman)
Subject: Re: CGI:standard
Message-Id: <9jqe97$ikr$1@bob.news.rcn.net>
Brian D. Green <greenbd@u.washington.edu> wrote:
> Thanks for the help, Tina! The script now puts the data where I want it.
> There's still a problem, though: although it does fine with my HTML form's
> checkboxes (where the name and value are identical), it only prints the
> names of my textarea fields and not their contents. Do you know what I'd
> need to change? Here's the script again:
> use CGI qw/:standard/;
> $query = new CGI;
> @items = $query->param;
In list context, param() returns a list of, you guessed it, the *names* of
the parameters.
> $netid = $ENV{'REMOTE_USER'};
> open(SAVE, ">>$netid.out") || die("Can't open $netid.out for writing:
> $!\n");
It's *not* a good idea to open a file for writing or appending when its
name comes from outside your program and you haven't checked the name to
make sure it's reasonable.
> print SAVE "$date|";
$date is never set anywhere in your code.
> foreach $item (@items) {
> chomp ($item);
No need to do this.
> print SAVE "$item|";
print SAVE $query->param($item),'|';
> }
> print SAVE "\n";
> close(SAVE);
Of course, you could replace everything between the open and the close
with:
print SAVE "$date|",map({$query->param($_).'|'} $query->param()),"\n";
and get rid of @items altogether.
------------------------------
Date: 26 Jul 2001 22:37:21 GMT
From: Erland Sommarskog <sommar@algonet.se>
Subject: Re: DBD:ODBC, Openlink->MSSQL, and LongReadLen
Message-Id: <Xns90EB6559D031Yazorman@127.0.0.1>
Sebastian (dethtoll@yahoo.com) writes:
> I'm using perl DBI, DBD::ODBC, and OpenLink drivers to connect to a
> Win2k MS SQL Server. DBI's LongReadLen method seems to have no effect,
> I still get <255 characters when retrieving data from long memo
> fields. Has anyone run into this?
How old is the OpenLink driver?
If the client is speaking an earlier version of the TDS protocol than
TDS7, then SQL Server will revert to 6.5 behaviour, and only return
the datatypes available in 6.5. Which for varchar and char means
255 character at most.
Now, you say "memo", which is usually means "text", and even with
an old driver you should be able to get back data > 255.
But I have not used DBI nor Openlink, so I don't know if there any
issue with these.
--
Erland Sommarskog, Abaris AB
sommar@algonet.se
SQL Server MVP
------------------------------
Date: 26 Jul 2001 23:21:52 GMT
From: abigail@foad.org (Abigail)
Subject: Re: don't laugh (case usage for variables)
Message-Id: <slrn9m19hr.d4m.abigail@alexandra.xs4all.nl>
Benjamin Goldberg (goldbb2@earthlink.net) wrote on MMDCCCLXXXVI September
MCMXCIII in <URL:news:3B5FCADB.79D816A2@earthlink.net>:
^^
^^ For subs, *most* perl builtins are lowercase, the exceptions being
^^ BEGIN, CHECK, INIT, END, AUTOLOAD, and DESTROY -- the functions
^^ mentioned in perltie don't count, since if you don't tie to the package,
^^ it's not a concern. Umm, Abigail, I don't see UNTIE anywhere in my
^^ docs... which version of perl is that? 5.6.<what?>
Yeah, but it's the exceptions that seems to have to most frequent
additions.
From perltie:
Tying Scalars
A class implementing a tied scalar should define the fol
lowing methods: TIESCALAR, FETCH, STORE, and possibly
UNTIE and/or DESTROY.
Saying "well if you don't tie to a package, it's not a concern" is
stupid. People *do* tie(), so it is a concern. Or does using tie()
mean backwards compatability is no concern? With that reasoning,
we could change localtime() to return a 4 digit year too - after all,
backwards compatibility doesn't count, since if you don't use localtime,
it's not a concern.
Abigail
--
echo "==== ======= ==== ======"|perl -pes/=/J/|perl -pes/==/us/|perl -pes/=/t/\
|perl -pes/=/A/|perl -pes/=/n/|perl -pes/=/o/|perl -pes/==/th/|perl -pes/=/e/\
|perl -pes/=/r/|perl -pes/=/P/|perl -pes/=/e/|perl -pes/==/rl/|perl -pes/=/H/\
|perl -pes/=/a/|perl -pes/=/c/|perl -pes/=/k/|perl -pes/==/er/|perl -pes/=/./;
------------------------------
Date: Thu, 26 Jul 2001 22:18:41 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: Extract the relative sorting of items from multiple lists
Message-Id: <tl51mt4jfloh9vkam46erlpeq8f3ppl57c@4ax.com>
Michael Carman wrote:
># Use pred/succ of known pred/succ to extrapolate more...
># Only making one pass now, How many times must we loop
># to ensure resolving everything? Could recursion help here?
What I have already done in such a case, is repeat until nothing changes
any more.
>> As a thought for a starting point: for qw(A C F), I'd assign a
>> number to each entry, for example (A => 1, C => 2, F => 3), and
>> try to assign intermediate values for values that have to come
>> in between existing items.
>
>I don't think that scales well. It's like programming in BASIC. Sure,
>you can start out numbering lines 10, 20, 30, etc., but what happens
>when you've already inserted 11..19 and you need to squeeze in one more?
>Renumber everything?
Nothing demands that these must be integers. Floating points works just
as well. What's more: multiples of negative powers of 2 (like 5/16) have
an *exact* representation in FP. So inserting 1.5 between 1 and 2 isn't
such a bad situation.
--
Bart.
------------------------------
Date: Fri, 27 Jul 2001 00:17:27 GMT
From: PerlFAQ Server <faq@denver.pm.org>
Subject: FAQ: How do I remove consecutive pairs of characters?
Message-Id: <rq287.93$os9.190766592@news.frii.net>
This message is one of several periodic postings to comp.lang.perl.misc
intended to make it easier for perl programmers to find answers to
common questions. The core of this message represents an excerpt
from the documentation provided with every Standard Distribution of
Perl.
+
How do I remove consecutive pairs of characters?
To turn ""abbcccd"" into ""abccd"":
s/(.)\1/$1/g; # add /s to include newlines
Here's a solution that turns "abbcccd" to "abcd":
y///cs; # y == tr, but shorter :-)
-
Documents such as this have been called "Answers to Frequently
Asked Questions" or FAQ for short. They represent an important
part of the Usenet tradition. They serve to reduce the volume of
redundant traffic on a news group by providing quality answers to
questions that keep coming up.
If you are some how irritated by seeing these postings you are free
to ignore them or add the sender to your killfile. If you find
errors or other problems with these postings please send corrections
or comments to the posting email address or to the maintainers as
directed in the perlfaq manual page.
Answers to questions about LOTS of stuff, mostly not related to
Perl, can be found by pointing your news client to
news:news.answers
or to the many thousands of other useful Usenet news groups.
Note that the FAQ text posted by this server may have been modified
from that distributed in the stable Perl release. It may have been
edited to reflect the additions, changes and corrections provided
by respondents, reviewers, and critics to previous postings of
these FAQ. Complete text of these FAQ are available on request.
The perlfaq manual page contains the following copyright notice.
AUTHOR AND COPYRIGHT
Copyright (c) 1997-1999 Tom Christiansen and Nathan
Torkington. All rights reserved.
This posting is provided in the hope that it will be useful but
does not represent a commitment or contract of any kind on the part
of the contributers, authors or their agents.
04.19
--
This space intentionally left blank
------------------------------
Date: 26 Jul 2001 19:43:54 -0500
From: logan@cs.utexas.edu (Logan Shaw)
Subject: Re: Finding a word in a sorted list.
Message-Id: <9jqdga$s1f$1@charity.cs.utexas.edu>
In article <42f3ee2.0107251508.35e589ea@posting.google.com>,
Mario Rizzuti <mariorizzuti@yahoo.com> wrote:
>I have a fixed-length-records file where:
>
>* each record is 18 bytes made by an username (12 bytes) and a number
>(6 bytes).
>* the records are sorted alphabetically.
>
>I am wondering what is the fastest possible way to find the associated
>numbers of a given list of usernames?
^^^^ ^^
I think the answer depends on the size of the list of usernames to be
located relative to the total number of usernames. If most all of them
are to be located, it will probably be most efficient just to read the
file in order and match them up. (Sort of like a merge sort.)
The solution you gave takes the list of usernames and searches for them
in order, using that to set the lower bound for the binary search.
While this does help reduce the effort required for the binary search
to some degree, it doesn't reduce it by much if your list of names to
be found is approximately evenly distributed throughout the list you're
searching.
If you have 10 names, then the first one is likely to occur about
1/10th of the way into the list, the second is likely to occur about
2/10th of the way into the list, and so on. So, by using a lower
bound, you are reducing the space that must be searched, but not by
very much. In fact, the size of reduced search space follows this
pattern:
which search you're doing reduced search space size
------------------------- -------------------------
#1 100% of original
#2 90% of original
#3 80% of original
#4 70% of original
: :
: :
#10 10% of original
The math here isn't quite right, but it gives the general idea that
size of the reduced search space falls about linearly.
This *does* help, but not very much, because searching 9/10th of the
search space is only going to be a tiny, tiny bit more efficient since
a binary search runs in logarithmic time (the logarithm of n is just
not that much larger than the logarithm of 9/10 * n).
If you want to take that approach further, take your sorted list of
needles to be found in the haystack, and then instead of searching for
the first needle in the list first, search for the needle in the middle
of the list of needles first. Then recursively search for all the
needles before the middle needle and then all the ones after it. This,
like your strategy, capitalizes on results of past searches, but it
does a better job because after the first search, you have split the
space approximately in half, and every search after that has a reduced
search space that's no more than 50% of the original. Some of them are
reduced by 50% again (and maybe again), and you can really start to
have a nice noticeable effect on log(n) if you reduce n by 50% once or
twice or more.
Having said all that, if your file is pretty large (i.e. not so small
that the whole thing gets slurped into disk cache after enough of
this), then you're going to find that the limiting factor is the number
of times you hit the disk. Seeks are expensive, because the disk head
has to shoot across to some other part of the disk and then recalibrate
itself so it's lined up with the desired track. You want to avoid
seeks, so maybe the best plan is to structure your solution around
avoiding as many disk accesses as possible (and/or making them
sequential).
If you think in those terms, there's another solution that might make
sense. Instead of taking advantage of just the results of the search,
you might want to store the information you get at every single step of
every binary search so that you have available the most information
possible later on. This will minimize having to go back to disk to get
information you've already gotten. You could use a scheme where every
time you read any record off the disk, you store that record and its
offset in a data structure (a binary search tree). Then future
searches can look in that binary search tree first and (as it gets
populated) get very precise lower and upper bounds.
But actually, there's a much simpler and more elegant way to do it.
To get a feel for this algorithm[1], take the concepts behind binary
search and the concepts behind quicksort, put them in a box, and shake
vigorously. (And don't think in terms of using information from
previous searches in subsequent ones; instead, think in terms of using
information right when you get it.)
Basically, what you do is forget the idea of doing one binary search
after another. Instead, you do them ALL AT ONCE. The first thing you
do is you look at the element is right in middle of your search space.
You read whatever element you find there. Then you look at all the
things you're searching for. Some of them occur before that element.
Some of them occur after that element. Some of them *are* that
element, and you've found (part of) what you're looking for.
What you want to do is split up your list of things-to-find into two
lists: those that occur before the element you've just examined, and
those that occur after it. (This is the part that's like quicksort.)
Now, with the first list, you have an interesting situation. You have
a list of things to find (those that occur before the element you just
examined), and you have a search space (everything in the search space
you just considered, as long as it's before the element you just looked
at) to find them in. And that's the same problem you started with,
only half as big. So you call yourself recursively. All this stuff
holds true for the second half of the search space and the list of
things you expect to find in it too, so you call yourself recursively
again.
The delightful thing is that the search space is being cut in half
every time, and the list of things to find in it is being cut sometimes
too. This should result in a small amount of processing and a very
small number of disk accesses. The only potential problem is that
you've got to store in memory all these temporary lists of items you're
searching for. That kind of sucks, actually, but probably not too
badly.
Here's some sample code:
#! /usr/local/bin/perl
@needles = map (int(rand(1_000_000)), 1 .. 50);
@haystack = map (int(rand(1_000_000)), 1 .. 10000);
@haystack = sort { $a <=> $b } (@haystack, @needles);
sub find_them
{
my ($haystack, $lowest_index, $highest_index, $needles) = @_;
my ($middle_index, $element);
return if (@$needles == 0);
if ($lowest_index > $highest_index)
{
foreach my $not_found (@$needles)
{
print "'$not_found' not found\n";
}
return;
}
$middle_index = int (($lowest_index + $highest_index) / 2);
$element = $haystack->[$middle_index];
foreach my $found (grep ($_ == $element, @$needles))
{
print "'$found' found at index $middle_index\n";
}
find_them (
$haystack,
$lowest_index, $middle_index - 1,
[ grep ($_ < $element, @$needles) ]
);
find_them (
$haystack,
$middle_index + 1, $highest_index,
[ grep ($_ > $element, @$needles) ]
);
}
find_them (\@haystack, 0, @haystack - 1, \@needles);
Actually, that may waste more memory than it should, since it makes
copies of @$needles every time, even though there are probably lots of
times when @$needles doesn't change at all. Here's a version of the
function which saves some memory by checking for that:
sub find_them
{
my ($haystack, $lowest_index, $highest_index, $needles) = @_;
my ($middle_index, $element, $grep_temp);
return if (@$needles == 0);
if ($lowest_index > $highest_index)
{
foreach my $not_found (@$needles)
{
print "'$not_found' not found\n";
}
return;
}
$middle_index = int (($lowest_index + $highest_index) / 2);
$element = $haystack->[$middle_index];
foreach my $found (grep ($_ == $element, @$needles))
{
print "'$found' found at index $middle_index\n";
}
$grep_temp = [ grep ($_ < $element, @$needles) ];
# if they're the same size, they have the same contents, so
# save memory by just using the original instead of a copy.
if (@$grep_temp == @$needles)
{
$grep_temp = $needles;
}
find_them (
$haystack,
$lowest_index, $middle_index - 1,
$grep_temp
);
$grep_temp = [ grep ($_ > $element, @$needles) ];
# see $grep_temp comment above.
if (@$grep_temp == @$needles)
{
$grep_temp = $needles;
}
find_them (
$haystack,
$middle_index + 1, $highest_index,
$grep_temp
);
}
You could actually eliminate all the copying of lists of needles if
desired. The way to do that would be to sort the list of needles
initially and then just pass an upper and lower bound for what part of
the list of needles you're concerned about. But then you wouldn't get
to use the pretty grep() function and the code would be ugly. :-)
Note one other nice property of the sample implementation that I gave:
the recursive calls are done in such an order that all searching in the
first half of the file (assuming you replace my usage of an arrays with
usage of a file) is done before all searching in the second half of the
file. This property is recursively true, too. So basically what this
means is that your disk accesses will be fairly close to sequential,
and definitely localized to specific regions of the disk. This is good
because hard disk buffers handle that situation well (especially disks
that do prefetching), and it's also good because it takes less time to
seek shorter distances.
Of course, now that I've said all of this, I should point out that in
most cases it probably *would* be faster to use Berkeley DB or
something similar. At least, it would be faster if the list of things
you're searching for is pretty small. (The one weakness of those
systems is that they use hashing, so there is no way they can make
their disk accesses sequential. If the file is large and you're
looking for lots of things in it, performance probably won't be that
great because of that.)
- Logan
[1] an algorithm which I just came up with, but which probably has
already been invented by somebody else I don't know about. If I
did invent it, SOMEBODY PLEASE TELL ME SO. :-)
--
"Our grandkids love that we get Roadrunner and digital cable."
(Advertisement for Time Warner cable TV and internet access, July 2001)
------------------------------
Date: Thu, 26 Jul 2001 22:54:25 -0000
From: Chris Stith <mischief@velma.motion.net>
Subject: Re: sortlen -- filter to sort text by line length
Message-Id: <tm17t1gute4f8@corp.supernews.com>
Yves Orton <demerphq@hotmail.com> wrote:
> Benjamin Goldberg <goldbb2@earthlink.net> wrote in message news:<3B54F150.7408211B@earthlink.net>...
>> How does ST compare with GRT or with basic/custom sort?
> After reading the thread I did a bunch of benchmarks. It looks like
> ST is actually pretty bad. I even tried using super long strings to
> try to cause trouble for the GRT types substr() operation, but that
> seemed to have a more profound effect on the ST than on the GRT, which
> quite frankly suprised me.
That's why benchmarks are important. ;-)
[snip]
> them out?? Frankly these results to me are suprising, unless your
> comments above about the optimizations that perl does are the
> explanation. If so then your ideas about extending such optimizations
That's probably a good start. With a language with so much going on
under the hood, though, it's often difficult to tell how much speed
you're talking about without looking at what's already being done
internally.
> A minor question: What does GRT stand for?
That's a major question, IMHO. It stands for Guttman-Rosler Transform,
as in Uri Guttman an Larry Rosler, coauthors of a work on efficient
sorting in Perl. They codiyfied it clearly and popularized it, and
although they did not think up the concept from scratch, I think the
name is pretty fitting considering the amount of time and effort
Rosler and Guttman put into demonstrating its use with mny different
data types.
Chris
--
Programming is a tool. A tool is neither good nor evil. It is
the user who determines how it is used and to what ends.
------------------------------
Date: Thu, 26 Jul 2001 20:32:07 -0400
From: Jason LaPenta <lapenta_jm@yahoo.com>
Subject: Stripping extention from a filename in a string
Message-Id: <3B60B687.B08F6531@yahoo.com>
Hi,
I have a string var $filename = "filename.ext" and I want it to be
turned into $filename = "filename" by removing the ".ext" How can I do
this? I've been reading about pattern matching for an hour and can't
find even the slightest reference the such and operation.
BTW: I'm finding it very difficult to find out how to do the simplest
things in perl. I go though the tutorials.. which seem to be written for
people who already know the language. And I have the book Programming
Perl by O'reilly, but it seems that I have to read like 2days each time
I want to find out how to do the simplest thing... then I just wind up
going to the newsgroups and asking. Is there some resource I'm missing?
Thanks a ton
Jason
------------------------------
Date: Thu, 26 Jul 2001 23:07:50 GMT
From: slash@dot.c.o.m.org
Subject: Re: What's the idiomatic way for this?
Message-Id: <3b60a27c.16980390@news.freeserve.co.uk>
On 26 Jul 2001 08:29:10 -0700, samara_biz@hotmail.com (Alex) wrote:
>Hi,
>
>I wrote this code for printing days in a calendar. The way it works is
>I have @grid array, which always has 42 elements. (Since maximum
>number of weeks is 6, 6*7 = 42). What I do I just print it 7 elements
>in a row.
>
>Here's the code:
>
>for( my $i = 1; $i < 7; $i++ ){
> print("<tr align=\"right\">\n");
> my $s_ind = ($i - 1) * 7;
> my $e_ind = $s_ind + 6;
> foreach (@grid[$s_ind..$e_ind]){
> print("<td>" . ($_ == 0 ? ' ' : $_ ) . "</td>\n");
> } #foreach
> print("<td width=\"20%\" align=\"right\">\<$i</td>\n");
> print("</tr>\n");
>} #for
>
>I was wondering if there's a way to rewrite this in a more idiomatic
>way. I have a feeling that I don't really need to use for() loop or
>compute slice indexes on two separate lines, but I can't think of a
>better way to write this.
>
>Can anyone show me a better way?
>Thanks a lot!
>
>Alex
If you're on a *nix system, you could use cal to make life a lot easier :-)
#!/usr/local/bin/perl -w
print "Content-type: text/html\n\n<html><body><table border=1 cellpadding=2
cellspacing=0>\n";
$c=`cal`;
@d=split(/\n/,$c);
$header=splice(@d,0,1);
print "<h1>$header</h1>\n";
foreach (@d){
s/(\S+)/<td align="right">$1<\/td>/gi;
print "<tr>$_</tr>\n";
}
print "</table>\n</body>\n</html>";
------------------------------
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.
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 1388
***************************************