[17538] in Perl-Users-Digest
Perl-Users Digest, Issue: 4958 Volume: 9
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Nov 23 21:10:38 2000
Date: Thu, 23 Nov 2000 18:10:14 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <975031813-v9-i4958@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Thu, 23 Nov 2000 Volume: 9 Number: 4958
Today's topics:
Sorting data based on 2 key fields <junk@yada.net>
Re: Sorting data based on 2 key fields (Tad McClellan)
Re: Sorting data based on 2 key fields (Tad McClellan)
Re: Sorting data based on 2 key fields <junk@yada.net>
Re: Sorting data based on 2 key fields (Anno Siegel)
Re: Sorting data based on 2 key fields (Tad McClellan)
Re: Stripping Comma's and Spaces <dvbryan@_nospamplsHotmail.com>
Re: Subroutine Stuff??? (Honza Pazdziora)
TELE COMMUTE ONLY - PERL - Java JavaScript CSS Dre InternationalPERLjavaPRG@IJavaPerlPrg.com
Re: Tom Christiansons' 'style' (Tim Hammerquist)
Uploading .jpg files to server <DanYllanes@NetZero.net>
Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 23 Nov 2000 19:44:45 GMT
From: "Timothy Valis" <junk@yada.net>
Subject: Sorting data based on 2 key fields
Message-Id: <NseT5.231131$g6.105959247@news2.rdc2.tx.home.com>
I have an ASCII text file, (Database output) which I need to sort. I need
to select entire lines and sort on 2 column sets, numerically first on a 9
digit number in columns 4-12, then on a 2 digit number in columns 1-2.
After a few references to perl.com and the perl FAQs I can sort on the first
field, but cannot figure how to nest the second sort. Below is the code I
have so far, and a sample set of data. I am quite new to programming and
perl, so am having a difficult time understanding some of the coding. Any
help would be greatly appreciated.
P.S. I know that currently @idx1 serves no purpose, that is where I am
stuck...
------------------ Code thus far -----------------------
#!/usr/bin/perl -w
# sortdata.pl: a very simple sort application.
# This program reads a text file that is an employee
# database and sorts it it by SSN and ID number.
#
# Each record is on a single line. Each field in the
# record is a fixed length
# The database has two key fields:
# SSN in columns 4-12
# ID in columns 1-2
$InPut = 'OCSOdata.txt'; # The name of the initial database file
$OutPut = 'OCSOsorted.txt'; # The name of the output database file
# Open the input database file but quit if it doesn't exist
open(INDB, $InPut) or die "The input database $InPut could not be
opened.\n";
# Open the output database file in overwrite mode and quit if it fails
open(OUTDB, ">$OutPut") or die "The output database $OutPut could not be
opened.\n";
@Unsort = <INDB>; #Define an array of the unsorted elements
@idx1 = (); # define an empty array for first sort index
for (@Unsort) {
($item) = substr ($_, 0, 2); #populate index array with ID
push @idx1, ($item);
}
@idx2 = (); # define an empty array for second sort index
for (@Unsort) {
($item) = substr ($_, 3, 9); #populate idx2 array with SSN
push @idx2, ($item);
}
@sorted = @Unsort[ sort { $idx2[$a] cmp $idx2[$b] } 0 .. $#idx2]; #sort on
idx2
print OUTDB @sorted;
print "Program finished.\n";
-------------Sample INPUT Data----------------------
02S005502791Smith Constance LAWRENCE SMITH
M09021941SP
01S005502791Smith Constance 3845 Senegal Circle
02S023403436Murry John Cynthia Murry
F08231961SPY
02S006387612Jones Charles CYNTHIA JONES
F08151944SP
03S023462581Leonard Dennis BRITTANY LEONARD
F06121989CH
04S023462581Leonard Dennis KAITLYN LEONARD
F02041999CH
01S007622304Jacque Robert 8205 Sun Springs Circle Apt
01S023403436Murry John 1630 The Oaks Blvd
01S023462581Leonard Dennis 4148 Floralwood Court
01S006387612Jones Charles 12 Wagon Circle
02S023462581Leonard Dennis PEGGY LEONARD
F11221956SP
-------------Sample OUTPUT Data----------------------
01S005502791Smith Constance 3845 Senegal Circle
02S005502791Smith Constance LAWRENCE SMITH
M09021941SP
01S006387612Jones Charles 12 Wagon Circle
02S006387612Jones Charles CYNTHIA JONES
F08151944SP
01S007622304Jacque Robert 8205 Sun Springs Circle Apt
01S023403436Murry John 1630 The Oaks Blvd
02S023403436Murry John Cynthia Murry
F08231961SPY
01S023462581Leonard Dennis 4148 Floralwood Court
02S023462581Leonard Dennis PEGGY LEONARD
F11221956SP
03S023462581Leonard Dennis BRITTANY LEONARD
F06121989CH
04S023462581Leonard Dennis KAITLYN LEONARD
F02041999CH
------------------------------
Date: Thu, 23 Nov 2000 14:10:07 -0500
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: Sorting data based on 2 key fields
Message-Id: <slrn91qqsf.t5u.tadmc@magna.metronet.com>
Timothy Valis <junk@yada.net> wrote:
>I have an ASCII text file, (Database output) which I need to sort. I need
>to select entire lines and sort on 2 column sets, numerically first on a 9
>digit number in columns 4-12, then on a 2 digit number in columns 1-2.
>After a few references to perl.com and the perl FAQs I can sort on the first
>field,
Err yes, but you are not sorting "numerically" as you say above.
You are sorting as strings ( 'cmp' is for strings, '<=>' is for numbers).
>but cannot figure how to nest the second sort.
Add an "or <second comparison>" after the first comparison.
@sorted = @Unsort[ sort { $idx2[$a] <=> $idx2[$b] or
$idx1[$a] <=> $idx1[$b] } 0 .. $#idx2];
--
Tad McClellan SGML consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: Thu, 23 Nov 2000 14:15:18 -0500
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: Sorting data based on 2 key fields
Message-Id: <slrn91qr66.t5u.tadmc@magna.metronet.com>
Timothy Valis <junk@yada.net> wrote:
> @idx1 = (); # define an empty array for first sort index
> for (@Unsort) {
> ($item) = substr ($_, 0, 2); #populate index array with ID
> push @idx1, ($item);
> }
> @idx2 = (); # define an empty array for second sort index
> for (@Unsort) {
> ($item) = substr ($_, 3, 9); #populate idx2 array with SSN
> push @idx2, ($item);
> }
Rather than 2 loops that each do one thing, you should have
1 loop that does 2 things.
You seem to be sprinkling random unnecessary parenthesis around too.
You don't need a temporary variable either.
foreach (@Unsort) {
push @idx1, substr ($_, 0, 2);
push @idx2, substr ($_, 3, 9);
}
--
Tad McClellan SGML consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: Thu, 23 Nov 2000 20:37:10 GMT
From: "Timothy Valis" <junk@yada.net>
Subject: Re: Sorting data based on 2 key fields
Message-Id: <WdfT5.231231$g6.106011758@news2.rdc2.tx.home.com>
Thanks! It works great!, but I do not understand, why an or operand at that
location in the sorting caused the desired output. Could you shed a little
light on that?
"Tad McClellan" <tadmc@metronet.com> wrote in message
news:slrn91qqsf.t5u.tadmc@magna.metronet.com...
> Timothy Valis <junk@yada.net> wrote:
>
> >I have an ASCII text file, (Database output) which I need to sort. I
need
> >to select entire lines and sort on 2 column sets, numerically first on a
9
> >digit number in columns 4-12, then on a 2 digit number in columns 1-2.
> >After a few references to perl.com and the perl FAQs I can sort on the
first
> >field,
>
>
> Err yes, but you are not sorting "numerically" as you say above.
>
> You are sorting as strings ( 'cmp' is for strings, '<=>' is for numbers).
>
>
> >but cannot figure how to nest the second sort.
>
>
> Add an "or <second comparison>" after the first comparison.
>
>
> @sorted = @Unsort[ sort { $idx2[$a] <=> $idx2[$b] or
> $idx1[$a] <=> $idx1[$b] } 0 .. $#idx2];
>
>
>
> --
> Tad McClellan SGML consulting
> tadmc@metronet.com Perl programming
> Fort Worth, Texas
------------------------------
Date: 23 Nov 2000 21:04:47 -0000
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Sorting data based on 2 key fields
Message-Id: <8vk0pf$6lk$1@lublin.zrz.tu-berlin.de>
Timothy Valis <junk@yada.net> wrote in comp.lang.perl.misc:
>"Tad McClellan" <tadmc@metronet.com> wrote in message
>news:slrn91qqsf.t5u.tadmc@magna.metronet.com...
[surgical correction of jeopardiosis][1]
>> >but cannot figure how to nest the second sort.
>>
>>
>> Add an "or <second comparison>" after the first comparison.
>>
>>
>> @sorted = @Unsort[ sort { $idx2[$a] <=> $idx2[$b] or
>> $idx1[$a] <=> $idx1[$b] } 0 .. $#idx2];
>
>Thanks! It works great!, but I do not understand, why an or operand at that
>location in the sorting caused the desired output. Could you shed a little
>light on that?
Well, the part after the or jumps in when the first comparison doesn't
reach a decision. The value of "<=>" is 0 (hence boolean false) for
equal comparands, so the "or" evaluates its second operand. This is
the secondary comparison you want to make to break ties. Its value
becomes the value of "or", and that is what sort sees.
To read more about this behavior of "or" (and a few more Perl operators)
see perldoc perlop and search for "short-circuit".
Anno
[1] In other words, please don't slap your reply on top of the text
you're replying to. Trim the original text to keep enough context
and put your reply behind what you are replying to. I have
rearranged your posting in that manner.
------------------------------
Date: Thu, 23 Nov 2000 17:07:48 -0500
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: Sorting data based on 2 key fields
Message-Id: <slrn91r59k.ths.tadmc@magna.metronet.com>
[ Text moved around to conform to a normal time sequence ]
Timothy Valis <junk@yada.net> wrote:
>
>"Tad McClellan" <tadmc@metronet.com> wrote in message
>news:slrn91qqsf.t5u.tadmc@magna.metronet.com...
>> Timothy Valis <junk@yada.net> wrote:
[ snip, wants a 2 level sort ]
>> >but cannot figure how to nest the second sort.
>>
>> Add an "or <second comparison>" after the first comparison.
You didn't mention perlfunc.pod as one of the places you looked.
perldoc -f sort
[ this, from my other followup: ]
Tad> foreach (@Unsort) {
Tad> push @idx1, substr ($_, 0, 2);
Tad> push @idx2, substr ($_, 3, 9);
Tad> }
Note how similar that is to this from perlfunc:
@nums = @caps = ();
for (@old) {
push @nums, /=(\d+)/;
push @caps, uc($_);
}
All that is different from my code above are the conditions
for the two levels.
>> @sorted = @Unsort[ sort { $idx2[$a] <=> $idx2[$b] or
>> $idx1[$a] <=> $idx1[$b] } 0 .. $#idx2];
Note how similar that is to this from perlfunc:
@new = @old[ sort {
$nums[$b] <=> $nums[$a]
||
$caps[$a] cmp $caps[$b]
} 0..$#old
];
perlfunc then goes on to convert the above to a Schwartzian
Transform, avoiding the 2 temporary arrays. You should
"finish up" yours by taking it to an ST too. Understanding
how it works will stand you in good stead, and you already
have all of the "steps" that it encapsulates.
You may want to read the part about creating anonymous arrays
in perlreftut too, if you plan to decipher the ST.
Try it. If you get stuck, post your code here.
>Thanks! It works great!, but I do not understand, why an or operand at that
>location in the sorting caused the desired output. Could you shed a little
>light on that?
See the "C-style Logical Or" section in perlop.pod.
--
Tad McClellan SGML consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: Thu, 23 Nov 2000 19:23:53 -0000
From: "D.B" <dvbryan@_nospamplsHotmail.com>
Subject: Re: Stripping Comma's and Spaces
Message-Id: <d6eT5.5472$ws2.804335@news3.cableinet.net>
Thanks for all your help Guy's!
I C the stuff read about the 'Perl' community is true..
:0)
D.B
------------------------------
Date: Thu, 23 Nov 2000 13:39:49 GMT
From: adelton@fi.muni.cz (Honza Pazdziora)
Subject: Re: Subroutine Stuff???
Message-Id: <G4HCMD.2A9@news.muni.cz>
On Thu, 23 Nov 2000 12:30:05 -0000, vidulats@yahoo.co.uk <vidulats@yahoo.co.uk> wrote:
> I want to write one subroutine like
> Receive ($Val1, %Elements, $Time);
>
> Inside the Receive function :
> sub Receive
> {
> ($myv, %mye, $myt) = @_;
> # Here I want to modify the values of the "mye" hash
> and I want that the same values to the reflected back to the calling
> function.
> #But I don't want
> %Elements = Receive ($Val1, %Elements, $Time)
> # How do I do that
Use hash reference:
Receive($Val1, \%Elements, $Time);
sub Receive {
my ($Val1, $Elements_hashref, $Time) = @_;
$Elements_hashref->{'krtek'} = 'jezek';
}
Yours,
--
------------------------------------------------------------------------
Honza Pazdziora | adelton@fi.muni.cz | http://www.fi.muni.cz/~adelton/
.project: Perl, DBI, Oracle, MySQL, auth. WWW servers, MTB, Spain.
Petition for a Software Patent Free Europe http://petition.eurolinux.org
------------------------------------------------------------------------
------------------------------
Date: Thu, 23 Nov 2000 20:54:14 GMT
From: InternationalPERLjavaPRG@IJavaPerlPrg.com
Subject: TELE COMMUTE ONLY - PERL - Java JavaScript CSS DreamWeaver
Message-Id: <WtfT5.3917$b73.2143604@typhoon.san.rr.com>
*** TELE COMMUTE ONLY ****
WEB PAGE PROGRAMMER
MUST HAVE EXCELLENT EXPERIENCE WITH:
* DREAMWEAVER (Large Domain Mgt.) PREFERRED
* DHTML AND CSS CAPABILITIES OF DREAMWEAVER
* WINDOWS NT 4.0 IIS 5.0 (server)
* WEB PROGRAMMING: PERL / CGI / JavaScript / DHTML / CSS / JAVA
* PROGRAMMING: C / Java
***** ON A SCALE FROM 1 - 10 PERL must be 10 *****
15 TO 40 HOURS PER WEEK
$15.00 to $25.00 per hour commensurate with experience.
* Using a Remote Control software utility called TIMBUKTU
* 2 people can work on the same computer simultaneously
even though thousands of miles apart..
* Timbuktu has an audio channel and chat features.
* High-Bandwidth Not necessary, but PREFERRED.
* Specific "Log-On' Schedule required.
* Flexible
* Prefer Mornings: Pacific Standard Time (PST),
but afternoons, ok.
* 2 to 3 hours per remomte 'log-on' session using Timbuktu
* 2 to 3 times per week minimum.
* Some work done off-line, but this is PRIMARILY a
real-time, log-on position.
* Instead of having a specific schedule at an office,
a specific computer log-on schedule is required.
* Please respond directly to:
Telecommuter@Law.com
++++++ MUST INCULDE +++++++
+ RESUME / CV
+ URL's DEMONSTRATING WEB PAGE PROGRAMMING WORK
------------------------------
Date: Fri, 24 Nov 2000 00:24:16 GMT
From: tim@degree.ath.cx (Tim Hammerquist)
Subject: Re: Tom Christiansons' 'style'
Message-Id: <slrn91reas.1f3.tim@degree.ath.cx>
Chris Stith <mischief@velma.motion.net> wrote:
> It's not relevant to Perl. That's the point. The poster was pointing
> out that one of your posts seemed to be off-topic in a Perl group
^^^^
Please keep track of whose posts are whose. I did not post the message
in question. I'm just over-zealous in looking out for anti-*nix
remarks. =)
--
-Tim Hammerquist <timmy@cpan.org>
Dozens of people spontaneously combust each year,
it's just not really widely reported.
-- David St. Hubbins, "This is Spinal Tap"
------------------------------
Date: Thu, 23 Nov 2000 21:08:28 GMT
From: Dan Yllanes <DanYllanes@NetZero.net>
Subject: Uploading .jpg files to server
Message-Id: <3A1DB0BE.74870556@NetZero.net>
I am trying to implement a script to upload .jpg files to a server. Here
is the script:
#!/usr/local/bin/perl
use CGI qw/:standard/;
use CGI::Carp qw(fatalsToBrowser);
$CGI::POST_MAX=1024 * 100;
$file_name = param('IMAGE');
print header('image/gif');
open (MYFILE, ">http://www.eco-interiors.com/products/$file_name");
binmode MYFILE;
while(read($file_name,$data,1024)) {
print MYFILE $data;
print $data;
}
close MYFILE;
The problem is the file does not get saved to the server. The
permissions for the directory are set to 777. I am using ENCTYPE =
"multipart/form-data" in my HTML form. I am even able to display the
file to the browser from this script with the "print $data;" line. But
the file does not seem to be created for some reason. I am not getting
any kind of error message. Can anyone help?
If you're interested in seeing this script at work, the HTML form that
calls it can be found at :
http://www.eco-interiors.com/UploadFile.html
Dan Yllanes
DanYllanes@Home.com
------------------------------
Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 16 Sep 99)
Message-Id: <null>
Administrivia:
The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc. For subscription or unsubscription requests, send
the single line:
subscribe perl-users
or:
unsubscribe perl-users
to almanac@ruby.oce.orst.edu.
| NOTE: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.
For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V9 Issue 4958
**************************************