[19725] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1920 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Oct 12 18:10:31 2001

Date: Fri, 12 Oct 2001 15:10:13 -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: <1002924613-v10-i1920@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Fri, 12 Oct 2001     Volume: 10 Number: 1920

Today's topics:
    Re: mode date from dir <khera@kcilink.com>
    Re: Optimizing lookup in hash of regexps <goldbb2@earthlink.net>
        Pattern Matching (Tim)
    Re: Pattern Matching <Laocoon@eudoramail.com>
    Re: PATTERN Matching: Substitution problem <goldbb2@earthlink.net>
        Problem determining the mime-type of a file upload. (Jason Smith)
    Re: question about MAP function, please help. (Abigail)
    Re: Quicker way to loop through directories? <goldbb2@earthlink.net>
    Re: Sorting larg arrays <goldbb2@earthlink.net>
    Re: sorting question <goldbb2@earthlink.net>
    Re: Strange behaviour or I/O error (Idiot Operator)? -  <aneely@softouch.on.ca>
        Text Scraper, Cookies, and Oracle Portal (9ias)...pleas (Ryan)
        UDP server <riccardo@ebi.ac.uk>
    Re: UDP server (John J. Trammell)
        Value sort Display by key (interesting) <ghed9@netzero.net>
    Re: Value sort Display by key (interesting) <tony_curtis32@yahoo.com>
    Re: Value sort Display by key (interesting) <tony_curtis32@yahoo.com>
    Re: Value sort Display by key (interesting) <ghed9@netzero.net>
    Re: Value sort Display by key (interesting) <tony_curtis32@yahoo.com>
    Re: Value sort Display by key (interesting) <mjcarman@home.com>
        Working with Bit Vectors <caughran@chem.uga.edu>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 12 Oct 2001 14:53:57 -0400
From: Vivek Khera <khera@kcilink.com>
Subject: Re: mode date from dir
Message-Id: <x7n12wbsfu.fsf@onceler.kciLink.com>

>>>>> "MA" == Mahesh A <maheshasolkar@yahoo.com> writes:


MA> I think so too. But this might help if the requirement is not huge
MA> enough to go for a module:

MA> ----------
MA> while (defined($file = readdir(DIR))) {
MA>   my $mod = (stat($file))[9];
MA>   my ($sec,$min,$hr,$day,$mon,$yr,$wd,$yd,$isdt) = localtime($mod);
MA>   print "$file -> $mon/$day/".(1900+$yr)." $hr:$min:$sec\n";
MA> }
MA> ----------
MA> I used this in one of my scripts. I am not aware of the caveats related.

Your code assumes that the DIR in question is the current working
directory.


-- 
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Vivek Khera, Ph.D.                Khera Communications, Inc.
Internet: khera@kciLink.com       Rockville, MD       +1-240-453-8497
AIM: vivekkhera Y!: vivek_khera   http://www.khera.org/~vivek/


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

Date: Fri, 12 Oct 2001 18:06:35 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Optimizing lookup in hash of regexps
Message-Id: <3BC7696B.3357CE3E@earthlink.net>

Matt Christian wrote:
> 
> Hi,
> 
> I have a Perl program that looks up data by matching against a hash of
> regexps (regular expressions) as follows:
[snip]
> Which are the expected results.  However, this is just a subset of the
> 'real' data which includes thousands of datums to lookup and thousands
> of lookup table entries.  So as these arrays and hashes get bigger,
> this program gets slower.
> 
> I have to believe that there are more efficient ways of doing
> this...
[snip]
> Any ideas or tips to help speed this program up?

Here's one solution:
use warnings;
use strict;

my %lookupTable = ....;

