[19737] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1932 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Oct 15 09:06:20 2001

Date: Mon, 15 Oct 2001 06:05:08 -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: <1003151108-v10-i1932@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Mon, 15 Oct 2001     Volume: 10 Number: 1932

Today's topics:
        Array as a variable <blnukem@hotmail.com>
    Re: Balanced RegExp Pattern? (Anno Siegel)
        connecting to DB2 <aort527@hotmail.com>
        Forcing IO buffer flush <this.address@is.a.foo>
    Re: Forcing IO buffer flush <Thomas@Baetzler.de>
    Re: Forcing IO buffer flush (Martien Verbruggen)
        getting tag text from HTML::LinkExtor (mbower)
    Re: getting tag text from HTML::LinkExtor <Thomas@Baetzler.de>
    Re: getting tag text from HTML::LinkExtor (mbower)
        Help on wait and get process ids.. (Rashyid)
    Re: Help on wait and get process ids.. (Martien Verbruggen)
        How to recieve input in a child process (Rune)
    Re: How to recieve input in a child process <josef.moellers@fujitsu-siemens.com>
    Re: How to recieve input in a child process (Martien Verbruggen)
    Re: html parsing <yann@linagora.com>
        I need to simulate keyboard keypress to an external app <tonino.sclavont@clin.ucl.ac.be>
        newbie question <bjelkea@online.no>
    Re: newbie question (Rafael Garcia-Suarez)
    Re: Spliting string to an array of word <s.warhurst@rl.ac.uk>
    Re: Value sort Display by key (interesting) <ghed9@netzero.net>
    Re: Value sort Display by key (interesting) <ghed9@netzero.net>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Mon, 15 Oct 2001 12:28:46 GMT
From: "Blnukem" <blnukem@hotmail.com>
Subject: Array as a variable
Message-Id: <2EAy7.13636$yj.2096939@news02.optonline.net>

Hi All

I need help with this, What I'm trying to do is retrieve a form "value" that
is always different. 1-10 fist page 11-20 next page and so on I cant seem to
figure it out. I can get "$0 = $FORM{'0'} $1 = $FORM{'1'}" pushed into an
array but how can I assign each in the array as a variable to be used in the
script?

print "Content-type: text/html\n\n"

for ($i = 0; $i < 10; $i +=1 ) {
push (@TEMP, "\$$i = \$FORM{'$i'}\;\n" )
}

print @TEMP;     # just so I can view the array.

Thanx in advance Blnukem




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

Date: 15 Oct 2001 12:53:37 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Balanced RegExp Pattern?
Message-Id: <9qem8h$3ae$1@mamenchi.zrz.TU-Berlin.DE>

According to Davin Shearer  <davin@cablespeed.com.ns>:
> Greetings,
> 
> I'm using Java oro to use Perl regular expressions in a servlet I'm 
> writing. I've come across an obstacle that I'm hoping someone here can 
> assist me in overcoming.
> 
> What I want to do is parse a set of macros. Call them [list][/list]. 
> These list macros, like HTML lists, can be nested, and like HTML lists, 
> they are required to be balanced.

[snip]

You probably want Text::Balanced.

Anno


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

Date: Mon, 15 Oct 2001 07:18:34 -0500
From: alex <aort527@hotmail.com>
Subject: connecting to DB2
Message-Id: <3BCAD41A.8E700E53@hotmail.com>

I need to connect to a DB2 database using a
data source.  Can someoby give me an example
of the connect string?



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

Date: Mon, 15 Oct 2001 10:43:55 +0100
From: "Sam" <this.address@is.a.foo>
Subject: Forcing IO buffer flush
Message-Id: <9qefgu$338$1@reader-00.news.insnet.cw.net>

I am writing a daemon in perl. I want to, at various points in the program,
ensure that the IO buffers are flushed and that everything has been comitted
to disk. I do not want to close and reopen my files, as the position that
the progrma has within the files in of critical importance to it's
operation.

