[19769] in Perl-Users-Digest
Perl-Users Digest, Issue: 1964 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Oct 19 14:05:57 2001
Date: Fri, 19 Oct 2001 11:05:06 -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: <1003514706-v10-i1964@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Fri, 19 Oct 2001 Volume: 10 Number: 1964
Today's topics:
Re: alarm and put a timeout on a perl program an its sh (Andrew Hutchinson)
Re: Attach a file to email (Andrew Hutchinson)
Re: Can't get the output for shell command from CGI (Carfield Yim)
Re: Case-insensitive Command Line Args (Martien Verbruggen)
Classified ads script - beta testing (Greg)
Re: Classified ads script - beta testing (Miko O'Sullivan)
Re: Elegant Code - Indexing Letters <comdog@panix.com>
Re: extract file extention from file name <peter_icaza@REMOVE2REPLYuhc.com>
Re: extract file extention from file name <bernard.el-hagin@lido-tech.net>
Re: extract file extention from file name <jurgenex@hotmail.com>
Re: extract file extention from file name <peter_icaza@REMOVE2REPLYuhc.com>
Re: file upload in perl <matthew@perthonline.net>
Re: HELP: removing all references to an array (Tad McClellan)
Re: HELP: removing all references to an array (Harsh Pandey)
How can you sort a CSV file by diffrent fields ?? <thechile@ntlworld.com>
Re: How can you sort a CSV file by diffrent fields ?? <rich@21cws.com>
Re: need ideas for two-way associative array <tzz@beld.net>
Parsing/Returning Variables To/From Subroutines* slightlysprintingdog@ntlworld.com
Re: Perl remote communication - Is it tcp port, netbios <ra990866@ic.unicamp.br>
Problem with perl 5.6.1 and MCPAN <wusyk@americasm01.nt.com>
Re: use strict; (Tad McClellan)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 19 Oct 2001 16:38:45 GMT
From: andrew.hutchinson@mcmail.vanderbilt.edu (Andrew Hutchinson)
Subject: Re: alarm and put a timeout on a perl program an its shell childs ?
Message-Id: <3bd05508.588733468@news.vanderbilt.edu>
The Perl Cookbook has a good way to do this - recipe 16.21 in my
edition.
Basically, you install $SIG{ALARM} to call die, set an alarm, and then
eval the code that you want to timeout. The alarm will call die, but
since the code is eval'd it won't actually kill the process, but
rather only the eval block. You can look at the eval error ($@) that
you installed to make sure that timeout killed your block, and move on
from there.
For more details, buy the Cookbook (O'Reilly book w/ Bighorn Sheep on
the front) - it's worth every penny and then some.
Andrew
On Fri, 19 Oct 2001 14:50:50 +0200, "Gildas PERROT"
<perrot@NOSPAM.fluxus.net> wrote:
>Hi,
>
>I want to put a timeout on a perl program and its shell child. In order to
>kill both of them, I use "exec" but in that cas, I can set the SIG{ALRM}
>message which by default "Alarm clock". How can I do that ? When using
>system or '' to execute the shell command, this one is not killed at the
>timeout.
>
>Here is my perl code :
>
>$timeout = 1;
>$SIG{'ALRM'} = "TIMEOUT !";
>
>alarm($timeout);
>exec("/shell_cmd");
>alarm(0);
>
>Thanks in advance for your help. Gildas.
>
>
------------------------------
Date: Fri, 19 Oct 2001 16:26:29 GMT
From: andrew.hutchinson@mcmail.vanderbilt.edu (Andrew Hutchinson)
Subject: Re: Attach a file to email
Message-Id: <3bd05416.588491593@news.vanderbilt.edu>
Thanks to all - I used MIME::Lite, and it worked like a dream.
Thanks again,
Andrew
On Fri, 19 Oct 2001 06:27:55 GMT, Michael Budash <mbudash@sonic.net>
wrote:
>In article <3bcfaa3f.24873756@news.vanderbilt.edu>, ahutch@macatawa.org
>wrote:
>
>> I'm trying to add functionality to a script that generates several
>> report files as output. Currently, I have the reports formatted in
>> HTML and posting to a intranet site. However, several people would
>> like the files emailed directly to them.
>>
>> I've sent email from scripts before by binding a handle to sendmail,
>> piping to sendmail, and using Net::SMTP and Mail::Mailer. However, I
>> can't seem to find anything on CPAN that allow a file _attachment_.
>>
>> Anybody know of anything like this?
>
>use MIME::Lite;
>
>hth-
>--
>Michael Budash ~~~~~~~~~~ mbudash@sonic.net
------------------------------
Date: 19 Oct 2001 07:47:50 -0700
From: carfield@programmer.net (Carfield Yim)
Subject: Re: Can't get the output for shell command from CGI
Message-Id: <c40dea5f.0110190647.40f8f78@posting.google.com>
> Try the code above, and tell me what your results are.
A blank page with no warning, I think replace exec() to system() might
help. Anyway, thx for your help
------------------------------
Date: Sat, 20 Oct 2001 00:13:13 +1000
From: mgjv@tradingpost.com.au (Martien Verbruggen)
Subject: Re: Case-insensitive Command Line Args
Message-Id: <slrn9t0d7p.5ep.mgjv@martien.heliotrope.home>
On Fri, 19 Oct 2001 08:49:14 +0000 (UTC),
Joe Smith <inwap@best.com> wrote:
>
> getopts('ivcle:IVCLE:',\%opt); # List all valid options here
> $opt{i} = $opt{I} if exists $opt{I};
> $opt{v} = $opt{V} if exists $opt{V};
> $opt{c} = $opt{C} if exists $opt{C};
> $opt{l} = $opt{L} if exists $opt{L};
> $opt{e} = $opt{E} if exists $opt{E};
map { $opt{"\L$_"} = $opt{$_} if exists $opt{$_} } qw/I V C L E/;
Yep, this is one of them cases where you can't use a for modifier, and I
just don't want to waste four lines where one will do just as fine.
Martien
--
|
Martien Verbruggen | The world is complex; sendmail.cf
| reflects this.
|
------------------------------
Date: 19 Oct 2001 07:32:24 -0700
From: google@uksites4all.co.uk (Greg)
Subject: Classified ads script - beta testing
Message-Id: <62127286.0110190632.3b4bc193@posting.google.com>
Hi, all.
I've just completed a free classified ads script using Perl/cgi.
I would appreciate it if as many people, on as many platforms, with as many
browsers as possible, could test it out, and report problems back to me.
If you post a dummy message for testing purposes, please put something to
that effect in the message or subject line.
The site is at http://www.uksites4all.co.uk/freeads.html
Many thanks
Regards,
Greg Smith
freeads@uksites4all.co.uk
------------------------------
Date: 19 Oct 2001 10:21:49 -0700
From: miko@idocs.com (Miko O'Sullivan)
Subject: Re: Classified ads script - beta testing
Message-Id: <db27ea77.0110190921.49efb6ce@posting.google.com>
google@uksites4all.co.uk (Greg) wrote in message news:<62127286.0110190632.3b4bc193@posting.google.com>...
>
> I've just completed a free classified ads script using Perl/cgi.
Are the ads free or is the script free (or both)? If the script is
free (as in beer *and* speech) I'd be happy to give the script a quick
lookover.
-Miko
------------------------------
Date: Fri, 19 Oct 2001 10:07:32 -0400
From: brian d foy <comdog@panix.com>
Subject: Re: Elegant Code - Indexing Letters
Message-Id: <comdog-3C61D6.10073219102001@news.panix.com>
In article <9a223a9d.0110181506.6372084b@posting.google.com>,
iNeverReadAnythingSentToMe@hotmail.com (David Filmer) wrote:
> But, now suppose that $var is a TWO-character alphabetic,
> and it indexes like this: AA >> AB, AB >> AC, etc, AZ >> BA (just as
> if it were a base-26 numbering system with only alphabetic digits).
> ($a, $b) = ($var =~ /(.)(.)/);
> if ($b =~ /Z/) {$a =~ tr/A-Z/B-ZA/}
> $b =~ tr/A-Z/B-ZA/;
> $var = "$a$b";
> It works, but it's not elegant any more. How could I do this
> elegantly?
$n = $n eq 'ZZ' ? 'AA' : ++$n;
--
brian d foy <comdog@panix.com> - Perl services for hire
CGI Meta FAQ - http://www.perl.org/CGI_MetaFAQ.html
Troubleshooting CGI scripts - http://www.perl.org/troubleshooting_CGI.html
------------------------------
Date: Fri, 19 Oct 2001 09:44:19 -0400
From: peter <peter_icaza@REMOVE2REPLYuhc.com>
Subject: Re: extract file extention from file name
Message-Id: <3BD02E33.C909A608@REMOVE2REPLYuhc.com>
Bernard El-Hagin wrote:
> > $fName =~ s/(^.*\.[^\.]*)\.\d$/$1/;
>
> I guess you want the opposite of what you actually specified since
> "lope off and RETAIN the .1" means, at least to me, that you want
> to cut off the .1 and keep it.
actually i split the extention off before hand.
> Anyway, this is Perl so I guess
> DWIM is in effect. :-) Your expression can be simplified to:
DWIM = dimwit? :)
>
>
> $fName =~ s/^(.*)\.\d$/$1/;
>
> or, even better:
>
> $fName =~ s/\.\d$//;
i guess i having been reading too much friedl lately :)
thanks,
peter
------------------------------
Date: 19 Oct 2001 14:02:48 GMT
From: Bernard El-Hagin <bernard.el-hagin@lido-tech.net>
Subject: Re: extract file extention from file name
Message-Id: <slrn9t0c7d.ec9.bernard.el-hagin@gdndev25.lido-tech>
On Fri, 19 Oct 2001 09:44:19 -0400, peter <peter_icaza@REMOVE2REPLYuhc.com>
wrote:
> Bernard El-Hagin wrote:
>
>> > $fName =~ s/(^.*\.[^\.]*)\.\d$/$1/;
>>
>> I guess you want the opposite of what you actually specified since
>> "lope off and RETAIN the .1" means, at least to me, that you want
>> to cut off the .1 and keep it.
>
> actually i split the extention off before hand.
>
>> Anyway, this is Perl so I guess
>> DWIM is in effect. :-) Your expression can be simplified to:
>
> DWIM = dimwit? :)
"Do what I meant", actually. :)
>> $fName =~ s/^(.*)\.\d$/$1/;
>>
>> or, even better:
>>
>> $fName =~ s/\.\d$//;
>
> i guess i having been reading too much friedl lately :)
Not enough Friedl seems more accurate. ;)
Cheers,
Bernard
------------------------------
Date: Fri, 19 Oct 2001 08:05:19 -0700
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: extract file extention from file name
Message-Id: <3bd04130@news.microsoft.com>
"peter" <peter_icaza@REMOVE2REPLYuhc.com> wrote in message
news:3BD010B6.D31AE32E@REMOVE2REPLYuhc.com...
> hi,
> i have a filename in a string var that looks like this
>
> long.file.name.1
>
> i want to lope off and retain the .1.
use File::Basename;
will do the job quite nicely and without any hassle.
jue
------------------------------
Date: Fri, 19 Oct 2001 13:20:30 -0400
From: peter <peter_icaza@REMOVE2REPLYuhc.com>
Subject: Re: extract file extention from file name
Message-Id: <3BD060DE.61691E2@REMOVE2REPLYuhc.com>
"Jürgen Exner" wrote:
> "peter" <peter_icaza@REMOVE2REPLYuhc.com> wrote in message
> news:3BD010B6.D31AE32E@REMOVE2REPLYuhc.com...
> > hi,
> > i have a filename in a string var that looks like this
> >
> > long.file.name.1
> >
> > i want to lope off and retain the .1.
>
> use File::Basename;
>
> will do the job quite nicely and without any hassle.
>
> jue
that seems to want to be passed the path to the file and it will parse
that. it didnt wanna work with just the filename. i may have done
something wrong but....
thanks in any event.
peter
------------------------------
Date: Sat, 20 Oct 2001 01:10:15 +0900
From: "Matthew Lock" <matthew@perthonline.net>
Subject: Re: file upload in perl
Message-Id: <9qpjb3$1in$1@newsflood.tokyo.att.ne.jp>
You don't need to write your own upload function. Perl comes with a CGI
module ( http://stein.cshl.org/WWW/software/CGI/ ) Which includes the
ability to receive uploaded files. It works fine on IIS, here is the info on
uploading files.
Also O'Reily's "Programming CGI with Perl", has a a very thorough section on
uploading files.
http://www.amazon.com/exec/obidos/ASIN/1565924193/prestigetraining
"Steven" <steven@steven.com> wrote in message
news:3bce91a5$0$7109$4d4efb8e@news.be.uu.net...
> Dear,
>
> Certain perl scripts (.pl) are located in their working directory on the C
> drive. Inside the perl script, we can change to another directory on that
> same C drive.
>
> However, changing to the D drive seems impossible.
>
> Question: how do we program script to be able to write a file on another
> drive than the one the working directory is located on?
>
> Attaced are the working script (file_upload_ok.pl) and the script that
tries
> to write to another drive (file_upload_NOK.pl).
>
> Thanks beforehand for a solution or a tip.
>
> Els
>
>
>
>
>
> SCRIPTS:
>
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
> - - - - - - - - - - - - - - - - - - - - - -
>
> file_upload_NOK.pl
>
> - - - - - - - - -
>
>
> #!/usr/local/bin/perl
>
############################################################################
> #####
> # cgi script to retrieve and store a file to a directory location
> #
> #
> # Special note: Had major difficulty getting EOF from STDIN if using IIS.
> The
> # workaround is to get exactly the number of bytes needed
for
> a
> # file and not wait for an EOF. This wasn't a problem for
> Apache.
>
############################################################################
> #####
>
>
############################################################################
> #####
> # routine to log activity to a file.
> ##########################################################################
##
> #####
> sub do_log
> {
> my($filename, $pref, $msg) = @_;
>
> $LOGFILE = ">>file_upload.log";
>
> # Open/Print/Close the log file. Don't die if you can't open the log
> file.
> $tod = scalar localtime;
> open (LOGF, $LOGFILE);
> printf LOGF "%s: %s: %s %s\n", $tod, $pref, $filename, $msg;
> close LOGF;
> }
>
>
############################################################################
> #####
> # For testing we put out a message to say we are working ok.
>
############################################################################
> #####
> sub not_a_post
> {
> my($meth) = @_;
> if ($meth eq "GET") {
> print "Content-type: text/html\n\n";
> print "<HTML>\n";
> print "<HEAD>\n";
> print "<TITLE>Successful Connection</TITLE>\n";
> print "</HEAD>\n";
> print "<BODY>\n";
> print "<H1>Congratulations, file_upload.pl is correctly
> installed!!</H1>\n";
> print "</BODY>\n";
> print "</HTML>\n";
> } else { # from command line
> print "Congratulations, Perl is correctly configured!\n";
> }
> }
>
>
############################################################################
> #####
> # Main routine
>
############################################################################
> #####
> {
> use Env qw(REQUEST_METHOD);
> $BLOCK_SIZE=16384; # size of buffer
>
>
> binmode STDIN; # must be in binary mode!!
> if ($REQUEST_METHOD ne "POST") {
> not_a_post("$REQUEST_METHOD");
> close(STDOUT);
> exit;
> }
>
> print "Content-type: text/html\n\n";
> chomp($TEMP_FILE = <STDIN>);
> chomp($DIR = <STDIN>);
> chomp($SIZE = <STDIN>);
>
> # delete the carriage return that comes from pc land.
>
> $TEMP_FILE =~ s/\r//;
> $DIR =~ s/\r//;
> $SIZE =~ s/\r//;
>
> $OUTFILE = "$DIR/$TEMP_FILE";
>
> do_log ($OUTFILE, "Started File", "Size($SIZE)");
> open (OUT, ">$OUTFILE") or do_log ($OUTFILE, "Outfile Error", "Error
> opening file") and print "NOK" and close(STDOUT) and exit;
>
> binmode OUT; # must also be in binary mode!!
>
> $buf = '';
> $len = 0;
>
> # get our initial buffer size
> $buflen = $BLOCK_SIZE;
> if ($SIZE < $buflen) {
> $buflen = $SIZE;
> }
>
> # read the file till finished
> while ($len = read STDIN, $buf, $buflen){
> #do_log ($OUTFILE, "Got Data ", "Length:$len");
> syswrite(OUT, $buf, $len);
> $SIZE -= $len;
> if ($SIZE < $buflen) {
> $buflen = $SIZE;
> }
> }
>
> do_log ($OUTFILE, "Finished File", "Successful!!");
>
> # Send a OK back for ok.
> print "OK";
> close(STDOUT);
>
> }
>
>
############################################################################
> #####
>
############################################################################
> #####
>
############################################################################
> #####
>
> - - - - - - - - -
>
> file_upload_ok.pl:
>
> - - - - - - - - - -
>
> #!/usr/local/bin/perl
>
############################################################################
> #####
> # cgi script to retrieve and store a file to a directory location
> #
> #
> # Special note: Had major difficulty getting EOF from STDIN if using IIS.
> The
> # workaround is to get exactly the number of bytes needed
for
> a
> # file and not wait for an EOF. This wasn't a problem for
> Apache.
>
############################################################################
> #####
>
>
############################################################################
> #####
> # routine to log activity to a file.
>
############################################################################
> #####
> sub do_log
> {
> my($filename, $pref, $msg) = @_;
>
> $LOGFILE = ">>file_upload.log";
>
> # Open/Print/Close the log file. Don't die if you can't open the log
> file.
> $tod = scalar localtime;
> open (LOGF, $LOGFILE);
> printf LOGF "%s: %s: %s %s\n", $tod, $pref, $filename, $msg;
> close LOGF;
> }
>
>
############################################################################
> #####
> # For testing we put out a message to say we are working ok.
>
############################################################################
> #####
> sub not_a_post
> {
> my($meth) = @_;
> if ($meth eq "GET") {
> print "Content-type: text/html\n\n";
> print "<HTML>\n";
> print "<HEAD>\n";
> print "<TITLE>Successful Connection</TITLE>\n";
> print "</HEAD>\n";
> print "<BODY>\n";
> print "<H1>Congratulations, file_upload.pl is correctly
> installed!!</H1>\n";
> print "</BODY>\n";
> print "</HTML>\n";
> } else { # from command line
> print "Congratulations, Perl is correctly configured!\n";
> }
> }
>
>
############################################################################
> #####
> # Main routine
>
############################################################################
> #####
> {
> use Env qw(REQUEST_METHOD);
> $BLOCK_SIZE=16384; # size of buffer
>
>
> binmode STDIN; # must be in binary mode!!
> if ($REQUEST_METHOD ne "POST") {
> not_a_post("$REQUEST_METHOD");
> close(STDOUT);
> exit;
> }
>
> print "Content-type: text/html\n\n";
> chomp($TEMP_FILE = <STDIN>);
> chomp($DIR = <STDIN>);
> chomp($SIZE = <STDIN>);
>
> # delete the carriage return that comes from pc land.
>
> $TEMP_FILE =~ s/\r//;
> $DIR =~ s/\r//;
> $SIZE =~ s/\r//;
>
> $OUTFILE = "$TEMP_FILE";
> $DIRECTORY = "$DIR";
>
> do_log ($OUTFILE, "Started File", "Size($SIZE)");
> chdir("$DIRECTORY") or do_log ($DIRECTORY, "Directory Error", "Could
not
> change Dir") and print "NOK" and close(STDOUT) and exit;
> open (OUT, ">>$OUTFILE") or do_log ($OUTFILE, "Outfile Error", "Error
> opening file") and print "NOK" and close(STDOUT) and exit;
>
> binmode OUT; # must also be in binary mode!!
>
> $buf = '';
> $len = 0;
>
> # get our initial buffer size
> $buflen = $BLOCK_SIZE;
> if ($SIZE < $buflen) {
> $buflen = $SIZE;
> }
>
> # read the file till finished
> while ($len = read STDIN, $buf, $buflen){
> do_log ($OUTFILE, "Got Data ", "Length:$len");
>
> syswrite(OUT, $buf, $len);
> $SIZE -= $len;
> if ($SIZE < $buflen) {
> $buflen = $SIZE;
> }
> }
>
> do_log ($OUTFILE, "Finished File", "Successful!!");
>
> # Send a OK back for ok.
> print "OK";
> close(STDOUT);
>
> }
>
>
############################################################################
> #####
>
############################################################################
> #####
>
############################################################################
> #####
>
>
>
>
>
>
>
------------------------------
Date: Fri, 19 Oct 2001 15:05:50 GMT
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: HELP: removing all references to an array
Message-Id: <slrn9t0csa.tjm.tadmc@tadmc26.august.net>
Bruno Boettcher <bboett@bboett.dyndns.org> wrote:
>i just played around with delete....
>and i have 2 problems with it:
>
>delete removes only the reference in a hash, is it possible to actually
>let it destroy the data pointed by an array and thus all references
>pointing on it?
delete() is not the way to do that. (unless you delete $tata{titi} too)
Letting all references to the data go out of scope is the
"usual way" of reclaiming memory.
"Coping with Scoping" may help with that:
http://perl.plover.com/FAQs/Namespaces.html
>if i have
>$toto{titi} = [1,2,3];
>$tata{titi} = $toto{titi};
>if i make
>delete $toto{titi};
>only the reference to the array is destroyed, not the array, and the
>reference form the other hash is still valid...
Because the "reference count" for the anon array is not yet zero,
it is still one (because $tata{titi} still refers to it).
>that's nice most of the time, but sometimes i would like ot be able to
>destroy complately every race of the array, is this easily achievable?
Yes, when all references to the array (there are 2 in your example)
go out of scope, the anonymous array's memory will be garbage collected.
>and since i am at it...
>delete is handy to destroy positionsin a hash, is there a similar easy
>way to remove a position in an array?
perldoc -f splice
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 19 Oct 2001 10:27:08 -0700
From: harsh.pandey@bms.com (Harsh Pandey)
Subject: Re: HELP: removing all references to an array
Message-Id: <abc68a76.0110190927.7dbb5319@posting.google.com>
Try
splice @{$toto{titi}}, 0;
Harsh
bboett@bboett.dyndns.org (Bruno Boettcher) wrote in message news:<9qp094$9tk$1@neon.noos.net>...
> hello
> i just played around with delete....
> and i have 2 problems with it:
>
> delete removes only the reference in a hash, is it possible to actually
> let it destroy the data pointed by an array and thus all references
> pointing on it?
>
> if i have
> $toto{titi} = [1,2,3];
> $tata{titi} = $toto{titi};
> if i make
> delete $toto{titi};
> only the reference to the array is destroyed, not the array, and the
> reference form the other hash is still valid...
>
> that's nice most of the time, but sometimes i would like ot be able to
> destroy complately every race of the array, is this easily achievable?
>
> and since i am at it...
> delete is handy to destroy positionsin a hash, is there a similar easy
> way to remove a position in an array? at the moment i foreach through
> the array and write all positions less the one to be destroyed into a
> new array and reset all references to it later... quite cumbersome....
------------------------------
Date: Fri, 19 Oct 2001 15:03:59 +0100
From: "Scott" <thechile@ntlworld.com>
Subject: How can you sort a CSV file by diffrent fields ??
Message-Id: <1nWz7.16165$sF.1458542@news2-win.server.ntlworld.com>
Hi,
I have a CSV file database that i am reading into @DATA and then doing
foreach $rec (@DATA)
{
chomp($rec);
($a,$b,$c)=split(/\,/,$rec);
print "$a,$b,$c\n";
}
I would like to be able to sort them by each field but by using @sorted =
sort @DATA; it just sorts based on the first field.
Can anyone suggest away of specifiying field a,b or c and then sorting the
data based on that.
thanks,
------------------------------
Date: Fri, 19 Oct 2001 10:29:23 -0500
From: "RH" <rich@21cws.com>
Subject: Re: How can you sort a CSV file by diffrent fields ??
Message-Id: <WxXz7.11516$K8.759454@e3500-atl1.usenetserver.com>
"Scott" <thechile@ntlworld.com> wrote in message news:1nWz7.16165
$sF.1458542@news2-win.server.ntlworld.com...
> Hi,
>
> I have a CSV file database that i am reading into @DATA and then doing
>
> foreach $rec (@DATA)
> {
> chomp($rec);
> ($a,$b,$c)=split(/\,/,$rec);
> print "$a,$b,$c\n";
>
> }
>
> I would like to be able to sort them by each field but by using @sorted =
> sort @DATA; it just sorts based on the first field.
>
> Can anyone suggest away of specifiying field a,b or c and then sorting the
> data based on that.
>
> thanks,
Personally, I would turn @DATA into a list of lists as I was reading from
the file. Then it would be simple to specify which field to use to sort the data.
If you know that none of the fields will need to be sorted numerically,
something like the code below would work (likewise, if you knew that
all fields would sort numerically, you would use the <=> operator).
my @data;
my $field_to_sort = 2;
while (<DATA>) {
chomp;
push @data, [split ','];
}
my @sorted = sort {$a->[$field_to_sort] cmp $b->[$field_to_sort]} @data;
foreach (@sorted) {
print join ',', @{$_};
print "\n";
}
__DATA__
q,12,w3w
a,3,44
p,42,4d
If you have some fields that would need to be sorted numerically and
some that need to be sorted asciibetically, you could use something
like the code below.
my @data;
my $field_to_sort = 2;
my $is_numeric = 1;
while (<DATA>) {
chomp;
push @data, [split ','];
$is_numeric = 0 if $data[$#data]->[$field_to_sort] =~ /\D/;
}
my @sorted = sort {($is_numeric) ? $a->[$field_to_sort] <=> $b->[$field_to_sort] :
$a->[$field_to_sort] cmp $b->[$field_to_sort]} @data;
Rich
------------------------------
Date: Fri, 19 Oct 2001 11:01:21 -0400
From: Ted Zlatanov <tzz@beld.net>
Subject: Re: need ideas for two-way associative array
Message-Id: <m3hesv3c8u.fsf@heechee.beld.net>
Benjamin Goldberg <goldbb2@earthlink.net> writes:
> Not all that much extra work:
>
> my (%forward, %backward);
>
> # A1 -> { B1, B2, B3 }
> $forward{A1}{$_} = $backward{$_}{A1} = 1 for qw(B1 B2 B3);
> # A2 -> { B1, B3 }
> $forward{A2}{$_} = $backward{$_}{A2} = 1 for qw(B1 B3);
>
> local $" = ", ";
> while( my ($a, $b) = each %forward ) {
> print "$a -> ( @{[keys %$b]} )\n";
> }
> print "\n";
> while( my ($b, $a) = each %backward ) {
> print "$b -> ( @{[keys %$a]} )\n";
> }
Essentially that's what I'm doing now, yes. But I want it to be done
behind the scenes for me, by a module. If there isn't one in CPAN,
then I'll write it myself :) Doing it myself every time is not elegant.
Thanks
Ted
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 80,000 Newsgroups - 16 Different Servers! =-----
------------------------------
Date: Fri, 19 Oct 2001 16:51:20 +0100
From: slightlysprintingdog@ntlworld.com
Subject: Parsing/Returning Variables To/From Subroutines*
Message-Id: <3BD04BF8.D0C984A6@ntlworld.com>
*with a twist.....
I'm trying to make some code scroll a text string in a status window.
From the status windows subroutine I call the function scroll as follows
:
| after(100, \&scroll);
where scroll is :
| sub scroll {
| $sim_status =~ /(.)(.*)/;
| $sim_status = "$2$1";
| after(100, \&scroll);
| }
However, I need to parse the subroutine some variables, namely
$sim_status and also return its new value to the status window
subroutine.
I've tried :
| after(100, [ \&scroll, $sim_status ]);
And it works fine, I then added the line :
return $sim_status;
to the scroll subroutine. However, how then do I alter the first call
to accept the returned value for $sim_status ?
I can appreciate that this explanation is a bit poor but its the best I
can do after a (very) long day at work ! : )
Is that enough information ?
Any ideas ?
Thanks
Andy
------------------------------
Date: Fri, 19 Oct 2001 11:04:23 -0200
From: Alessandro Augusto <ra990866@ic.unicamp.br>
To: Pat Gunn <qc@apk.net>
Subject: Re: Perl remote communication - Is it tcp port, netbios or what?
Message-Id: <Pine.GSO.4.10.10110191058170.12470-100000@tigre.dcc.unicamp.br>
Right now, yes.
I use this module in some scripts, and I wonder, how does perl makes the
remote connection?
Yesterday, I was sniffing two computers with windump.
It seems that Perl uses RPC calls, because:
On the computer A, where I run the script to connect to the registry of
computer B (using tieregistry), the package is TCP using different ports
(1045 to 1050 on my tests).
On the computer B, where the registry was audited by the script on
computer A, the package is TCP always on port 445.
My goal is, how can i specify always the same port on the computer where I
execute the script? That way, i know that traffic from port xxx to port
445 is always the perl traffic of my script.
where do i specify that? I looked at the code of tieregistry.pm, but
didn't find anything yet. Any ideia?
thanks
alessandro
> You might have better luck if you phrase your question,
> "How does Win32::TieRegistry connect to remote systems?".
> Perl can use many different mechanisms to talk to remote
> systems, and it's only how Win32::TieRegistry works that
> interests you.
------------------------------
Date: Fri, 19 Oct 2001 11:00:52 -0400
From: "Usyk, Walter [SKY:1P67:EXCH]" <wusyk@americasm01.nt.com>
Subject: Problem with perl 5.6.1 and MCPAN
Message-Id: <3BD04024.41020753@americasm01.nt.com>
have installed the ActiveState Perl 5.6.1 on NT and now I want to use
perl -MCPAN -e shell to install packages. I get the following error when
using autobundle and some other commands. Does any one have an idea why
this is happening. I do not want to use PPM as I need to install some
packages that are not available through PPM.
Is my only choice doing it manually???
Thanks for your help.
cpan> autobundle
Fetching with LWP:
ftp://theoryx5.uwinnipeg.ca/pub/CPAN/authors/01mailrc.txt.gz
Going to read F:\.cpan\sources\authors\01mailrc.txt.gz
CPAN: Compress::Zlib loaded ok
Fetching with LWP:
ftp://theoryx5.uwinnipeg.ca/pub/CPAN/modules/02packages.details.txt.gz
Going to read F:\.cpan\sources\modules\02packages.details.txt.gz
Database was generated on Thu, 18 Oct 2001 13:33:06 GMT
Subroutine time2str redefined at
\\zmers01a\disk1\spiffcc\tools\winnt-4.0\perl\5.6.1\site\lib/HTTP\D
ate.pm line 23.
Subroutine str2time redefined at
\\zmers01a\disk1\spiffcc\tools\winnt-4.0\perl\5.6.1\site\lib/HTTP\D
ate.pm line 35.
Subroutine parse_date redefined at
\\zmers01a\disk1\spiffcc\tools\winnt-4.0\perl\5.6.1\site\lib/HTTP
\Date.pm line 83.
Subroutine time2iso redefined at
\\zmers01a\disk1\spiffcc\tools\winnt-4.0\perl\5.6.1\site\lib/HTTP\D
ate.pm line 239.
Subroutine time2isoz redefined at
\\zmers01a\disk1\spiffcc\tools\winnt-4.0\perl\5.6.1\site\lib/HTTP\
Date.pm line 249.
CPAN: HTTP::Date loaded ok
Fetching with LWP:
ftp://theoryx5.uwinnipeg.ca/pub/CPAN/modules/03modlist.data.gz
Going to read F:\.cpan\sources\modules\03modlist.data.gz
Package namespace installed latest in CPAN file
AnyDBM_File undef undef
G/GS/GSAR/perl-5.6.1.tar.gz
Apache::SOAP 0.51 0.51
K/KU/KULCHENKO/SOAP-Lite-0.51.tar.gz
--
Walter Usyk - Tools Developer
Software Development Environment Tools (1P67)
email: wusyk@nortelnetworks.com
ESN: 398-4603
Tel: (613) 768-4603
------------------------------
Date: Fri, 19 Oct 2001 15:05:51 GMT
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: use strict;
Message-Id: <slrn9t0d8h.tjm.tadmc@tadmc26.august.net>
perl misk <perlmisk@yahoo.co.uk> wrote:
>the 'strict' pragma doesn't (appear) to catch an error if I define two
>subroutines with the same name.
Sometimes you _want_ to redefine a subroutine.
>Instead it uses the last one it
>finds.
>
>Is there a reason for this behaviour
I expect that it is more flexible the way it is.
Now we can redefine a function if we want to.
That would become impossible if it was a fatal error.
Perl would become less powerful.
>or is it worth me submitting it
>as a suggestion for futuer versions?
>
>Where would it fit best 'strict' or 'warnings'?
^^^^^^^^
You don't have to do anything as it is *already* in there.
You should always enable warnings when developing Perl code!
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
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 1964
***************************************