[13869] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1279 Volume: 9

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Nov 4 15:05:37 1999

Date: Thu, 4 Nov 1999 12:05:21 -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: <941745920-v9-i1279@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Thu, 4 Nov 1999     Volume: 9 Number: 1279

Today's topics:
    Re: "Argument" I don't understand. (Bart Lateur)
    Re: "Argument" I don't understand (Joe Petolino)
    Re: and since you seem to know your way about... (Bart Lateur)
    Re: and since you seem to know your way about... (Kragen Sitaker)
    Re: Can't get Perl working with Apache <nessus@shell5.ba.best.com>
    Re: Determining Open Mode <james.p.williams@usahq.unitedspacealliance.com>
    Re: duplicates in an array <aqumsieh@matrox.com>
    Re: Extract string (Bart Lateur)
        File handle tests with pattern matching to promote earl <mic_news@bip.bham.ac.uk>
        Help! Simple Perl problem - newbie!! <darren-edwards@esc-ltd.freeserve.co.uk>
        HELP!! Redirecting STDIN and STDOUT to UDP socket aikea@my-deja.com
    Re: HELP:Elements in an array have a space in front of  <aqumsieh@matrox.com>
    Re: Help? What are Black Squares? <gellyfish@gellyfish.com>
    Re: Help? What are Black Squares? <lr@hpl.hp.com>
    Re: Help? What are Black Squares? (Kragen Sitaker)
        How can I connect/control a running process? <joe.carvalho@mci.com>
    Re: How can I connect/control a running process? <rootbeer@redcat.com>
    Re: how sort an array? raju_k@iname.com
    Re: how sort an array? <lr@hpl.hp.com>
    Re: how sort an array? <lr@hpl.hp.com>
    Re: how sort an array? <jhelgesen@my-deja.com>
    Re: how sort an array? <vincent.murphy@cybertrust.gte.com>
    Re: how sort an array? <jhelgesen@my-deja.com>
    Re: how sort an array? <lr@hpl.hp.com>
    Re: how sort an array? <lr@hpl.hp.com>
    Re: Lost in Splitting....... <aqumsieh@matrox.com>
    Re: NEW software repository (Martin Vorlaender)
        Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)

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

Date: Thu, 04 Nov 1999 17:55:11 GMT
From: bart.lateur@skynet.be (Bart Lateur)
Subject: Re: "Argument" I don't understand.
Message-Id: <3826c6d1.3326807@news.skynet.be>

Patrick Smith wrote:

>Argument "260.056 09-17-99 01:20:00 ..." isn't numeric in ncmp at line
>...etc.
>
>This array as you can see has nothing but numbers in it. Any ideas as to
>why I am getting this Argument?

Because it is not ONE number. If you try using this in a numeric
context, e.g. in an addition, it would only use "260.056", up to the
first space. The rest is ignored. Hence the warning.

	print 0+ "260.056 09-17-99 01:20:00";
-->
	260.056
	Argument "260.056 09-17-99 01:20:00" isn't numeric in add

-- 
	Bart.


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

Date: 4 Nov 1999 18:01:00 GMT
From: petolino@Eng.Sun.COM (Joe Petolino)
Subject: Re: "Argument" I don't understand
Message-Id: <7vshks$g1f$1@engnews3.Eng.Sun.COM>

Ala Qumsieh  <aqumsieh@matrox.com> wrote:
>kragen@dnaco.net (Kragen Sitaker) writes:
>> Patrick Smith  <patrick@ocg6.marine.usf.edu> wrote:

>> >#sorts array of test data
>> >@srtedarray = sort{(split/''/,$a)[0]<=>(split/''/,$b)[0]}@data;

>> Here you're using a pattern consisting of two apostrophes for your
>> field delimiter.  My guess is that you didn't mean to do that, because
>> there are no apostrophes in your input data.
>> 
>> If you want to split on whitespace, use /\s+/ instead.

>No. Use the magical ''. I guess this is what Patrick was trying to do,
>but failed:
>
>	split '' => $a;

Perhaps you mean the magical ' ', i.e. a literal string consisting of a 
single space character?  That splits on whitespace, but, unlike split /\s+/,
it ignores any leading spaces.  Splitting with '' (the null string) is
equivalent to splitting with the pattern //: it splits an N-character string
into an N-element list of one-character strings.  It's "magical" in the sense
that it uses a rather obscure feature of split (i.e. <string> is interpreted
as /<string/), but it looks almost identical to the truly magical ' '.
Just trying to clear up some potential confusion.