sub lookup;
{
    my $thesub = qq[sub lookup {\n\tlocal $_ = shift;\n\t];
    while( my( $regex, $result ) = each %lookupTable ) {
        $thesub .= qq[if(/$regex/) {\n\t\treturn '$result'\n\t} els];
    }
    $thesub .= qq[e {\n\t\treturn\n\t}\n];
    print "Created sub -->\n$thesub<--\n";
    no warnings 'redefine';
    eval $thesub;
    die "Couldn't form lookup sub: $@" if $@;
};

my @data = ....

foreach my $datum ( @data ) {
   print "datum = '$datum'\t";
   if( defined(my $result = lookup($datum) ) {
       print "result = '$result'";
   } else {
       print "NO MATCH!";
   }
   print "\n";
}

__END__

Another method might be:
use warnings;
use strict;

my %lookupTable = ....;

my $bigre = join "|", map "($_)", keys %lookupTable;
my @results = values %lookupTable;

my @data = ....;

foreach my $datum ( @data ) {
   print "datum = '$datum'\t";
   if( $datum =~ /$bigre/o ) {
       print "result = '$results[($#-) - 1]';
   } else {
       print "NO MATCH!";
   }
   print "\n";
}
__END__

NB: Both of these are untested.


-- 
    "Just how stupid are you Kuno?"
    "Verily, Tatewaki Kuno knows no limits."


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

Date: 12 Oct 2001 08:33:45 -0700
From: tvn007@hotmail.com (Tim)
Subject: Pattern Matching
Message-Id: <21724be2.0110120733.1c8bafa9@posting.google.com>

Hi, 

Could someone please help on this ?

my script  does not work  the way I expected because
it cannot tell the different between the data as shown below:

Below is data:

vcc:    2.30     2.50     2.70 
vcc:    3.0
vcc:    2.4      2.0

Here is my script: 
			if ( /vcc:\s*(\d\.\w*)\){
				$vccio1=$1;
			}				}
			if ( /vcc:\s*(\d\.\w*)\s*(\d.\w*)/){
				$vccio1=$1;

				$vccio2=$2;
											                }
			if ( /vcc:\s*(\d\.\w*)\s*(\d.\w*)\s*(\d.\w*)/){
				$vccio1=$1;
				$vccio2=$2;
				$vccio3=$3
			}	

Thanks advance for your help


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

Date: Fri, 12 Oct 2001 21:18:15 +0200
From: Laocoon <Laocoon@eudoramail.com>
Subject: Re: Pattern Matching
Message-Id: <Xns9138D8BB688BBLaocooneudoramailcom@62.153.159.134>

> my script  does not work  the way I expected because
> it cannot tell the different between the data as shown below:
> 
> Below is data:
> 
> vcc:    2.30     2.50     2.70 
> vcc:    3.0
> vcc:    2.4      2.0

$a = qr/\s*(\d\.\d+)?/;
if($data =~ /vcc:$a $a $a/) {

                 ...

}


hope this helps u..


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

Date: Fri, 12 Oct 2001 16:46:53 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: PATTERN Matching: Substitution problem
Message-Id: <3BC756BD.4FF80692@earthlink.net>

Jean-Philippe AVELANGE wrote:
> 
> i've been looking for a solution to the ???? below, that would
> (without having to change $PATTERN variable) replace values according
> to that PATTERN, by the new $v1 and $v2 in my $_ variable to give the
> final result displayed at the end. Something like: "Replace the $1 and
> $2 by $v1 and $v2".
> 
> thanks in advance,
> Jean-Philippe.
> 
> $PATTERN = "ABCD\d+-\d+-EFGH(\d+)-(\d+)";
> $_ = "ABCD1-2EFGH3-4";
> ( $v1, $v2 ) = /$PATTERN/;
> $v1++;
> $v2++;
> ????
> print;
> 
> ABCD1-2EFGH4-5

How about:
my $index = 0;
$_ = "ABCD1-2EFGH3-4";
/ABCD\d+-\d+-/g && substr($_,pos) =~ s/(\d+)/$1 + 1/eg;

-- 
    "Just how stupid are you Kuno?"
    "Verily, Tatewaki Kuno knows no limits."


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

Date: 12 Oct 2001 09:32:33 -0700
From: jsmith@portiva.com (Jason Smith)
Subject: Problem determining the mime-type of a file upload.
Message-Id: <5a933ddf.0110120832.3eb8a97b@posting.google.com>

Hello All,

I have a script that I use to allow users to upload files of an
arbitrary type
using the multipart/form-data html form.  I'm currently using CGI.pm
to retrieve the mime-type with the following:

use strict;
use CGI qw(:standard);
my $query = new CGI;
my $filename = $query->param('upfile');
my $type = $query->uploadInfo($filename)->{'Content-Type'};
if ($type =~ /text/) {
    while (<$filename>) {
    print "$_";
    $blob .= $_;
    }
 } else {
    while($bytesread=read($filename,$buffer,1024)) {
    $blob .= $buffer;
    }
}

The script then continues to upload the attachment as a BLOB into an
oracle database.  On my development machine, this works just like it
is supposed to, but I've moved the code over to another machine for
more testing, and I can no longer access the mime-type.  If I hardcode
the type to text, (or another type that matches the type of file I'm
uploading) then everything works as it is supposed to.  This tells me
that the browser is actually sending the information, but I'm losing
the mime-type for some odd reason.  I'm wondering if anyone else has
run across anything similar.  The versions I'm using of everything are
as follows:

Red Hat Linux 7.0 - kernel 2.4.7
Apache web server version 1.3
Perl 5.6.0
CGI.pm - version 2.76


The machine that everything works correctly on is
Red Hat Linux 6.2 - 2.2.19
Apache web server version 1.3
Perl 5.005_03
CGI.pm - 2.46

Any thoughts or comments are greatly appreciated.  If someone has an
idea, but needs more information, I'll be glad to post it.

Thanks,
Jason Smith


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

Date: 12 Oct 2001 22:01:39 GMT
From: abigail@foad.org (Abigail)
Subject: Re: question about MAP function, please help.
Message-Id: <slrn9seq1e.vs1.abigail@alexandra.xs4all.nl>

Anno Siegel (anno4000@lublin.zrz.tu-berlin.de) wrote on MMCMLXIII
September MCMXCIII in <URL:news:9q3tkn$rhb$2@mamenchi.zrz.TU-Berlin.DE>:
}} According to Doug King  <Doug.King@abh.siemens.com>:
}} > "Thomas Btzler" wrote:
}} > > 
}} > > On Sun, 07 Oct 2001, clintp@geeksalad.org (Clinton A. Pierce) wrote:
}} > > >map is used to iterate over one list, returning another list.  Essentially
}} > > >doing a transformation of the first into the second.  The second list can
}} > > >be larger, smaller, related to or have nothing to do with the first list
}} > > >at all.
}} > > 
}} > > One thing to bear  in mind is that $_ is an alias for the elements of
}} > > the source list. If you modify $_, you also modify your input. Doing
}} > > this is usually considered a boo-boo.
}} > 
}} > I often use map to *intentionally* modify all the elements in an array:
}} > 
}} > map { s/asdf/lkj/ } @arr;
}} 
}}     s/asdf/lkj/ for @arr;


      CARPARK:
   {  my $fookie = 0; BUBBLE:
      last CARPARK if $fookie >= @arr;
      $arr [$fookie ++] =~ s/asdf/lkj/;
      goto BUBBLE
   }