If I am asking a question which has been covered in another text, FAQ or
newsgroup thread then please point me there, I wouldn't want to reinvent the
wheel.

Thanks: Sam.




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

Date: Mon, 15 Oct 2001 13:44:22 +0200
From: =?ISO-8859-1?Q?Thomas_B=E4tzler?= <Thomas@Baetzler.de>
Subject: Re: Forcing IO buffer flush
Message-Id: <jqilstgmed6vm2t29jbf730stajcif4bd2@4ax.com>

On Mon, 15 Oct 2001 10:43:55 +0100, "Sam" <this.address@is.a.foo> wrote:

>I am writing a daemon in perl. I want to, at various points in the program,
>ensure that the IO buffers are flushed and that everything has been comitted
>to disk. I do not want to close and reopen my files, as the position that
>the progrma has within the files in of critical importance to it's
>operation.

You could either turn on autoflush for your filehandles (see $| in
perlvar and the perlfunc manpage) or use the OO file access interface
via IO::File - that package inherits flush from IO::Handle.

HTH,
-- 
use strict;my($i,$t,@r)=(0,'5 -.@BHJPT4acd6e2hk2lmn2o4r2s3tuz',map{ord}
split//,unpack('u*','L#`T&)QD5#0`#!!`#%1D)#08`#P05!!(3``$$"``#"0L&``('.
'"`P<!`````0$`'));$t=~s/(\d)(.)/$2x$1/eg;map{$t.=substr$t,$i,1,''while
$_--;$i++}@r;print"$t\n";# Thomas@Baetzler.de - http://baetzler.de/perl


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

Date: Mon, 15 Oct 2001 22:29:12 +1000
From: mgjv@tradingpost.com.au (Martien Verbruggen)
Subject: Re: Forcing IO buffer flush
Message-Id: <slrn9sllko.di2.mgjv@martien.heliotrope.home>

On Mon, 15 Oct 2001 10:43:55 +0100,
	Sam <this.address@is.a.foo> wrote:
> I am writing a daemon in perl. I want to, at various points in the program,
> ensure that the IO buffers are flushed and that everything has been comitted
> to disk. I do not want to close and reopen my files, as the position that
> the progrma has within the files in of critical importance to it's
> operation.

You could set autoflush for the file handles (see the $| variable in
perlvar, or the autoflush method in the IO::Handle module).

You can also regularly call the sync or flush methods, described in the
IO::Handle module.

> If I am asking a question which has been covered in another text, FAQ or
> newsgroup thread then please point me there, I wouldn't want to reinvent the
> wheel.

Perl's file handles are IO::Handle objects, nowadays, so you should
probably just familiarise yourself with that documentation.

And, of course, there's the Perl FAQ, section 5, first question:

    How do I flush/unbuffer an output filehandle?  Why must I
    do this?

Although that FAQ entry, arguably, isn't entirely complete and up to
date.

Martien
-- 
Martien Verbruggen              | 
Interactive Media Division      | That's funny, that plane's dustin'
Commercial Dynamics Pty. Ltd.   | crops where there ain't no crops.
NSW, Australia                  | 


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

Date: Mon, 15 Oct 2001 12:02:54 GMT
From: mbower@ibuk.bankgesellschaft.de (mbower)
Subject: getting tag text from HTML::LinkExtor
Message-Id: <3bcace41.1394671281@news>

Hi,
I'm having problems extracting a tag value from a HTML tag
<A HREF="xx.html">Some text here</A> 
<A HREF="xx.html"><img src="haha.gif"></A>

I'd like to get the text values in between <A> and </A>, and if not
text then ingnore it.

The script loads the page, extracts all links and then lists them..

Can anyone point me in the right direction ?