-Joe


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

Date: Thu, 04 Nov 1999 17:58:43 GMT
From: bart.lateur@skynet.be (Bart Lateur)
Subject: Re: and since you seem to know your way about...
Message-Id: <3829c91b.3912527@news.skynet.be>

Re'em Bar wrote:

>I want my program to create a new file on the server and then read/write
>it. should it be a problem on Unix/Linux because of the permission
>business? is it simpler on NT?

Yes. You need EXECUTE permission on the directory to be allowed to
create a new file. YOu only need WRITE permission to overwrite an
existing file. Silly, but true.

-- 
	Bart.


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

Date: Thu, 04 Nov 1999 18:06:40 GMT
From: kragen@dnaco.net (Kragen Sitaker)
Subject: Re: and since you seem to know your way about...
Message-Id: <QWjU3.27917$23.1475505@typ11.nn.bcandid.com>

In article <3829c91b.3912527@news.skynet.be>,
Bart Lateur <bart.lateur@skynet.be> wrote:
>Re'em Bar wrote:
>
>>I want my program to create a new file on the server and then read/write
>>it. should it be a problem on Unix/Linux because of the permission
>>business? is it simpler on NT?
>
>Yes. You need EXECUTE permission on the directory to be allowed to
>create a new file.

You need both execute and write permission on Unix.  Execute suffices
to allow you to try to open existing files, for either read or write;
the files' permissions determine whether the attempt will succeed.

>You only need WRITE permission to overwrite an
>existing file. 

You need execute permission on the directory, so you can open the file,
and write permission on the file to overwrite the file.

> Silly, but true.

It is not the most transparent scheme.  It is considerably simpler than
NTFS's, I think, and also less versatile.

Generally, though, you don't have to worry about it most of the time on
either system.
-- 
<kragen@pobox.com>       Kragen Sitaker     <http://www.pobox.com/~kragen/>
Tue Nov 02 1999
6 days until the Internet stock bubble bursts on Monday, 1999-11-08.
<URL:http://www.pobox.com/~kragen/bubble.html>


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

Date: 04 Nov 1999 19:55:50 GMT
From: "J. David Eisenberg" <nessus@shell5.ba.best.com>
Subject: Re: Can't get Perl working with Apache
Message-Id: <3821e4c6$0$226@nntp1.ba.best.com>

Nathaniel Abram Mishler <nmishler@uiuc.edu> wrote:
: I'm having the same troubles as you only I'm running the Win32 version
: of 1.3.9.

: I keep generating the Internal Server Error
: The server encountered an internal error or misconfiguration and was
: unable to complete your request...

: My cgi script starts with #!/usr/bin/perl.
: My error.log says: [Thu Oct 21 07:37:05 1999] [error] [client "ip"]
: (2)No such file or directory: couldn't spawn child process: c:/program
: files/apache/cgi-bin/readinput.cgi

: But I have checked and double-checked the directory and the
: readinput.cgi document IS in the folder.

I had the same problem this morning, and it made me
crazy for about an hour until I finally remembered that
Apache does _not_ ignore the first line. You have to put
in the correct location of Perl on your windows system.
In my case, it was:

   #!C:\Perl\5.005\bin\MSWin32-x86-object\Perl.exe

and voila, it worked great.
-- 
J. David Eisenberg    http://www.best.com/~nessus


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

Date: Thu, 04 Nov 1999 17:13:04 GMT
From: Jim Williams <james.p.williams@usahq.unitedspacealliance.com>
Subject: Re: Determining Open Mode
Message-Id: <7vseqs$1bg$1@nnrp1.deja.com>

In article <x3yr9i7rubh.fsf@tigre.matrox.com>,
  Ala Qumsieh <aqumsieh@matrox.com> wrote:
>
> Jim Williams <james.p.williams@usahq.unitedspacealliance.com> writes:
>
> > I'm creating a module that
> > represents a particular file format we use.  Such objects can be
> > configured to either read or write the format, but not both.  The
> > constructor accepts a filehandle, so I have to add another
> > argument, 'r' or 'w', so I know if the object is being created for
> > input or output.
>
> I guess this is a necessary piece of information since your module can
> be used in more than one way; much like the open() function.

Yeah.  I wanted objects created from the module to support both input
and/or output, just like IO::Files, Perl filehandles, C FILE pointers,
etc. (but not C++ istreams and ostreams).

> You can either do that, or you can pass to the constructor the name of
> the file, and the mode of access to that file. Then you can create a
> filehandle locally in your object.