Abigail
-- 
perl -wle 'print prototype sub "Just another Perl Hacker" {};'
#    A flying eagle. A
#    beetle flies above a lake.
#    A bear. A Bishop.


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

Date: Fri, 12 Oct 2001 16:14:09 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Quicker way to loop through directories?
Message-Id: <3BC74F11.1F093B58@earthlink.net>

S Warhurst wrote:
> 
> I currently have a routine that searches through each of several
> thousand subdirectories and totals up the size of a specific type of
> file. It goes like this:
> 
> if(opendir(DIR, "f:/list-logs")) {
>   while($dir = readdir(DIR))   {
>     if(-d "f:/list-logs/$dir" and $dir ne "." and $dir ne "..") {
>       if(opendir(SUB, "$listlogs/$dir")) {
>         while($file = readdir(SUB)) {
>           if($file =~ /\.log.+/i) {
>             $total += -s "$listlogs/$dir/$file";
>           }
>         }
>         closedir(SUB);
>       }
>     }
>   }
>   closedir(DIR);
> }
> 
> Under f:/list-logs there are about 3800 subdirectories and inside each
> I only want to total the size of files with the file extension
> ".logxxxx", hence the "/\.log.+/" match.
> 
> Now, this routine takes ~15 minutes on my test machine. Can anyone
> think of an imginative way of speeding it up?

foreach my $file (glob "f:/list-logs/*/*.log*") {
    $total += -s $file;
}

foreach my $file (glob "f:/list-logs/*/*.log*") {
    open(my ($foo), "<$file") or next;
    $total += -s $foo;
}

foreach my $dir ( do { 
    opendir(my ($foo), "f:/list-logs")
        or die "Couldn't opendir f:/list-logs: $!";
    grep !/^\.\.?$/, readdir($foo)
}) {
    substr( $dir, 0, 0, "f:/list-logs/" );
    foreach my $file ( do { 
        opendir(my ($sub), $dir) or next;
        grep /\.log./, readdir($sub);
    }) {
        $total += -s "$dir/$file";
        # open my ($fh), "<$dir/$file" or next;
        # $total += -s $fh;
    }
}

For some filesystems, it may be faster to open the file and stat the
filehandle, than to stat the filename.  YYMV.