sample output
----------------------
LINK http://www.perl.com/pub/a/language/info/software.html
LINK http://www.perl.com/CPAN-local/README.html
LINK http://perl.oreilly.com/news/success_stories.html
LINK http://www.perl.com/pub/a/2001/10/10/perlmx.html
LINK http://www.perl.com/pub/a/2001/10/10/perlmx.html
LINK http://www.perl.com/pub/a/2001/10/p5pdigest/20011010.html
LINK http://www.perl.com/pub/a/2001/10/p5pdigest/20011010.html
LINK http://www.xml.com/pub/a/2001/10/10/sax-filters.html
LINK http://www.perl.com/pub/a/2001/10/02/apocalypse3.html
LINK http://www.perl.com/pub/a/2001/10/03/exegesis3.html
LINK http://www.perl.com/pub/a/2001/09/26/crypto1.html
LINK http://www.xml.com/pub/a/2001/09/19/sax-non-xml-data.html
LINK http://www.perl.com/pub/a/2001/09/18/parrot.html
LINK http://www.perl.com/pub/a/2001/09/12/wxtutorial1.html
LINK http://www.perl.com/pub/a/2001/09/p6pdigest/20010908.html
LINK http://www.perl.com/pub/a/2001/09/04/tiedhash.html
LINK http://www.perl.com/pub/a/universal/pcb/solution.html
LINK http://www.perl.com/pub/a/language/info/software.html#stable
LINK http://www.perl.com/pub/a/language/info/software.html#devel
LINK http://www.perl.com/pub/a/general/about.html
LINK http://www.oreillynet.com/pub/a/mediakit/contact.html
LINK http://www.oreillynet.com/pub/a/mediakit/privacy.html




#!/usr/local/bin/perl -w
use strict;
use LWP::UserAgent;
use HTML::LinkExtor;
use URI::URL;

my @ahref = ();
my $url = "http://www.perl.com";

my $page_content = &get_page($url);
if ($page_content eq '0') { die "Bad page!!!\n"; }
&get_more($url,$page_content);

#---------------------------SUBROUTINES------------------------------

sub get_more {
        my ($checkurl,$pagecontent) = @_;
        my $p;
        $p = HTML::LinkExtor->new(\&callback2,$checkurl);
        $p->parse($pagecontent);

        print "Links found\n--\n";
        foreach(@ahref) { print "LINK $_\n" }
}

sub callback2 {    # this strips out links
        my($tag, %attr) = @_;
        return if ($tag ne 'a');  # tags and references only
        my @vals = values %attr;
        my $expand = join(' ',@vals);
        if ($expand =~ /\.htm[l]?/i) { push(@ahref, $expand) }
}

sub get_page {
        my ($checkurl) = @_;
        my $agent = new LWP::UserAgent;
        my $retval;
        $agent->agent( "$0/0.1 " . $agent->agent );

        # Request document and parse it as it arrives
        my $request = new HTTP::Request 'GET' => $checkurl;
        $request->header( 'Accept' => 'text/html' );
        $agent->proxy( http => "http://myproxyserver" );
        $request->proxy_authorization_basic( "yeah", "right" );
        my $result = $agent->request( $request );

        if ( $result->is_success ) { $retval = $result->content } else
{ $retval = "0" }
        return($retval);
}
#-----------------------END OF SUBROUTINES---------------------------





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

Date: Mon, 15 Oct 2001 14:16:20 +0200
From: =?ISO-8859-1?Q?Thomas_B=E4tzler?= <Thomas@Baetzler.de>
Subject: Re: getting tag text from HTML::LinkExtor
Message-Id: <1pklstchtam9d5tpqo1flqd3gnimh8i8ck@4ax.com>

On Mon, 15 Oct 2001, mbower@ibuk.bankgesellschaft.de (mbower) wrote:
>I'm having problems extracting a tag value from a HTML tag
><A HREF="xx.html">Some text here</A> 
><A HREF="xx.html"><img src="haha.gif"></A>
>
>I'd like to get the text values in between <A> and </A>, and if not
>text then ingnore it.