If the constructor accepted a filename instead of a filehandle, the
module could no longer support reading from and writing to arbitrary
pipes, sockets, etc.  If it was more than just a filename and included
the '<', '>', '|' characters expected by Perl's open(), the additional
argument would become redundant.  Clients specify the mode in both the
filename and the extra argument.  This is not a huge deal; I was just
looking for a way to eliminate the extra argument to eliminate the
possibility of it disagreeing with the mode as it appears in the
filename.  Such disconnects are always the price payed for redundancy.

In hindsight, it's less difficult than I thought to parse filenames
passed to open() to determine their mode.  There just aren't too many
combinations of '<', '>', and '|' I'd need to look for.  However, I've
chosen to go the C++ route and have two classes, one for input, and
another for output, both inheriting from a common base class.

As an aside, I found most of the answer to my original question.  fcntl
() *can* be used to determine the original open mode of a filehandle.
I was floundering because of a strangeness with the standard streams.
See below.

> > I want to keep using filehandles here to support both
> > files and pipes without having to parse the file specifier myself.
> > I was looking for a way to eliminate the annoying open mode
> > argument since it's redundant information I'm imposing on clients
> > of the module.
>
> From what you explain, I can't see how this information is
> redundant. If your clients don't need to access the files themselves,
> don't let them open() the files. Ask them for the filename, and the
> mode of acces, and let the object do all the work. Like open(), you
> can have a default mode, but I don't see why you are calling it
> "redundant information".

It's redundant because the client has to specify it in both the
filename as embedded '<', '>', '|', and in the extra argument.  Without
care, the two can disagree.

> I wouldn't do that myself. But, I might consider writing a parent
> class, and a couple of child classes that inherit from the parent
> class, and do their respective business.

That's what I've done.

Returning to the original question about using fcntl() on a filehandle
to determine the original open mode, the following shows how.  The only
remaining question is why fcntl() seems to think STDIN, STDOUT, and
STDERR are open for both reading and writing!  It works fine for
streams I open, but not the standard streams, apparently.  As a check,
an equivalent version of this program written in C does the same
thing.  All of this is on a machine running IRIX 6.5, if it matters.
The program and its output follow.

Thanks again,

Jim Williams

#!/usr/local/bin/perl -w

use strict;
use Fcntl;
use IO::File;

printFlags(*STDIN,'STDIN');
printFlags(*STDOUT,'STDOUT');
printFlags(*STDERR,'STDERR');

my($fh);

die unless $fh=new IO::File('> goober');
printFlags($fh,'goober out');
die unless close $fh;

die unless $fh=new IO::File('< goober');
printFlags($fh,'goober in');
die unless close $fh;

die unless unlink 'goober';

sub printFlags
{
   my($fh,$name)=@_;
   my($flags)=fcntl $fh,F_GETFL,0;

   die "fcntl($name) failed: $!.\n" unless $flags;

   print "$name: ";

   if ($flags & O_WRONLY) {
      print 'write only';
   } elsif ($flags & O_RDWR) {
      print 'read/write';
   } else {
      print 'read only';
   }

   print ', append' if $flags & O_APPEND;

   print "\n";
}

The output of this program is the following.  Note in particular the
strangeness with the standard streams.  What gives?  While no one was
looking, I wrote a C program that wrote to stdin.  I was less than
stunned when fprintf(stdin,"...") failed.

STDIN: read/write
STDOUT: read/write
STDERR: read/write
goober out: write only
goober in: read only


Sent via Deja.com http://www.deja.com/
Before you buy.


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

Date: Thu, 4 Nov 1999 12:05:43 -0500 
From: Ala Qumsieh <aqumsieh@matrox.com>
Subject: Re: duplicates in an array
Message-Id: <x3yln8erxug.fsf@tigre.matrox.com>


"Baris Sumengen" <sumengen@iplab.ece.ucsb.edu> writes:

> Do you know an easy way to remove duplicates from an array.(If multiple
> entries are identicle just keep one copy of it).

Of course. We all know. It's in the FAQs.

From perlfaq4:

	How can I extract just the unique elements of an array?

HTH,
--Ala



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

Date: Thu, 04 Nov 1999 17:04:29 GMT
From: bart.lateur@skynet.be (Bart Lateur)
Subject: Re: Extract string
Message-Id: <3822bc2d.602417@news.skynet.be>

Adrian Chin wrote:

>vol3/extr/rol/rojak/bunga/fungi.sh:1:2:3
>
>How do I extract the colored string out to a variable

Again? Are you blind or something? (Maybe color-blind ;-) You (or a
friend of yours) asked this question a few days ago, and it got several
answers.

-- 
	Bart.


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

Date: Thu, 4 Nov 1999 19:10:32 -0000
From: "michael moorhouse" <mic_news@bip.bham.ac.uk>
Subject: File handle tests with pattern matching to promote early termination of a file read
Message-Id: <7vsljr$fhr$1@usenet.bham.ac.uk>

Hello.
I am having problems terminating a file scan I am writting.
I have a filehandle opened and pointed at a file which contains:
"
first
second
third
X
last
"
I'm aiming at a proof of concept that the 'X' line is reached, the loop
terminates.
The construct :

while (<FILEH> && /^[^X]/) {print $_}

doesn't work.
yet the construct:

while (defined ($_=<FILEH>) && (/^[^X]/)) {print $_}

does print out all lines of the file until the 'X' section I am looking for
and then terminates, like I would expect it to.
Apparently, the 'while (<FILEH>) {}' construct is a special case and does
fancy things like load in the next line from the file into $_ and performs a
undef. test which will terminate the loop at the (natural) end of a file.
Is there a simpler way to do this or have I been pampered too much by the
very convenient 'while (<FILEH>) {}' construct and therefore shouldn't
expect it to be as simple?  *gives dummy an experimental suck* Yup - it's
still there.
Michael M.
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Michael Moorhouse,
Office 8.21
Bioinformatics Project
Dept Biochemistry
University of Birmingham
BIRMINGHAM
B15 2TT
TEL : 0121 414 7879
-------------------------------------------------------------------------
"How to explain?  How to describe?  Even the omniscient viewpoint quails"
from Vernor Vinge : 'A Fire Upon the Deep'
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =




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

Date: Thu, 4 Nov 1999 19:37:52 -0000
From: "Daza" <darren-edwards@esc-ltd.freeserve.co.uk>
Subject: Help! Simple Perl problem - newbie!!
Message-Id: <7vsnet$ido$1@news4.svr.pol.co.uk>

Oh dear, I've lost the plot!

I'm learning Perl from a book and using a 'simple' example with forms. I'm
on NT4 workstation with IE4. The HTML file looks like this:

<html>
<body>
<form action="H:\html.cgi" method="POST">
Name: <input type="text" name="Name" size=15>
<center>
<input type="submit" name="OK">
<input type="reset" name="Clear">
</form>
</body>
</html>

The H:\html.cgi file looks like this:

print "Content-type: text/html\n\n";
print "<HTML><BODY>HI</body></html>";

When I call up the form it looks fine. When I click the OK button I don't
see the ?Name=xxxx at the end of the URL, and a new HTML page appears with
the actual text (verbatim) from the cgi file. I expected to see  just "HI"

What's going on?
tia
DARREN






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

Date: Thu, 04 Nov 1999 19:11:31 GMT
From: aikea@my-deja.com
Subject: HELP!! Redirecting STDIN and STDOUT to UDP socket
Message-Id: <7vslov$73a$1@nnrp1.deja.com>

Hello all,

I have scoured the newsgroups with no luck on this topic.

Here is my scenario.   I have a UNIX shell script that currently re-
directs STDOUT to a serial tty device (i.e. /dev/ttyS01) and STDIN to
my program. The device that I am talking to also has the ability to
talk UDP sockets.  So in short,  I would like to create a device that
is really a perl program (I.E. /dev/perl_program) that will redirect my
STDIN and STDOUT serial data to and from my program.

Any help is greatly appreciated, and I apologize if this information
exists in a FAQ or HOWTO.

Thanks,

bbaer@spatial.com


Sent via Deja.com http://www.deja.com/
Before you buy.


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

Date: Thu, 4 Nov 1999 10:13:12 -0500 
From: Ala Qumsieh <aqumsieh@matrox.com>
Subject: Re: HELP:Elements in an array have a space in front of them
Message-Id: <x3yogdas320.fsf@tigre.matrox.com>


lt lindley <ltl@rgsun40.viasystems.com> writes:

> Damn.  All those other guys have better PSI::ESP modules than I do.

Which version do you have? If less than 3.21, upgrade.

HTH,
--Ala



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

Date: 4 Nov 1999 17:08:23 GMT
From: Jonathan Stowe <gellyfish@gellyfish.com>
Subject: Re: Help? What are Black Squares?
Message-Id: <3821bd87_2@newsread3.dircon.co.uk>