NB: None of the above code is tested.

-- 
    "Just how stupid are you Kuno?"
    "Verily, Tatewaki Kuno knows no limits."


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

Date: Fri, 12 Oct 2001 17:24:04 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Sorting larg arrays
Message-Id: <3BC75F74.AEE51B5@earthlink.net>

Mr. Mostrom wrote:
> 
> Hello!
> 
> Is it possible to sort an array of more then 100.000 entries in a fast
> way?
> 
> For example (running under Unix/Linux):
> 
> #!/usr/bin/perl
> #
> # Filename: example.pl
> #
> 
> @DIRS="/to/dir1 /to/dir2 /to/dir3";

Sure, replace the code I snipped with:
@TOTAL = `find @DIRS | sort`;

Yes you can do sorting in perl, but if the list is really big, bigger
than perl can hold in memory, the sort program might be your best bet,
since it can do an external sort.

-- 
    "Just how stupid are you Kuno?"
    "Verily, Tatewaki Kuno knows no limits."


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

Date: Fri, 12 Oct 2001 16:23:39 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: sorting question
Message-Id: <3BC7514B.9173492E@earthlink.net>

Usyk, Walter [SKY:1P67:EXCH] wrote:
> 
> I have a question about sorting. Lets suppose that I have an unsorted
> list with the following entries:
> 
> test.2 test.6 test.1 test.9 test.22 test.66 sort.1 sort.10 test.11
> test.7 test.8 test.10 test.100 zest.10
> 
> If I sort it with perl I will get the following:
> 
> sort.1 sort.10 test.1 test.10 test.100 test.11 test.2 test.22 test.6
> test.66 test.7 test.8 test.9 zest.10
> 
> What I really want is this order :
> 
> sorted.1 sorted.10 test.1 test.2 test.6 test.7 test.8 test.9 test.10
> test.22 test.66 test.100 zest.10

my @list = qw( test.2 test.6 test.1 test.9
        test.22 test.66 sort.1 sort.10 test.11
        test.7 test.8 test.10 test.100 zest.10
);
my @sorted = map { $_->[0] }
        sort { $a->[1] cmp $b->[1] or $a->[2] <=> $b->[2] }
        map { ($_, split /(?>=\d)/, $_, 2) }
        @list;

-- 
    "Just how stupid are you Kuno?"
    "Verily, Tatewaki Kuno knows no limits."


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

Date: Fri, 12 Oct 2001 16:53:33 -0700
From: Amer Neely <aneely@softouch.on.ca>
Subject: Re: Strange behaviour or I/O error (Idiot Operator)? - solved?
Message-Id: <3BC7827D.E6C6C211@softouch.on.ca>

Amer Neely wrote:
> 
> Forgive me if this has already been covered - I've gone through the
> relevant google postings and not found anything like this.
> 
> I have a log file consisting of data in the form:
> 
> 2000-07-12|12:19:43|209.240.200.81|Mozilla/3.0 WebTV/1.2 (compatible;
> MSIE 2.0)
> 2000-07-12|12:19:56|209.240.200.81|Mozilla/3.0 WebTV/1.2 (compatible;
> MSIE 2.0)
> 2000-07-12|12:31:25|216.254.150.180|Mozilla/4.0 (compatible; MSIE 5.0;
> Windows NT; DigExt)
> 2000-07-12|12:35:59|216.209.106.187|Mozilla/4.51 [en] (Win95; U)
> 2000-07-12|12:37:52|216.209.106.187|Mozilla/4.51 [en] (Win95; U)
> 
> That is: date|time|IP|agent
> 
> It consists of almost 6000 lines which I've thrown into a __DATA__ block
> at the end of my script. I then throw this into @IN.
> 
> Walking through the array (@IN) and simply printing it out introduces
> blank lines in the output at a few locations - always the same
> locations.
> 
> Checked and fixed the input data for multiple newlines and bad data -
> still produced blank lines.
> 
> chomped @IN: output on one line as expected.
> chomped each line: output on one line as expected.
> chomped each line then added \n back to each line upon printing: blank
> lines show up again in the same locations.
> 
> Introducing 'sort' didn't change the behaviour - blank lines in the
> *same* locations as unsorted output.
> 
> This really has me stumped. I can't go further (splitting out and
> sorting by IP) until this issue with the blank lines is resolved. If
> anyone has a light to shine on this I would certainly appreciate it.
> Thanks in advance.