Here's some code that will extract link info.

#!/usr/bin/perl -w

use strict;

use HTML::Parser;

my $htmlfile = 'foo.html';

my( $href, $text );

my @linklist;

 
sub a_start_handler {
  my( $tag, $self, $attr ) = @_;

  # we only act on <a tags  
  return if $tag ne "a";
  
  if( defined( $href = $attr->{href} ) ){
    $self->handler(text => sub { $text = shift; $text =~ s/\n//g; },
"dtext");
    $self->handler( end => \&a_end_handler, "tagname,self" );
  }
  
  foreach my $key ( keys %$attr ){
  #  print ">$key=$attr->{$key}\n";
  }
  
  
}

sub a_end_handler {
  return if shift ne "a";
  my $self = shift;
  
  push @linklist, [ $href, $text ] if defined $text && $text !~ /^\s*$/;
  
  $self->handler(end => undef );
  $self->handler(text => undef );
}
  


my $p = HTML::Parser->new(api_version => 3);

$p->handler( start => \&a_start_handler, "tagname,self,attr" );

$p->unbroken_text( 1 );

$p->parse_file( $htmlfile ) || die $!;

foreach my $link ( @linklist ){
  print "LINK: $link->[0]\n";
}

__END__

HTH,
-- 
use strict;my($i,$t,@r)=(0,'5 -.@BHJPT4acd6e2hk2lmn2o4r2s3tuz',map{ord}
split//,unpack('u*','L#`T&)QD5#0`#!!`#%1D)#08`#P05!!(3``$$"``#"0L&``('.
'"`P<!`````0$`'));$t=~s/(\d)(.)/$2x$1/eg;map{$t.=substr$t,$i,1,''while
$_--;$i++}@r;print"$t\n";# Thomas@Baetzler.de - http://baetzler.de/perl


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

Date: Mon, 15 Oct 2001 12:19:03 GMT
From: mbower@ibuk.bankgesellschaft.de (mbower)
Subject: Re: getting tag text from HTML::LinkExtor
Message-Id: <3bcad3de.1396107562@news>

On Mon, 15 Oct 2001 14:16:20 +0200, =?ISO-8859-1?Q?Thomas_B=E4tzler?=
<Thomas@Baetzler.de> wrote:

>use HTML::Parser;

Thanks for the reply, it's a shame I can't get it from
HTML::LinkExtor.  I guess I'll have to change things.




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

Date: 15 Oct 2001 03:30:26 -0700
From: rra@excite.com (Rashyid)
Subject: Help on wait and get process ids..
Message-Id: <79c1319c.0110150230.222f410c@posting.google.com>

HI there..

I am a newbie to Perl and i kinda need some help on how to do this
particular task..

Ok, what i need to do is to run several commands that needs to wait
til the the former finishes to start.. for example:

I need to run system commands comA, comB, comC, comD like:

start off with comA, and it returns pidA

wait til pidA finishes and then run comB which returns pidB

wait til pidB finishes and then run comC which returns pidC

but.. i also want to run comD when comA (pidA) finishes...

something like
        wait
comA------------> comD
  |
  |---> comB
   wait  |
         |---> comC
          wait


Anyone can help me here?


Thanks in advance

Redza


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

Date: Mon, 15 Oct 2001 22:10:56 +1000
From: mgjv@tradingpost.com.au (Martien Verbruggen)
Subject: Re: Help on wait and get process ids..
Message-Id: <slrn9slkif.di2.mgjv@martien.heliotrope.home>

On 15 Oct 2001 03:30:26 -0700,
	Rashyid <rra@excite.com> wrote:
> HI there..
> 
> I am a newbie to Perl and i kinda need some help on how to do this
> particular task..
> 
> Ok, what i need to do is to run several commands that needs to wait
> til the the former finishes to start.. for example:

> something like
>         wait
> comA------------> comD
>  |
>  |---> comB
>    wait  |
>          |---> comC
>           wait
> 
> 
> Anyone can help me here?

You don't need to do any magic with pid's and waiting.

system("comA");
system("comD &");
system("comB");
system("comC");

If you need to wait for comD to finish before your program ends, then
use a fork and exec.

system("comA");

my $pid = fork;
die "Cannot fork: $!" unless defined $pid;

unless ($pid)
{
    # child
    exec("comD") or die "Cannot exec comD: $!";
}

# parent
system("comB");
system("comC");

1 while wait != -1;


If this is all you need to do, then I wouldn't bother with Perl. Just do
it in a shell script. or if you have to do it in Perl:

system "comA; (comD &); comB; $comC; wait";


Oh, of course, in a real program you'd be checking the return values of
the system()s and you'd do something useful with them.

Martien
-- 
Martien Verbruggen              | The Second Law of Thermodenial: In
Interactive Media Division      | any closed mind the quantity of
Commercial Dynamics Pty. Ltd.   | ignorance remains constant or
NSW, Australia                  | increases.


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

Date: 15 Oct 2001 04:01:36 -0700
From: hoplah@hotmail.com (Rune)
Subject: How to recieve input in a child process
Message-Id: <6b0f1981.0110150301.139c8858@posting.google.com>

Hi all,
I trying to setup a very simple parent-child program. I general the
Parrent spawns the child and sends input to it regulary. I open the
child process with this command:

open (RANDOM, | props.plx);

Now to send info to the child I do like this:

print RANDOM "$number";

But my problem is how to setup the child program to recieve the
$number from the parent. I tried with:

$childNumber = <STDIN>;

within a while loop but the program just waited for input.

How to setup the child program for recieving the input from the
parent?

Thanks,
Rune


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

Date: Mon, 15 Oct 2001 13:16:30 +0200
From: Josef =?iso-8859-1?Q?M=F6llers?= <josef.moellers@fujitsu-siemens.com>
Subject: Re: How to recieve input in a child process
Message-Id: <3BCAC58E.44688BFB@fujitsu-siemens.com>

Rune wrote:
> =

> Hi all,
> I trying to setup a very simple parent-child program. I general the
> Parrent spawns the child and sends input to it regulary. I open the
> child process with this command:
> =

> open (RANDOM, | props.plx);
> =

> Now to send info to the child I do like this:
> =

> print RANDOM "$number";
> =

> But my problem is how to setup the child program to recieve the
> $number from the parent. I tried with:
> =

> $childNumber =3D <STDIN>;
> =

> within a while loop but the program just waited for input.
> =

> How to setup the child program for recieving the input from the
> parent?

I guess it's not the child that' failing to receive, it's the parent
who's failing to send (properly). You probably need to set the RANDOM
handle to autoflush:
	my $oldfh =3D select RANDOM; $| =3D 1; select $oldfh;

-- =

Josef M=F6llers (Pinguinpfleger bei FSC)
	If failure had no penalty success would not be a prize
						-- T.  Pratchett


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

Date: Mon, 15 Oct 2001 22:16:42 +1000
From: mgjv@tradingpost.com.au (Martien Verbruggen)
Subject: Re: How to recieve input in a child process
Message-Id: <slrn9slkta.di2.mgjv@martien.heliotrope.home>

On 15 Oct 2001 04:01:36 -0700,
	Rune <hoplah@hotmail.com> wrote:
> Hi all,
> I trying to setup a very simple parent-child program. I general the
> Parrent spawns the child and sends input to it regulary. I open the
> child process with this command:
> 
> open (RANDOM, | props.plx);

I don't believe you. This is not valid Perl. Maybe you meant

open(RANDOM, "|props.plx") or die "Cannot open pipe to props.plx: $!";