Reba Davis <queen7@gte.net> wrote:
> Hi,
> 
> Can someone tell me which key on the keyboard is used to display the
> "black" squares when one is looking at a CGI script when using Notepad.
> 

You need to learn how to use FTP to transfer the file in text mode ...

/J\
-- 
"It needs to be said: Tinky Winky is ... an ideal role model for any
child without toes or genitalia" - Paul Rudnick, The Guardian


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

Date: Thu, 4 Nov 1999 09:34:29 -0800
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: Help? What are Black Squares?
Message-Id: <MPG.128b63847b61f37f98a1a8@nntp.hpl.hp.com>

In article <kushfj2ci53.fsf@strange.bu.edu> on 04 Nov 1999 11:54:00 -
0500, Scott Lanning <slanning@bu.edu> says...
> "Reba Davis" <queen7@gte.net> writes:
> > Can someone tell me which key on the keyboard is used to display the
> > "black" squares when one is looking at a CGI script when using Notepad.
> > 
> > There have been times when I wanted to edit a script, but I don't
> > know where the black squares are coming from.  They seem to appear
> > between each piece of variable information for example.
> 
> I think they are newlines. Like when I write an HTML document
> in Unix, then view the source, there are a bunch of black boxes
> appearing in Windows where the newlines are and it is all one
> big line of text. The way to fix it is never use Windows and
> start a unabomber campaign against those who do.
> 
> (that, or convert the text-file format, but that's *so* boring...)

That, or use an editor that handles either format, such as [g]vim.

-- 
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: Thu, 04 Nov 1999 18:43:35 GMT
From: kragen@dnaco.net (Kragen Sitaker)
Subject: Re: Help? What are Black Squares?
Message-Id: <rtkU3.28010$23.1478878@typ11.nn.bcandid.com>

In article <MPG.128b63847b61f37f98a1a8@nntp.hpl.hp.com>,
Larry Rosler  <lr@hpl.hp.com> wrote:
>In article <kushfj2ci53.fsf@strange.bu.edu> on 04 Nov 1999 11:54:00 -
>0500, Scott Lanning <slanning@bu.edu> says...
>> "Reba Davis" <queen7@gte.net> writes:
>> > Can someone tell me which key on the keyboard is used to display the
>> > "black" squares when one is looking at a CGI script when using Notepad.
>> 
>> I think they are newlines. 
>> . . . The way to fix it is never use Windows and
>> start a unabomber campaign against those who do.
>> 
>> (that, or convert the text-file format, but that's *so* boring...)
>
>That, or use an editor that handles either format, such as [g]vim.

Or such as wordpad, which is probably a better choice if you're not
a power user.  If you are a power user but not a vi user, maybe Emacs
would be better.
-- 
<kragen@pobox.com>       Kragen Sitaker     <http://www.pobox.com/~kragen/>
Tue Nov 02 1999
6 days until the Internet stock bubble bursts on Monday, 1999-11-08.
<URL:http://www.pobox.com/~kragen/bubble.html>


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

Date: Thu, 04 Nov 1999 17:25:12 GMT
From: "Joe" <joe.carvalho@mci.com>
Subject: How can I connect/control a running process?
Message-Id: <YjjU3.4383$dz1.335651@pm02news>

I've got a restore tape scanner runnning. I need to leave the office, go
home and have a life. How can I connect to the running scanner and control
it from another terminal?
Tnx,
--joe





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

Date: Thu, 4 Nov 1999 10:43:43 -0800
From: Tom Phoenix <rootbeer@redcat.com>
Subject: Re: How can I connect/control a running process?
Message-Id: <Pine.GSO.4.10.9911041042330.29670-100000@user2.teleport.com>

On Thu, 4 Nov 1999, Joe wrote:

> I've got a restore tape scanner runnning. I need to leave the office,
> go home and have a life. How can I connect to the running scanner and
> control it from another terminal?

If you're asking how to work with processes in general, check the docs,
FAQs, and newsgroups about your operating system. If you're asking how to
do this with Perl, check the Perl docs, especially perlipc and perlfunc. 

Good luck with it!

-- 
Tom Phoenix       Perl Training and Hacking       Esperanto
Randal Schwartz Case:     http://www.rahul.net/jeffrey/ovs/



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

Date: Thu, 04 Nov 1999 16:55:22 GMT
From: raju_k@iname.com
Subject: Re: how sort an array?
Message-Id: <7vsdpp$m5$1@nnrp1.deja.com>

In article <7vscv9$vqj$1@nnrp1.deja.com>,
  BlastMaster <jhelgesen@my-deja.com> wrote:
> In article <7vsabm$to5$1@nnrp1.deja.com>,
>   blah1725@my-deja.com wrote:
> > You can do like this:
> >
> > while (<>) {
> >   @fields = split(/\|/, $_);
> >   $lines{$field[3]} = $_;
> > }
> >
> > for $f (sort keys %lines) {
> >   print $lines{$f};
> > }
> >
>
> This assumes that the ratings are unique and don't exceed 10.  You
also
> get the header in the output.
>
> how about
>
> $_ = <>;
> while ( <> ) {
>      @fields = split /\|/;
>      my $obj = { 'line' => $_, 'rating' => $field[3] };
>      push @lines, $obj;
> }
>
> foreach $obj ( sort { $a->{'rating'} <=> $b->{'rating'} } @lines ) {
>     print $obj->{'line'};
> }

  actually, with the above code, it doesn't matter if the rating is
  unique "<=>" will resolve that (you'll get lines with the same rating,
  in the order they appear in the original file).  Also, I believe
  that "<=>" is a numeric sort, so you can have ratings greater than
  10, cmp() is an alpha operator so it will sort it something like:

  1 10 11 12 ... 2 20 21 ... etc.

  --r
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.
>


Sent via Deja.com http://www.deja.com/
Before you buy.


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

Date: Thu, 4 Nov 1999 09:12:01 -0800
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: how sort an array?
Message-Id: <MPG.128b5e359c65568c98a1a6@nntp.hpl.hp.com>

[Rearranged to conform to normal human discourse -- question before 
answer.]

In article <7vsabm$to5$1@nnrp1.deja.com> on Thu, 04 Nov 1999 15:56:41 
GMT, blah1725@my-deja.com <blah1725@my-deja.com> says...
> In article <7vs455$ogn$1@nnrp1.deja.com>,
>   skeeta@gmx.net wrote:

 ...

> > Now I want to sort this file by rating. This can be a value between
> > zero and 5. So I want the file to look like this:

 ...

> You can do like this:
> 
> while (<>) {
>   @fields = split(/\|/, $_);
>   $lines{$field[3]} = $_;
> }
> 
> for $f (sort keys %lines) {
>   print $lines{$f};
> }

That works only if you don't mind ending up with at most 6 elements, no 
matter how many are in the data.  It also happens to sort correctly 
(though backward to what was requested), though you are using 
lexicographical sorting on numbers, because they are all single digit.

You may be confusing this code with the (useful) idea of storing the 
sortkeys in a hash:

  my @data = <>;

  my %lines = map { $_, (split /\|/)[3] } @data;

  print for sort { $lines{$b} <=> $lines{$a} } @data;

Though something similar can be done with only one pass through the data 
(the Orcish Maneuver).  See:

http://www.hpl.hp.com/personal/Larry_Rosler/sort/
 
-- 
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: Thu, 4 Nov 1999 09:28:36 -0800
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: how sort an array?
Message-Id: <MPG.128b621933d1061698a1a7@nntp.hpl.hp.com>

[Posted and a courtesy copy sent.]

In article <m1puxqtcsi.fsf@halfdome.holdit.com> on 04 Nov 1999 08:57:33 
-0800, Randal L. Schwartz <merlyn@stonehenge.com> says...
> >>>>> "Vincent" == Vincent Murphy <vincent.murphy@cybertrust.gte.com> writes:

 ...

> Vincent> print map { $_->[0] }
> Vincent>       sort { $b->[4] <=> $a->[4] }
> Vincent>       map { [$_, split(/\|/)] } @lines;
> 
> Slightly more optimized (to save the sound of all those scalars falling on
> the floor:
> 
> print map { $_->[0] }
>       sort { $b->[1] <=> $a->[1] }
>       map { [$_, (split(/\|/, $_, 5)[4]] } @lines;
                                       ^
                                       3
 
> But the obscuration may not be worth it.

I'll say!  :-)

-- 
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: Thu, 04 Nov 1999 18:32:05 GMT
From: BlastMaster <jhelgesen@my-deja.com>
Subject: Re: how sort an array?
Message-Id: <7vsjf6$55r$1@nnrp1.deja.com>

In article <7vsdpp$m5$1@nnrp1.deja.com>,
  raju_k@iname.com wrote:
>   actually, with the above code, it doesn't matter if the rating is
>   unique "<=>" will resolve that (you'll get lines with the same
rating,
>   in the order they appear in the original file).  Also, I believe
>   that "<=>" is a numeric sort, so you can have ratings greater than
>   10, cmp() is an alpha operator so it will sort it something like:

yeah, I was pointing out the shortcoming of the original response, not
my own code (I'll leave that to someone else!)

-- jeff


Sent via Deja.com http://www.deja.com/
Before you buy.


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

Date: Thu, 04 Nov 1999 18:45:29 GMT
From: Vincent Murphy <vincent.murphy@cybertrust.gte.com>
Subject: Re: how sort an array?
Message-Id: <xjg3dumayev.fsf@gamora.ndhm.gtegsc.com>

>>>>> "Larry" == Larry Rosler <lr@hpl.hp.com> writes:

    Larry> [Posted and a courtesy copy sent.]
    Larry> In article <m1puxqtcsi.fsf@halfdome.holdit.com> on 04 Nov 1999 08:57:33 
    Larry> -0800, Randal L. Schwartz <merlyn@stonehenge.com> says...
    >> >>>>> "Vincent" == Vincent Murphy <vincent.murphy@cybertrust.gte.com> writes:

    Larry> ...

    Vincent> print map { $_->[0] }
    Vincent> sort { $b->[4] <=> $a->[4] }
    Vincent> map { [$_, split(/\|/)] } @lines;
    >> 
    >> Slightly more optimized (to save the sound of all those scalars falling on
    >> the floor:
    >> 
    >> print map { $_->[0] }
    >> sort { $b->[1] <=> $a->[1] }
    >> map { [$_, (split(/\|/, $_, 5)[4]] } @lines;
    Larry>                                        ^
    Larry>                                        3
 
    >> But the obscuration may not be worth it.

    Larry> I'll say!  :-)

Me too.  I think mine is much clearer :-)


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

Date: Thu, 04 Nov 1999 18:35:16 GMT
From: BlastMaster <jhelgesen@my-deja.com>
Subject: Re: how sort an array?
Message-Id: <7vsjl5$5gc$1@nnrp1.deja.com>

In article <xjg7ljyb76r.fsf@gamora.ndhm.gtegsc.com>,
  Vincent Murphy <vincent.murphy@cybertrust.gte.com> wrote:
>
> open(IN, "./weblist") || die;
> my(@lines)=<IN>; close IN or die;
>
> print map { $_->[0] }
>       sort { $b->[4] <=> $a->[4] }
>       map { [$_, split(/\|/)] } @lines;

I knew there had to be a way to do this with map...though I think the
field in question is #3 and you still have to contend with the header.

-- jeff


Sent via Deja.com http://www.deja.com/
Before you buy.


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

Date: Thu, 4 Nov 1999 11:33:50 -0800
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: how sort an array?
Message-Id: <MPG.128b7f7c715e480298a1a9@nntp.hpl.hp.com>

In article <xjg3dumayev.fsf@gamora.ndhm.gtegsc.com> on Thu, 04 Nov 1999 
18:45:29 GMT, Vincent Murphy <vincent.murphy@cybertrust.gte.com> says...
> >>>>> "Larry" == Larry Rosler <lr@hpl.hp.com> writes:
>     Larry> In article <m1puxqtcsi.fsf@halfdome.holdit.com> on 04 Nov 1999 08:57:33 
>     Larry> -0800, Randal L. Schwartz <merlyn@stonehenge.com> says...
>     >> >>>>> "Vincent" == Vincent Murphy <vincent.murphy@cybertrust.gte.com> writes:

 ...

>     Vincent> print map { $_->[0] }
>     Vincent> sort { $b->[4] <=> $a->[4] }
>     Vincent> map { [$_, split(/\|/)] } @lines;
>     >> 
>     >> Slightly more optimized (to save the sound of all those scalars falling on
>     >> the floor:
>     >> 
>     >> print map { $_->[0] }
>     >> sort { $b->[1] <=> $a->[1] }
>     >> map { [$_, (split(/\|/, $_, 5)[4]] } @lines;
>     Larry>                                        ^
>     Larry>                                        3
>  
>     >> But the obscuration may not be worth it.
> 
>     Larry> I'll say!  :-)
> 
> Me too.  I think mine is much clearer :-)

I don't!  In fact, at first glance I thought your code had the same bug 
that Randal fell into, because I didn't grok that you had used $_ in the 
[0] spot of the anonymous arrays.  Fortunately for my pride, I canceled 
my response to your post before I hit 'Send'.  It would have read the 
same as what I responded to Randal with (but two 3's for your two 4's).

Your way may be much clearer to you, but it is neither the optimal nor 
the conventional way.  And your posting it trapped Randal SCHWARTZ into 
posting a buggy SCHWARTZ Transform.  How ironic!  :-)))

-- 
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: Thu, 4 Nov 1999 11:39:10 -0800
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: how sort an array?
Message-Id: <MPG.128b80bdcec729a998a1aa@nntp.hpl.hp.com>

In article <7vsdpp$m5$1@nnrp1.deja.com> on Thu, 04 Nov 1999 16:55:22 
GMT, raju_k@iname.com <raju_k@iname.com> says...

 ...

> > $_ = <>;
> > while ( <> ) {
> >      @fields = split /\|/;
> >      my $obj = { 'line' => $_, 'rating' => $field[3] };
> >      push @lines, $obj;
> > }
> >
> > foreach $obj ( sort { $a->{'rating'} <=> $b->{'rating'} } @lines ) {
> >     print $obj->{'line'};
> > }
> 
>   actually, with the above code, it doesn't matter if the rating is
>   unique "<=>" will resolve that (you'll get lines with the same rating,
>   in the order they appear in the original file). 

What makes you think that?  (Hint:  It isn't so.  The order isn't 
correlated to the order they appear in the original file.  Try a few 
cases and see.)

-- 
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: Thu, 4 Nov 1999 12:22:13 -0500 
From: Ala Qumsieh <aqumsieh@matrox.com>
Subject: Re: Lost in Splitting.......
Message-Id: <x3yk8nyrx2z.fsf@tigre.matrox.com>


"Matt King" <mattking@techie.com> writes:

> file. The first line looks like this:
>  ITEM 1/1   ITEM 2/2           DESCRIPTION
> This is not tab delmitated and the file has some 60000 lines in it so
> changing the format would be just to much work. I'm trying to read in and
> split the above like this:
> ...
>   foreach $line(@LINES) {
>   ($item1,$item2,$item3) = ($line =~  m{
>                  \s*
>                 (\S*(\s*)\S*)\s*
>                 (\S*(\s*)\S*)\s*
>                 (.*)\s*}x);
> ...
> 
> Then I print the output to make sure I got what I want with this (for now,
> later in a table format):
> print "($item1)($item2)($item3)\n";
> 
> The output looks like this:
> (ITEM 1/1)()( ITEM)

The output I get is:

(ITEM 1/1)( )(ITEM 2/2)

And the reason is obvious. The brackets within the brackets in your
regexps match the spaces in between ITEM and 1/1 (or 2/2). Those get
saved also in a variable.

> What is wrong with this split? I have been trying different 'formats', but I

There is no split() here.

> can't find the one that will give me the output that looks like this, which
> is the one I need:
> (ITEM 1/1)(ITEM 2/2)(DESCRIPTION)

What about:

	($item1, $item2, $item3) = 
		$line =~ /(\S+\s+\S+)\s+(\S+\s+\S+)\s+(.+)/;

> Can someone please point out where my eror is and what I need to change to
> make this work. Note there there is a space in the first two items and and
> the third item needs all remaining items even if there is a space.

See above.

HTH,
--Ala



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

Date: Thu, 04 Nov 1999 19:39:44 +0100
From: martin@RADIOGAGA.HARZ.DE (Martin Vorlaender)
Subject: Re: NEW software repository
Message-Id: <3821d2f0.524144494f47414741@radiogaga.harz.de>

Craig Berry (cberry@cinenet.net) wrote:
: Speaking of which, has anybody done an MSA formmail.pl workover using
: modern perl, sans security holes, etc.?  I see newbies being pummeled for
: using formmail, but haven't noticed suggested alternative quick-and-dirty
: form mailers.  If this is a void, I may try to fill it; I've got the job
: half done already for my own purposes.

I haven't ever looked at MSA, but have done my own formmail.pl (it's really
hard to find a decent name for a script with such purpose ;-).
Have a look at it at http://www.pdv-systeme.de/users/martinv/.

cu,
  Martin
--
                       |  Martin Vorlaender  |  VMS & WNT programmer
  OpenVMS: When you    |  work: mv@pdv-systeme.de
  KNOW where you want  |        http://www.pdv-systeme.de/users/martinv/
  to go today.         |  home: martin@radiogaga.harz.de


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

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 1279
**************************************


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