I believe I have discovered the source of this problem, but have yet to
find a solution. 

My output was going through a web server so I was viewing the output in
my browser (NS4.77). In viewing the same code with IE5 however, there
were no blank lines in any of the several variations. This seems to
indicate a problem NS has with outputting large amounts of textual data.
I'd be interested in hearing of anyone else running into something like
this, and especially if you hava a solution. Email me below for further
discussion.

-- 
Amer Neely aneely@softouch.on.ca
Softouch Information Services: www.softouch.on.ca/
Perl / CGI programming for shopping carts, data entry forms.
"We make web sites work!"


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

Date: 12 Oct 2001 09:06:36 -0700
From: rjstefani@yahoo.com (Ryan)
Subject: Text Scraper, Cookies, and Oracle Portal (9ias)...please help me...please...i'm dyin here...
Message-Id: <6290deb5.0110120806.40f4fc21@posting.google.com>

I'm working on a project where I have to give a text-only version of
an Oracle Portal (a.s.: 1.0.2.2, db: 8.1.7).

The easiest, safest way to do this is via a text-scraper. I've can
open a socket to the Oracle Portal pages, but Oracle Portal uses TONS
of cookies to maintain session info (like who's logged in).

The issue is, since I'm scraping the content from another server
(using Perl v5.004_04 for i386-linux) for the socket and scraping),
I've essentially created another middle tier (db-> application
server-> scraper-> browser) which prevents standard cookie processing.

So, my logical next step would be to get the cookies from the client,
send them to the scraper to be included in the GET command from the
application server. BUT, I can't get the cookie data using
CGI::Cookie, CGI, or through some manual, open source examples.

How can I get cookies from the client machine, of course, regardless
of OS and browser...

Thanks,
Ryan Stefani


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

Date: Fri, 12 Oct 2001 20:01:09 +0100
From: "Test Account" <riccardo@ebi.ac.uk>
Subject: UDP server
Message-Id: <G7Hx7.6611$oE5.659567@news2-win.server.ntlworld.com>

Hi, I'm trying to write a UDP server; what I've come up so far is (please
don't laugh):

#!/usr/bin/perl -w

use strict;
use IO::Socket;

$| = 1;

my $msgin;
my $listenport = 9999;
my $maxlen = 5000;
my $port = shift || $listenport;
$SIG{'INT'} = sub {exit 0};
my $sock = IO::Socket::INET->new(Proto=>'udp',LocalPort=>$port) or die
"Cannot open socket!";

while (1) {
    next unless $sock->recv($msgin,$maxlen);
    do_stuff_with($msgin);
    }
}

This is rather simple-minded, and it only accepts one message at a time, and
it can't listen for more messages while it's doing something with $msgin.
How can I modify this to be "multi-threaded"?

Thanks,
riccardo






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

Date: Fri, 12 Oct 2001 14:49:38 -0500
From: trammell@haqq.hypersloth.invalid (John J. Trammell)
Subject: Re: UDP server
Message-Id: <slrn9seiai.1sg.trammell@haqq.hypersloth.net>

On Fri, 12 Oct 2001 20:01:09 +0100, Test Account <riccardo@ebi.ac.uk> wrote:
> Hi, I'm trying to write a UDP server; what I've come up so far is (please
> don't laugh):

A Usenet Death Penalty server?  Sweet, I could use one of those.

-- 
ERR_NOSIG: sigfile out of commission while removing 'all your base'
           references.


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

Date: Fri, 12 Oct 2001 20:09:31 GMT
From: "Gerard Lapidario" <ghed9@netzero.net>
Subject: Value sort Display by key (interesting)
Message-Id: <%5Ix7.525$k82.412151@paloalto-snr2.gtei.net>

Hello perl experts.  I have an interesting problem here, 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 :-)






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

Date: Fri, 12 Oct 2001 15:24:17 -0500
From: Tony Curtis <tony_curtis32@yahoo.com>
Subject: Re: Value sort Display by key (interesting)
Message-Id: <87g08oli8e.fsf@limey.hpcc.uh.edu>

>> On Fri, 12 Oct 2001 20:09:31 GMT,
>> "Gerard Lapidario" <ghed9@netzero.net> said:

> Hello perl experts.  I have an interesting problem here,
> 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 => syntax is more common for hashes, b.t.w.