> Now to send info to the child I do like this:
> 
> print RANDOM "$number";
> 
> But my problem is how to setup the child program to recieve the
> $number from the parent. I tried with:
> 
> $childNumber = <STDIN>;
> 
> within a while loop but the program just waited for input.

This is probably buffering. Hard to tell without you showing us two
complete programs for the parent and the child. ou either need to
disable buffering in the writing process, and probably also the reading
process, or you have to wait until the writer closes the pipe.

Hard to tell what is best, since we don't have the foggiest clue what
you're actually trying to do.

> How to setup the child program for recieving the input from the
> parent?

Just read. it's most likely not the reader that's the problem, but the
writer.

Look up the $| variable in the perlvar documentation.

Martien
-- 
Martien Verbruggen              | 
Interactive Media Division      | In a world without fences, who needs
Commercial Dynamics Pty. Ltd.   | Gates?
NSW, Australia                  | 


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

Date: Mon, 15 Oct 2001 12:15:06 +0200
From: Yann Bizeul <yann@linagora.com>
Subject: Re: html parsing
Message-Id: <9qectn$1ojh$1@norfair.nerim.net>

Hi

Personnaly, I did the following things :
- remove all CR of the file to make the whole HTML on one single line
- replace </tr> by </tr>\n to make one row per line
- remove "^<tr><td>" and "</td></tr>$"
- replace "</td><td>" by \t

this gave me a tab separeted output of the html table.

Hope that helps you

hugo wrote:

> Hi
> 
> I was wondering if someone could help me with a bit of code: I would
> like to extract data from HTML files, but the tags that precede the data
> may vary, although most often they are likely to be table tags i.e.
> <table>,tr><td>get this (often a number)</td></td>get that (another
> number)</td></tr></table. I was thinking of using the perl HTML:Parsing
> module, but I am a bit unsure as how to tackle it. I would like to:
> 
> 1. Import the contents of a html file.
> 
> 2. Display to the user the text (html contents) between tags (I think
> this equates to the tag attributes), each time asking the user whether
> he/she wants to preserve that text.
> 
> 3. Store the text.
> 
> 4. Output the text in another file in tabular format.
> 
> Could anyone help me out with a bit of code that sets me on my way? I am
> not so sure how to go about it.
> 
> Any help will be greatly appreciated.
> 
> Thanks
> 
> Hugo
> 



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

Date: Mon, 15 Oct 2001 12:05:33 +0200
From: "Tonino Sclavont" <tonino.sclavont@clin.ucl.ac.be>
Subject: I need to simulate keyboard keypress to an external application
Message-Id: <9qecdb$q16$1@ail.sri.ucl.ac.be>

I need to simulate keyboard keypress to an external application

I want to close an external apllication ( dos type ) automaticaly.

For that I need to send a sequence of caracters to that application.

The only things I know is the process number and its name.

Than you.





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

Date: Mon, 15 Oct 2001 11:49:58 GMT
From: "Alexander Bjelke" <bjelkea@online.no>
Subject: newbie question
Message-Id: <G3Ay7.310$Ryf.135400960@news.telia.no>

Hi, take a look at his:

$d1 = "somewhere"
$d2 = "somewhereelse"

my @dir_array = qw($d1 $d2);

when I print  out the contest of @dir_array I get $d1 and $d2 instead of the
filepath. Why? (and how can I fix it)

Thanks alot! :-)
Alexander




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

Date: 15 Oct 2001 11:54:54 GMT
From: rgarciasuarez@free.fr (Rafael Garcia-Suarez)
Subject: Re: newbie question
Message-Id: <slrn9sljkf.c7t.rgarciasuarez@rafael.kazibao.net>

Alexander Bjelke wrote in comp.lang.perl.misc:
} Hi, take a look at his:
} 
} $d1 = "somewhere"
} $d2 = "somewhereelse"
} 
} my @dir_array = qw($d1 $d2);
} 
} when I print  out the contest of @dir_array I get $d1 and $d2 instead of the
} filepath. Why? (and how can I fix it)

