[19171] in Perl-Users-Digest
Perl-Users Digest, Issue: 1366 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Jul 24 09:05:32 2001
Date: Tue, 24 Jul 2001 06: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: <995979909-v10-i1366@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Tue, 24 Jul 2001 Volume: 10 Number: 1366
Today's topics:
Re: CGI -> Apache Problem <carlos@plant.student.utwente.nl>
Re: creating another file (Garry)
embedding perl under Win32 with borland (Christoph Droste)
FAQ: Why aren't my random numbers random? <faq@denver.pm.org>
gd-1.8.4 and GD1.33 with freetype2 support !! (ssa)
Re: Generating elements based on input <bholness@nortelnetworks.com>
Re: How can I determine how many charcters to represnt (Anno Siegel)
Re: How to convert Pc/Unix characterset (ISO-lat1) to t <alexis.roda@si.urv.es>
How to convert Pc/Unix characterset (ISO-lat1) to the M <louis.banens@xs4all.nl>
Re: Including a perl file into another perl file??? <bernie@fantasyfarm.com>
Re: nested forks - help.... <elias_haddad@tertio.com>
Re: newbie: storing multiple objects in a hash <m.grimshaw@salford.ac.uk>
Re: Open files (Anno Siegel)
Re: Regular Expression Experts <paul.johnston@dsvr.co.uk>
Re: Running the same scripts of different platforms <nomail@hursley.ibm.com>
Re: scope for recursive sub <SPAM_lapenta_jm@yahoo.com>
Re: single user mode? (Anno Siegel)
Re: Sorting an array of strings by 'closeness' to anoth <jasper@guideguide.com>
Re: Sorting an array of strings by 'closeness' to anoth <jasper@guideguide.com>
Re: Sorting an array of strings by 'closeness' to anoth (Anno Siegel)
Re: Split Question (Anno Siegel)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 24 Jul 2001 14:53:42 +0200
From: "carlos" <carlos@plant.student.utwente.nl>
Subject: Re: CGI -> Apache Problem
Message-Id: <9jjr4n$lrp$1@dinkel.civ.utwente.nl>
what does $! tell you?
--
carl0s
"Georg Vassilopulos" <Georg.Vassilopulos@SoftwareAG.de> wrote in message
news:9jjdhl$b55$1@gamma.ecomp.net...
> Hello People,
>
> Hope this request is not too much "off topic".
>
> I really freak out. Following situation:
>
> On Win NT 4.0 I wrote my CGI scripts. I have to "port" it to Win NT 4.0
> Server.
> I use same Apache versions (SERVER_SOFTWARE="Apache/1.3.12 (Win32)
> DAV/1.0.1")
>
> Now in one script a system call is made. I want to run a zip programm if
> someone uploads a "*.zip" file.
>
> My code snippet:
>
> my $command = "p:/tools/wni/unzip.exe $myFile";
> system ($command) or print "Fuck the system command:$!";
>
>
> Why does ist work under Win NT and not under Win NT Server?
>
> Can I specify wrights in Apache what to execute and what not?
>
> Where do I set permissions for executing programms in Apache?
>
> My environment is the same. I checkt for the silly mistakes like path
> variable, file permission.....
>
>
>
>
>
> Have a good time!!!
> Georg
> georg.vassilopulos@softwareAG.com
>
>
>
>
------------------------------
Date: 24 Jul 2001 03:41:17 -0700
From: garry_short@hotmail.com (Garry)
Subject: Re: creating another file
Message-Id: <bdcefd33.0107240241.5c4ed5e1@posting.google.com>
"Devon Perez" <hoss@chungk.com> wrote in message news:<aaJ67.32492$k33.2811249@typhoon.kc.rr.com>...
> how do i have perl create another file? i want it to create an html file...
> i use this code:
> open (FILEA,">$idnum.html");
> print FILEA "$test";
> close(FILEA);
>
> but it does not work, it doesnt create a new html file... please help
Not 100% sure, but I don't think > will create a file that doesn't
exist. I *am* 100% sure, however, that >> will.
If it still doesn't work, try ...
my $filename = $idnum . ".html";
open (FILEA, ">>$filename") or die "Could not create file $filename -
$!";
Regards,
Garry
------------------------------
Date: 24 Jul 2001 04:12:05 -0700
From: droste@aed-graphics.de (Christoph Droste)
Subject: embedding perl under Win32 with borland
Message-Id: <7a3a03da.0107240312.4d302ef5@posting.google.com>
Hello,
I have embedded Perl under Windows NT with the Borland compiler as
follows:
bcc32.exe -ID:/Perl/lib/CORE -DNDEBUG -DWIN32 -D_CONSOLE -DNO_STRICT
-DHAVE_DES_FCRYPT -D
CIT_CONTEXT -DPERL_IMPLICIT_SYS -DPERL_MSVCRT_READFIX -O1 -w -tWD
TestPerlApp_C.c -l -L"C:
CORE/" perl56.lib
to get a DLL that can further be used. The Perl Version is 5.6.1 from
Active State.
This works as it is. However there are a few things, that seem to be
strange.
- When using the Perl-Interpreter @INC is set to some paths, but it is
empty,
when the same script is run from the embedded interpreter. Is this a
problem
of the compiler, the platform or whatever?
- Although there are many examples in perlembed, no one cares about
the return
values of the used functions perl_parse(), perl_run(), and so on.
is there a documentation of the return codes. For robust use in
changing
environments, there must be feedback, if the script is syntactically
correct.
Any additional information concerning these points is appreciated.
Thanks
Christoph
------------------------------
Date: Tue, 24 Jul 2001 12:16:51 GMT
From: PerlFAQ Server <faq@denver.pm.org>
Subject: FAQ: Why aren't my random numbers random?
Message-Id: <TGd77.12$os9.201447936@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.
+
Why aren't my random numbers random?
If you're using a version of Perl before 5.004, you must call "srand"
once at the start of your program to seed the random number generator.
5.004 and later automatically call "srand" at the beginning. Don't call
"srand" more than once--you make your numbers less random, rather than
more.
Computers are good at being predictable and bad at being random (despite
appearances caused by bugs in your programs :-).
http://www.perl.com/CPAN/doc/FMTEYEWTK/random , courtesy of Tom Phoenix,
talks more about this. John von Neumann said, ``Anyone who attempts to
generate random numbers by deterministic means is, of course, living in
a state of sin.''
If you want numbers that are more random than "rand" with "srand"
provides, you should also check out the Math::TrulyRandom module from
CPAN. It uses the imperfections in your system's timer to generate
random numbers, but this takes quite a while. If you want a better
pseudorandom generator than comes with your operating system, look at
``Numerical Recipes in C'' at http://www.nr.com/ .
-
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.09
--
This space intentionally left blank
------------------------------
Date: 24 Jul 2001 03:51:04 -0700
From: seresa_null@yahoo.com (ssa)
Subject: gd-1.8.4 and GD1.33 with freetype2 support !!
Message-Id: <1ec4f806.0107240251.579010c1@posting.google.com>
gd-1.8.3 patch with the patch_gd.pl from GD-1.33
THE PROBLEM:
checking for freetype.h... no
configure: warning: libgd will be built without support for TrueType fonts.
checking for TT_Init_FreeType in -lttf... yes
There are 2 freetype.h :
/usr/local/include/freetype2/freetype/freetype.h
/usr/local/include/freetype/freetype.h
[root@daemon gd-1.8.4]# ./configure --enable-jpeg --enable-freetype
creating cache ./config.cache
checking for a BSD compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking whether make sets ${MAKE}... yes
checking for working aclocal... found
checking for working autoconf... found
checking for working automake... found
checking for working autoheader... found
checking for working makeinfo... found
checking for gcc... gcc
checking whether the C compiler (gcc ) works... yes
checking whether the C compiler (gcc ) is a cross-compiler... no
checking whether we are using GNU C... yes
checking whether gcc accepts -g... yes
checking for POSIXized ISC... no
checking host system type... i686-pc-linux-gnu
checking build system type... i686-pc-linux-gnu
checking for ranlib... ranlib
checking for ld used by GCC... /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld... yes
checking for BSD-compatible nm... /usr/bin/nm -B
checking whether ln -s works... yes
updating cache ./config.cache
loading cache ./config.cache within ltconfig
checking for object suffix... o
checking for executable suffix... no
checking for gcc option to produce PIC... -fPIC
checking if gcc PIC flag -fPIC works... yes
checking if gcc supports -c -o file.o... yes
checking if gcc supports -c -o file.lo... yes
checking if gcc supports -fno-rtti -fno-exceptions ... yes
checking if gcc static flag -static works... -static
checking if the linker (/usr/bin/ld) is GNU ld... yes
checking whether the linker (/usr/bin/ld) supports shared libraries... yes
checking command to parse /usr/bin/nm -B output... ok
checking how to hardcode library paths into programs... immediate
checking for /usr/bin/ld option to reload object files... -r
checking dynamic linker characteristics... Linux ld.so
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... yes
checking for objdir... .libs
creating libtool
updating cache ./config.cache
loading cache ./config.cache
checking for a BSD compatible install... /usr/bin/install -c
checking how to run the C preprocessor... gcc -E
checking for X... libraries /usr/X11R6/lib, headers /usr/X11R6/include
checking for main in -lm... yes
checking for deflate in -lz... yes
checking for png_check_sig in -lpng... yes
checking for jpeglib.h... yes
checking for jpeg_start_compress in -ljpeg... yes
checking for freetype.h... no
configure: warning: libgd will be built without support for TrueType fonts.
checking for TT_Init_FreeType in -lttf... yes
checking for ANSI C header files... yes
checking for malloc.h... yes
checking for unistd.h... yes
checking for zlib.h... yes
checking for png.h... yes
checking for working const... yes
updating cache ./config.cache
creating ./config.status
creating Makefile
------------------------------
Date: Tue, 24 Jul 2001 10:55:52 +0100
From: "Ben Holness" <bholness@nortelnetworks.com>
Subject: Re: Generating elements based on input
Message-Id: <9jjgm3$9sq$1@bcarh8ab.ca.nortel.com>
>
> could someone please tell me how i can generate a popupmenu based
> on an input that i have received. both the input received and the
> popup menu have to be on the same form. urgent!!! please reply asap.
Sounds like a Javascript question to me. Try comp.lang.javascript, or have a
look on the web - there are loads of examples.
Ben
------------------------------
Date: 24 Jul 2001 12:00:28 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: How can I determine how many charcters to represnt a given int value?
Message-Id: <9jjo0s$mju$4@mamenchi.zrz.TU-Berlin.DE>
According to Stan Brown <stanb@panix.com>:
> In <3B5C8B61.42CFF2BB@vpservices.com> Jeff Zucker <jeff@vpservices.com> writes:
>
> >Stan Brown wrote:
> >>
> >> I'm doing something (wrong perhaps) with PerlTK that requires that I tell
> >> the widget how much space to reserve for a given number (integer).
> >>
> >> How can I do this in perl? In C I would kust printf it into a temporary
> >> char array, anddo len() on it.
> >>
> >> What's a better perl way of doing it?
>
> >print length 1234567;
>
>
> ARGH! I thought I ollked in the function reference.
Kust ollk until you fnid it :)
Anno
------------------------------
Date: Tue, 24 Jul 2001 14:33:24 +0200
From: Alexis Roda <alexis.roda@si.urv.es>
Subject: Re: How to convert Pc/Unix characterset (ISO-lat1) to the Mac character set
Message-Id: <3B5D6B14.CDBE7262@si.urv.es>
Louis Banens wrote:
>
> Hi,
>
> Can anybody tell me how to convert Pc/Unix characterset (ISO-lat1) to the
> Mac character set. We have problems with extende characters uploaded from a
> Mac to a PC/Unix system. Are there any perl routines available for this.
I don't know if there is a perl solution. Have you tried recode?
Description: Character set conversion utility.
GNU `recode' converts files between character sets and usages. When
exact transliterations are not possible, it may get rid of the
offending characters or fall back on approximations. This program
recognizes or produces nearly 150 different character sets and is able
to transliterate files between almost any pair. Most RFC 1345
character sets are supported.
HTH
--
////
(@ @)
---------------------------oOO----(_)----OOo------------------------
Los pecados de los tres mundos desapareceran conmigo.
Alexis Roda - Universitat Rovira i Virgili - Reus, Tarragona (Spain)
--------------------------------------------------------------------
------------------------------
Date: Tue, 24 Jul 2001 14:00:18 +0200
From: "Louis Banens" <louis.banens@xs4all.nl>
Subject: How to convert Pc/Unix characterset (ISO-lat1) to the Mac character set
Message-Id: <9jjo3c$9q8$1@news1.xs4all.nl>
Hi,
Can anybody tell me how to convert Pc/Unix characterset (ISO-lat1) to the
Mac character set. We have problems with extende characters uploaded from a
Mac to a PC/Unix system. Are there any perl routines available for this.
Regards,
Louis Banens
------------------------------
Date: Tue, 24 Jul 2001 09:03:46 -0400
From: Bernie Cosell <bernie@fantasyfarm.com>
Subject: Re: Including a perl file into another perl file???
Message-Id: <0fsqlt4p3sk403pq8ecnvdbbt7j85a93nb@news.supernews.net>
inwap@best.com (Joe Smith) wrote:
} In article <9jhtfh$nrgus$1@ID-98646.news.dfncis.de>,
} Bob Rock <no_spam.yet_another_apprentice@hotmail.com> wrote:
} >what I'd like to do is like have the code necessary to generate a header or
} >a footer placed in one file and then include that one file in all the file
} >that need that header/footer. Which of the statements suggested would do?
}
} require "header+footer.pl"; # Defines $HEADER and $FOOTER;
One slight thing --- depending on what you want to do in your 'required'
program you may need to do:
BEGIN { require "header+footer.pl" }
/Bernie\
--
Bernie Cosell Fantasy Farm Fibers
bernie@fantasyfarm.com Pearisburg, VA
--> Too many people, too few sheep <--
------------------------------
Date: Tue, 24 Jul 2001 12:52:15 +0100
From: "Elias Haddad" <elias_haddad@tertio.com>
Subject: Re: nested forks - help....
Message-Id: <yid77.17260$SK6.1911067@news6-win.server.ntlworld.com>
> Why are you sleeping before you set the SIGCHLD handler? You should
> probably set it once before the first fork, otherwise some signal
> may arrive before the handler is set.
You're right, I have a feeling my errors are coming from the fact that I
have misplaced my SIGCHLD handlers.
> This second "if" is better written as an "else" alternative to the
> first one.
True as well.
> "Strange results" is not a very precise description of what you are
> seeing. Nothing in your code shows any sensitivity as to which
> child belongs to which parent; you are ignoring (most of) the
> SIGCHLDs anyhow.
I didn't want to elaborate on the error messages I was getting because they
are generated by the Net::SNMP module and didn't shed (in my opinion) any
light on what I was doing wrong.
>
> Do you realize what you are saying? "I'm being paid for this, so
> I need your free help urgently." Not a very diplomatic thing to say.
>
I wasn't quite saying that. Just stressing the fact that I was under a lot
of pressure to deliver a solution.
Thanks for tips and comments.
Elias.
------------------------------
Date: Tue, 24 Jul 2001 11:01:08 +0100
From: Mark Grimshaw <m.grimshaw@salford.ac.uk>
Subject: Re: newbie: storing multiple objects in a hash
Message-Id: <3B5D4764.84E7A544@salford.ac.uk>
Logan Shaw wrote:
>
> In article <VI077.429460$6b.16636998@Flipper>,
> Rolf Deenen <r.deenen@chello.nl> wrote:
> >
> >Hi,
> >
> >'m just starting to getting to learn perl and i have a small question about
> >storing data in a hash.
> >You can store object-pairs in a hash like in this example:
> >
> >%users (name, John Doe, email, jdoe@hotmail.com, telephone, 01234-567890)
> >
> >In this fasion it is possible to recall variables by there "type", like name
> >or telephone-number...
> >$johndoe_telephone=%users[telephone];
>
> Yes, except that the syntax is wrong and should be this instead:
>
> $johndoe_telephone = $users{telephone};
>
> >But what happens when I want to put in more than one users in a hash and how
> >can i recall them?
>
> You can't really do that.
<snip>
I'm sure a hash of hashes is a better/more efficient way to do it as has
been discussed here but if I want to store more than one user in a hash
and each user has more than one value, I create a delimited string of
those values and store them as the values for the keys. To get a
specific value back, I split the hash key value and subscript it. As a
delimiter, I use some character or grouping of characters that will not
be input as part of a value or better yet is actually forbidden by the
script that takes in those values - something like '¬'.
e.g.
#!/usr/local/bin/perl -w
use strict;
my (%hash);
my $unique_user_id1 = 1;
my $unique_user_id2 = 2;
my $value1 = join('¬', ('John Doe', 'jdoe@hotmail.com',
'01234-567890'));
my $value2 = join('¬', ('Me Me', 'me@hotmail.com', '04321-098765'));
$hash{$unique_user_id1} = $value1;
$hash{$unique_user_id2} = $value2;
my $email1 = (split(/¬/, $hash{$unique_user_id1}))[1];
my $email2 = (split(/¬/, $hash{$unique_user_id2}))[1];
print "$email1\n$email2\n\n";
prints:
jdoe@hotmail.com
me@hotmail.com
------------------------------
Date: 24 Jul 2001 12:23:33 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Open files
Message-Id: <9jjpc5$mju$5@mamenchi.zrz.TU-Berlin.DE>
According to $Bill Luebkert <dbe@todbe.com>:
> Rob Kirby wrote:
> >
> > I would like to use the Find.pm to traverse a filesystem to create
> > a list of files that reside in that filesystem and record various
> > info about each file with the stat.pm module. Is there a module
> > to obtain a list of open files for a given filesystem?
>
> File::Find
How does File::Find help in finding open files? As far as I see,
the OP intends to use that anyhow.
Finding open files under Unix is not trivial. Some (most?) systems
have "fuser" these days, but fuser is a relatively expensive operation.
I would hesitate to apply it indiscriminately to a lot of files.
Anno
------------------------------
Date: Tue, 24 Jul 2001 12:22:13 +0100
From: Paul Johnston <paul.johnston@dsvr.co.uk>
Subject: Re: Regular Expression Experts
Message-Id: <3B5D5A65.D151F677@dsvr.co.uk>
Hi,
> example:
> 1) asdf132/123-1432
> I should get 132/123
How about /(\d+\/[\d\/]+)/ ? Simpler than nested brackets, but it will
match things like 123//456
Paul
------------------------------
Date: Tue, 24 Jul 2001 11:45:00 +0100
From: Derek Fountain <nomail@hursley.ibm.com>
Subject: Re: Running the same scripts of different platforms
Message-Id: <9jjjjd$toe$1@sp15at20.hursley.ibm.com>
> #!/usr/bin/env perl
> "perl" should be in your path variable and is "/usr/bin/env" always
> true ?
> that I am not sure.
This works, except it doesn't like having the -w switch on the top line.
Can't be having that! Seems that:
#!/usr/bin/env perl
use diagnostics;
disable diagnostics;
gives me the behaviour I'm used to. Thanks!
> #!/bin/sh -- # -*- perl -*- -p
> eval 'exec perl -wS $0 ${1+"$@"}'
> if $running_under_some_shell;
For the record, this doesn't work under Linux. /bin/sh is bash and it
complains:
/bin/sh: -- # -*- perl -*- -w: unrecognized option
then gives the bash usage options.
------------------------------
Date: Tue, 24 Jul 2001 08:45:53 -0400
From: Jason LaPenta <SPAM_lapenta_jm@yahoo.com>
Subject: Re: scope for recursive sub
Message-Id: <3B5D6E01.A60C69DC@yahoo.com>
I tried, my, local,our.. none of them worked.. This everything is a
global is a major pain for someone who like encapsulation! What I want
is to call my subroutien from itself and have the data from the new call
be independant from the previous call. Does this make sense?
sub recursion {
$data = $index++;
print $data."\n"
if ( $index < 3 ){
recursion();
}
print $data;
}
$index = 0;
recursion();
# desired output
0
1
2
2
1
0
Akira Yamanita wrote:
>
> Jason LaPenta wrote:
> >
> > I have a recursive sub. and I want a local var that is not global, so
> > each call in the recursion has it's own copy. How do I do this. I've
> > tried delcaring it local.
>
> Use my instead of local.
------------------------------
Date: 24 Jul 2001 10:06:23 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: single user mode?
Message-Id: <9jjhav$mju$2@mamenchi.zrz.TU-Berlin.DE>
According to Andras Malatinszky <andras@mortgagestats.com>:
>
>
> Bernard El-Hagin wrote:
>
> > On Mon, 23 Jul 2001 19:01:20 +0800, allan <@netvigator.com> wrote:
> > >Hi,
> > >
> > >Does anyone remember how to enter "single" user mode?
> >
> > Perl doesn't have "single" user mode.
> >
>
> You mean it's for married users only?
No, it means that becoming a Perl user you are immediately wrapped
in a warm and fuzzy community that will ease all your˙˙˙˙˙NO CARRIER
------------------------------
Date: Tue, 24 Jul 2001 11:22:21 +0100
From: Jasper McCrea <jasper@guideguide.com>
Subject: Re: Sorting an array of strings by 'closeness' to another string
Message-Id: <3B5D4C5D.DB1D4750@guideguide.com>
jbp wrote:
>
> Howdy
>
> I have a string containing, say, 'transeint' (sic). After some
> massaging the string a little, then grepping a dictionary, then some
> more massaging, i get an array of strings like so:
>
> anastasia
> anisettes
> anteaters
snippety-do-da
> testiness
> transient
> treatises
> trinities
> triteness
>
> Now, what I want to do is sort this array into an order such that the
> ones near the top of the array (viewing it as a stack) are:
>
> transient
> treatises
> trinities
> ... etc
>
> How would I go about this? I've looked at String::Approx and one of
> the functions would be ideal, except I can't install the module on my
> host (the goddamn service provider won't do it). I've installed it on
> my local machine, but obviously developing with it would be useless.
>
> *sigh*
>
> Any ideas?
I was writing a program to do the word conversion game (you knoe, get
from GOAT to SEAT changing one letter at a time, and wrote a sub that
might help.
sub counter {
my @word = split '', shift;
my @possible = split '', shift;
my $counter = 0;
for (0..$#word) { ++$counter if $word[$_] ne $possible[$_] }
return $counter;
}
returns the number of letters different in the two words (they must be
the same length, but looking at your list, you already have that).
But this really isn't enough for your purposes, so I modified it to be
sub counter {
my @word = split '', shift;
my @possible = split '', shift;
my $counter = 0;
for (0..$#word) {
my $ascii_diff = ord($word[$_]) - ord($possible[$_]);
$counter += $ascii_diff < 0 ? $ascii_diff * -1 : $ascii_diff;
# 'coz I dunno if there's a function for |$num|
}
return $counter;
}
So, you could do
@list_of_words = sort { counter($your_word, $b) <=> counter($your_word,
$a) } @list_of_words;
At least I think you could...(goes away)...Checked it, and it seems to
work.
'transient' was at the top of the list when I pumped in 'transeint',
anyway.
Actually, this method should really bias towards a single typo better
(eg, if one letter was z when it should have been a, the counter sub
would return a big ish diff, bigger than if all the letters were just
one off). This might be done by $ascii_diff *= 100, or something. Not
sure.
Hope this gives you a better idea of what to do
I really should be working.
Jasper
--
split//,'019617511192'.
'17011111610114101114'.
'21011141011840799901'.
'17101174';
foreach(0..
$#_){$_[$_
++]^=$_[$_
--]^=$_[$_
]^=$_[++ $_]if!($_%
2)}$g.=$_ ,chr($g)=~
/(\w)/&&($o.=$1and
$g='')foreach@_;
print"$o\n"
------------------------------
Date: Tue, 24 Jul 2001 12:10:44 +0100
From: Jasper McCrea <jasper@guideguide.com>
Subject: Re: Sorting an array of strings by 'closeness' to another string
Message-Id: <3B5D57B4.5AB82E50@guideguide.com>
Jasper McCrea wrote:
>
> jbp wrote:
> >
>
> Actually, this method should really bias towards a single typo better
> (eg, if one letter was z when it should have been a, the counter sub
> would return a big ish diff, bigger than if all the letters were just
> one off). This might be done by $ascii_diff *= 100, or something. Not
> sure.
Thinking about this a bit more, and what you really want, I think, is a
method that returns based on bad typo on a QWERTY keyboard.
so you'd get
%qwerty = { a => { a => 0,
b => 5,
c => 3,
d => 2,
e => 2,
....
},
b => { a => 5,
b => 0,
c => 2,
....
c => ..
d => ..
};
etc. etc. You get the idea. And instead of saying ascii_diff (like I had
in my previous reply), you'd say
$qwerty_diff = $qwerty{$your_letter}{$word_letter};
and total that for each letter of the words.
(there's a good exercise in here of writing a sub to work out the
$qwerty_diff knowing the keyboard 'coordinates' of each key, but I can't
be arsed right now).
Transposition errors also being common, you'd have to have a method to
deal with those, too.
Say,
sub counter {
my @word = split '', shift;
my @possible = split '', shift;
my $counter = 0;
for (0..$#word) {
next if $word{$_} eq $possible{$_]; # we won't be adding to counter
here
my $qwerty_diff = $qwerty{$word{$_}}{$possible{$_}};
# make transposition true if it looks like a transposition error.
my $transposition = $word{$_} eq $possible{$_ -1} ? 1 :
( $word{$_} eq $possible{$_ + 1} ? 1 : 0);
# add the smaller of $qwe and $trans to total
$counter += $transposition < $qwerty_diff ?
$transposition : $qwerty_diff;
}
return $counter;
}
I haven't, and I'm not about to test any of this. And I'm pretty sure
that if I've come up with this crap in two minutes, there's far better
code out there to do what you want exactly.
Jasper
--
split//,'019617511192'.
'17011111610114101114'.
'21011141011840799901'.
'17101174';
foreach(0..
$#_){$_[$_
++]^=$_[$_
--]^=$_[$_
]^=$_[++ $_]if!($_%
2)}$g.=$_ ,chr($g)=~
/(\w)/&&($o.=$1and
$g='')foreach@_;
print"$o\n"
------------------------------
Date: 24 Jul 2001 12:43:01 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Sorting an array of strings by 'closeness' to another string
Message-Id: <9jjqgl$mju$6@mamenchi.zrz.TU-Berlin.DE>
According to Logan Shaw <logan@cs.utexas.edu>:
> In article <f06plts0n3rdpig7u2moer3b7ess0s9e52@4ax.com>,
> jbp <jpixton@dircon.co.uk> wrote:
> >Howdy
> >
> >I have a string containing, say, 'transeint' (sic). After some
> >massaging the string a little, then grepping a dictionary, then some
> >more massaging, i get an array of strings like so:
> >
> >anastasia
> >anisettes
> >anteaters
> >assassins
> >assistant
> >easterner
> :
> :
> >testiness
> >transient
> >treatises
> >trinities
> >triteness
> >
> >Now, what I want to do is sort this array into an order such that the
> >ones near the top of the array (viewing it as a stack) are:
> >
> >transient
> >treatises
> >trinities
> >... etc
>
> If you're going to sort in the traditional sense, then you have to have
> a function that can always compare two items and tell you which one
> comes first. In other words, you have to have an ordering defined.
> In other other words, I'm not sure what you're trying to accomplish.
Supposing there is a distance function on strings, this would make
sense:
sort { distance( $a, 'transient} <=> distance( $b, 'transient') }, @l;
Of course, it doesn't define a unique sequence, but it will bring
the "closest" matches to the front.
> >How would I go about this? I've looked at String::Approx and one of
> >the functions would be ideal, except I can't install the module on my
> >host (the goddamn service provider won't do it).
>
> "The service provider won't" and "I can't" aren't equivalent,
> especially if you can login to the web server.
True. perldoc -q "own module" shows how it's done.
Anno
------------------------------
Date: 24 Jul 2001 11:22:51 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Split Question
Message-Id: <9jjlqb$mju$3@mamenchi.zrz.TU-Berlin.DE>
According to Eric Bohlman <ebohlman@omsdev.com>:
[...]
> while (<INPUT>) {
> chomp;
> while (tr/,// < 41) {
> $_ .= <INPUT>;
> chomp;
> }
> my @array = split(/,/,$_);
> # process the fields
> }
>
> Note that this will go into an infinite loop if the last record is
> incomplete.
Put "last if eof INPUT;" as the first line of the inner loop to
fix that.
Anno
------------------------------
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 1366
***************************************