%files = ( A => 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.

You sort the keys by comparing the values.  The sort
comparison is thus:

    $files{$b} <=> $files{a}

However, you then apparently want to sort keys whose
values are the same, ascending (alpha, stringwise sort),
so:

    $files{$b} <=> $files{a} || $b cmp $a

(which I believe is a fairly trivial instance of the
Guttman-rosler transform.  Yes?)

hth
t
-- 
Oh!  I've said too much.  Smithers, use the amnesia ray.


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

Date: Fri, 12 Oct 2001 15:25:25 -0500
From: Tony Curtis <tony_curtis32@yahoo.com>
Subject: Re: Value sort Display by key (interesting)
Message-Id: <87bsjcli6i.fsf@limey.hpcc.uh.edu>

>> On Fri, 12 Oct 2001 15:24:17 -0500,
>> Tony Curtis <tony_curtis32@yahoo.com> said:

> However, you then apparently want to sort keys whose
> values are the same, ascending (alpha, stringwise sort),
> so:

>     $files{$b} <=> $files{a} || $b cmp $a

DOH!                              $a cmp $b

Fridays!

-- 
Oh!  I've said too much.  Smithers, use the amnesia ray.


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

Date: Fri, 12 Oct 2001 20:48:45 GMT
From: "Gerard Lapidario" <ghed9@netzero.net>
Subject: Re: Value sort Display by key (interesting)
Message-Id: <NGIx7.531$k82.450861@paloalto-snr2.gtei.net>

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)
{
   print "$cnt \n";
}

I didn't get the desired result.  What am I doing wrong :-(

Thanks...

"Tony Curtis" <tony_curtis32@yahoo.com> wrote in message
news:87bsjcli6i.fsf@limey.hpcc.uh.edu...
> >> On Fri, 12 Oct 2001 15:24:17 -0500,
> >> Tony Curtis <tony_curtis32@yahoo.com> said:
>
> > However, you then apparently want to sort keys whose
> > values are the same, ascending (alpha, stringwise sort),
> > so:
>
> >     $files{$b} <=> $files{a} || $b cmp $a
>
> DOH!                              $a cmp $b
>
> Fridays!
>
> --
> Oh!  I've said too much.  Smithers, use the amnesia ray.




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

Date: Fri, 12 Oct 2001 16:13:24 -0500
From: Tony Curtis <tony_curtis32@yahoo.com>
Subject: Re: Value sort Display by key (interesting)
Message-Id: <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: Fri, 12 Oct 2001 16:10:42 -0500
From: Michael Carman <mjcarman@home.com>
Subject: Re: Value sort Display by key (interesting)
Message-Id: <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: Fri, 12 Oct 2001 13:11:42 -0400
From: Joel Caughran <caughran@chem.uga.edu>
Subject: Working with Bit Vectors
Message-Id: <3BC7244E.7D4AE5FE@chem.uga.edu>

I'm working on a short script that represents people's schedules and
job's times as bit vectors.  It works but it seems to me that there must
be a better way to see if there is a conflict between a person's
schedule and a job's time.

Here is a sample script that shows the two ways I have to check the bit
vectors

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#!perl

use strict;

my $bitmap1;
my $bitmap2;
my $bitmap3;

vec($bitmap1, 1, 1) = 1;
vec($bitmap1, 2, 1) = 1;
print "bitmap1 : ", unpack("b*", $bitmap1), "\n";

vec($bitmap2, 3, 1) = 1;
vec($bitmap2, 5, 1) = 1;
print "bitmap2 : ", unpack("b*", $bitmap2), "\n";

print "b1 & b2 : ",unpack("b*", $bitmap1 & $bitmap2), "\n";

#	Method 1	###################################
if (unpack("b*", $bitmap1 & $bitmap2) =~ /^0+$/) {
	print "No Conflict\n";
} else {
	print "Conflict\n";
}
###########################################################

#	Method 2	###################################
$bitmap3 = $bitmap1 & $bitmap2;
my $conflict = 0;
for (my $i=0; $i<=length($bitmap3)*8; $i++) {
	if (vec($bitmap3, $i, 1)) {
     	$conflict = 1;
     }
}

if ($conflict) {
	print "Conflict\n";
} else {
	print "No Conflict\n";
}
###########################################################
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Is there a better way that either method 1 or 2?  Is one of these two
methods better/preferred?

Thanks for any assitance.

Joel Caughran
-- 
Joel A Caughran                 caughran@chem.uga.edu
Chemistry Learning Center       caughran@uga.edu
Department of Chemistry
University of Georgia           (706) 542-1906 voice
Athens, Georgia 30602-2556      (706) 542-9454 fax


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

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


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