Because qw() does not interpolate.
In other words, it (q)uotes (w)ords.
Use the good old syntax
    @dir_array = ($d1,$d2);

-- 
Rafael Garcia-Suarez / http://rgarciasuarez.free.fr/


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

Date: Mon, 15 Oct 2001 13:13:32 +0100
From: "S Warhurst" <s.warhurst@rl.ac.uk>
Subject: Re: Spliting string to an array of word
Message-Id: <9qejtd$hli@newton.cc.rl.ac.uk>

"Sasha" <sasha_lui@yahoo.com> wrote in message
news:9qe9co$bg8$1@newstoo.ericsson.se...
> Thank you Rafael for your reply. I am sorry if my message was in wrong
> format. But how could i send my message in right format, or in what format
> should my message be ?

In Outlook Express, when you are composing a new message, if you go to the
Format menu and select "Plain Text" as the message format.




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

Date: Mon, 15 Oct 2001 12:58:25 GMT
From: "Gerard Lapidario" <ghed9@netzero.net>
Subject: Re: Value sort Display by key (interesting)
Message-Id: <R3By7.49$SR6.54637@paloalto-snr2.gtei.net>

It works!

Thanks  :-)

"Tony Curtis" <tony_curtis32@yahoo.com> wrote in message
news:877ku0lfyj.fsf@limey.hpcc.uh.edu...
>
> [ top-posting rearranged ]
>
> >> On Fri, 12 Oct 2001 20:48:45 GMT,
> >> "Gerard Lapidario" <ghed9@netzero.net> said:
>
> > Thanks Tony.  I tried it, kind of a newby in perl, using
> > the statement below #!/usr/planet/bin/perl5.00404
>
> > %files = ( "A",1, "B",0, "C",0, "D",2, "E",2, "F",1 );
>
> > foreach $cnt (sort {$files{$b} <=> $files{a}} values %files)
>                                                 ^^^^^^
> You're sorting the values, not the keys.  You need to sort
> the *keys* by comparing their associated *values*.
>
> So...
>
>     sort { $files{$b} <=> $files{$a} || $a cmp $b } keys %files
>
> hth
> t
> --
> Oh!  I've said too much.  Smithers, use the amnesia ray.




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

Date: Mon, 15 Oct 2001 12:58:47 GMT
From: "Gerard Lapidario" <ghed9@netzero.net>
Subject: Re: Value sort Display by key (interesting)
Message-Id: <b4By7.50$SR6.54863@paloalto-snr2.gtei.net>

Thanks :-)
"Michael Carman" <mjcarman@home.com> wrote in message
news:3BC75C52.2F490A53@home.com...
> Gerard Lapidario wrote:
> >
> > Hello perl experts.  I have an interesting problem here,
>
> Let us be the judge of that. Most of us find it pretty mundane. In fact,
> it's an FAQ. See perlfaq4, "How do I sort an array by (anything)?"
>
> > below I have a hash.  I wish to sort it by value but display
> > the key instead of the value after it is sorted in descending
> > order.
> >
> > %files = (
> > "A",1,
> > "B",0,
> > "C",0,
> > "D",2,
> > "E",2,
> > "F",1
> > );
> >
> > The result of the item above after sorting should be:
> >
> > D
> > E
> > A
> > F
> > B
> > C
> >
> > Using foreach $cnt (sort {$b <=> $a} values %files) sort the values,
> > but I cannot retrieve the key.  Please help.  Thanks :-)
>
> It looks like you want to sort by value (descending) and then by key
> (ascending). You can't get the keys back with the value, but you can get
> the value with the key. So sort using the keys:
>
> foreach (sort {$files{$b} <=> $files{$a} || $a cmp $b} keys %files) {
>     print "$_\n";
> }
>
> -mjc




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

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


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