[22855] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 5076 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Jun 4 21:06:15 2003

Date: Wed, 4 Jun 2003 18: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)

Perl-Users Digest           Wed, 4 Jun 2003     Volume: 10 Number: 5076

Today's topics:
    Re: Efficient Date Sorting <ben.goldberg@hotpop.com>
        html parser questions <tequila-stuff@cox.net>
    Re: Need Help with directories <wksmith@optonline.net>
    Re: Perl fork() question <ben.goldberg@hotpop.com>
        perl <john62@electronmail.com>
    Re: perl <jurgenex@hotmail.com>
        Printing VISIO documents using PERL. (Chad Johnson)
        Problems with suidperl on AIX, "Can't reswap uid and eu (LOlson)
    Re: Reference types (Jay Tilton)
        regex based on numeric value (Rick Ferrante)
    Re: regex based on numeric value <hasting@agere.com>
    Re: Regex with numeric evaluation <noreply@gunnar.cc>
    Re: Regex with numeric evaluation <skuo@mtwhitney.nsc.com>
        Replacing Unsafe HTML Characters <sammie@greatergreen.com>
    Re: Replacing Unsafe HTML Characters <ericisgreat@attbroadband.com>
    Re: Replacing users password input with asterisks <kalinabears@hdc.com.au>
    Re: Replacing users password input with asterisks <zentara@highstream.net>
        SORT  sort hash with alpha-numeric keys <hasting@agere.com>
    Re: SORT  sort hash with alpha-numeric keys <noreply@gunnar.cc>
    Re: SORT  sort hash with alpha-numeric keys <michael.p.broida@boeing.com>
    Re: SORT  sort hash with alpha-numeric keys <roobarb@fastmail.fm>
    Re: SORT  sort hash with alpha-numeric keys <TruthXayer@yahoo.com>
    Re: SORT  sort hash with alpha-numeric keys <michael.p.broida@boeing.com>
    Re: strange perl behaviour with cgi <jurgenex@hotmail.com>
    Re: use()ing a module by pasting it in code <minceme@start.no>
    Re: while loop (Kevin Su)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 04 Jun 2003 19:15:44 -0400
From: Benjamin Goldberg <ben.goldberg@hotpop.com>
Subject: Re: Efficient Date Sorting
Message-Id: <3EDE7DA0.B9B30591@hotpop.com>

Matthew Braid wrote:
> 
> Hi all,
> 
> I have to write a coderef to pass to sort so that it takes a list of
> 'human-readable' dates. I can't preparse the list in any way first as
> it is being used as part of a Tk widget (Tk::MListbox), so I can't do
> any groovy map-sort-map tricks.
[snip]

How about something like the following:

   my ($am, $pm) = ($lang->AM, $lang->PM);
   sub canonicalize {
      my $date = shift;
      my @parts = $date =~ m{^\s*
         (\d\d?) : (\d\d) \s
         ( \Q$am\E | \Q$pm\E )
         (?=\s) .*? \s
         (\d\d?) / (\d\d) / (\d\d\d\d)
      \z}x;
      @parts = (0, 0, "am", 1, 1, 1900) unless @parts;
      $parts[0] += 12 if lc($parts[2]) eq $pm;
      --$parts[1];
      timelocal(0, @parts[1, 0, 3, 4, 5]);
   }

   my %date_cache;
   my $date_sort = sub {
      my $at = $date_cache{$a} ||= canonicalize($a);
      my $bt = $date_cache{$b} ||= canonicalize($b);
      return $at <=> $bt;
   };

-- 
$a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
);{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$@[$a%6
]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}


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

Date: Wed, 04 Jun 2003 19:48:55 GMT
From: "j" <tequila-stuff@cox.net>
Subject: html parser questions
Message-Id: <H2sDa.14076$Ik3.9855@news1.east.cox.net>

In perldoc, they give the example below to parse the title from an html
document.  Can someone explain what shift->eof if shift eq "title";  does in
the line marked with ->?    What I could use is a code example of a start
handler that can pass the title text back in the return value.

Thanks,
Jay


      use HTML::Parser ();

      sub start_handler
      {
        return if shift ne "title";
        my $self = shift;
        $self->handler(text => sub { print shift }, "dtext");
->        $self->handler(end  => sub { shift->eof if shift eq "title"; },
                               "tagname,self");
      }

      my $p = HTML::Parser->new(api_version => 3);
      $p->handler( start => \&start_handler, "tagname,self");
      $p->parse_file(shift || die) || die $!;
      print "\n";




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

Date: Wed, 04 Jun 2003 18:25:29 GMT
From: "Bill Smith" <wksmith@optonline.net>
Subject: Re: Need Help with directories
Message-Id: <tQqDa.13279$sG3.6379807@news4.srv.hcvlny.cv.net>

Sorry,
Ignore my previous post, I misread your error processing.


Bill Smith






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

Date: Wed, 04 Jun 2003 20:44:44 -0400
From: Benjamin Goldberg <ben.goldberg@hotpop.com>
Subject: Re: Perl fork() question
Message-Id: <3EDE927C.DB7D2523@hotpop.com>

Prodigy wrote:
> 
> Hello,
> 
> I wrote a script which builds serial IO::Sockets, but due to the fact
> that it needs to build a lot of sockets, it takes very much time,
> since every connection lasts for about 10 seconds.

Instead of dealing with connections one at a time, multiplex them using
IO::Select.

If your program's sufficiently small and clearly written, post it here;
you'll get some pointers on how to rewrite it so that many socket
connections can be handled at once.

-- 
$a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
);{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$@[$a%6
]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}


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

Date: Wed, 04 Jun 2003 17:36:32 -0400
From: j <john62@electronmail.com>
Subject: perl
Message-Id: <3EDE6660.9010902@electronmail.com>

could someone recommend a good book on perl for me?


-------------------------------------------------------------
Coldmail.us offers free web access to newsgroups.
http://www.coldmail.us/
-------------------------------------------------------------



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

Date: Wed, 04 Jun 2003 21:47:10 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: perl
Message-Id: <yNtDa.44290$da1.19564@nwrddc03.gnilink.net>

j wrote:
> could someone recommend a good book on perl for me?

Please see The Fine Manual: "perldoc -q books"

jue




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

Date: 4 Jun 2003 11:53:11 -0700
From: cjohnso9@amfam.com (Chad Johnson)
Subject: Printing VISIO documents using PERL.
Message-Id: <a3c05aa8.0306041053.6e656430@posting.google.com>

I am new to PERL and I am trying to write a script to do the
following:
  1. Read in a VISIO document.
  2. Change the printing options to 'TRUE TYPE FONT' and 'DOWNLOAD AS
SOFTFONT'.
  3. Print the document.

Does anyone have any examples of scripts that can do something like
this. If so please send me them.

Thanks


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

Date: 4 Jun 2003 14:13:40 -0700
From: lolson@lynx.nisc.cc (LOlson)
Subject: Problems with suidperl on AIX, "Can't reswap uid and euid."
Message-Id: <35437e43.0306041313.2f6b03ac@posting.google.com>

I need to get suid PERL scripts working on AIX.
First, let me say I know suid scripts are "bad" and suidperl is
deprecated, but we already have a number of PERL scripts that work on
Linux, Tru64, and SCO and I need to get them working ASAP on AIX as
well and do not have the time to rewrite them all, and would very much
like to keep them as portable as they are now.
I am trying to get PERL 5.6.1 working on AIX 4.3.3 and 5.1, but so far
I keep getting the "Can't reswap uid and euid." error.  There are many
posts out here about this subject, some claiming that they have gotten
it working, but I have not found any with a solution that I can make
work for me, and most seem to refer to much older versions of PERL.
If anyone knows how to get suid PERL scripts working on AIX please let
me know.
Thanks,
Leif


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

Date: Wed, 04 Jun 2003 21:56:34 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: Reference types
Message-Id: <3ede6ad6.83145846@news.erols.com>

david.pointon@tesco.net (Dave Pointon) wrote:
: tadmc@augustmail.com (Tad McClellan) wrote in message 
: news:<slrnbdpuva.hue.tadmc@magna.augustmail.com>...
: > Dave Pointon <david.pointon@tesco.net> wrote:
: > 
: > > Can anyone help ...
: > 
: > If we knew what the _real_ problem was we might be able to help...
: > 
: > > I'm doing my nut trying to distinguish between references to anonymous
: > > and named data elements
: > 
: > Why do you think that you need to distinguish them?
: 
: I need a sub, which when passed a ref., takes a copy of either the
: ref. or the structure dependant on whether the ref. is to named or
: anonymous data 

It's an extraordinary thing to want to know, and it would require
extraordinary measures to find out.  One would have to be certain this
is the only possible solution to the problem before contemplating how
to code it.  Right now, it's not even clear what the root problem is.

Posting a short, complete program whose behavior is affected by the
distinction between named and anonymous data structures should explain
the problem well.

: e.g. assuming
: 
: my %hash = (
:   field1 => \%someOtherHash,
:   field2 => {
:     field1 => undef,
:     field2 => undef,
:   },
: );
: 
: Calling sub(\%hash) should result in field1 being copied as is i.e. an
: ARRAY ref., whereas field2 (the anonymous hash) is copied wholesale.
  ^^^^^^^^^
  ^^^^^^^^^ Do you mean a hash reference?

The unusual terms "copied as is" and "copied wholesale" need
explanation.  Perhaps the more common terms "shallow copy" and "deep
copy" approximate your meaning.

What should it do when one of those anonymous hashrefs is an object?
Copy its underlying data structure, clone it into a new object, or
leave it alone and create another reference to it?

I'm sure other readers besides myself are getting bad vibes on this
idea.  If we could understand why you think you need this (there is a
shedload of imaginable reasons), we could demonstrate why you don't,
or propose a different technique to achieve your goal.



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

Date: 4 Jun 2003 11:33:24 -0700
From: rferrante@tnsi.com (Rick Ferrante)
Subject: regex based on numeric value
Message-Id: <726cd874.0306041033.4438b289@posting.google.com>

Given a string like "Level 87.34"

Is there a regex that will match if the string is like "Level
([0-9.]+)" and \1 is > 75 ?

Rick


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

Date: Wed, 4 Jun 2003 15:57:23 -0400
From: "Joe Hasting" <hasting@agere.com>
Subject: Re: regex based on numeric value
Message-Id: <bbliv4$enr$1@alageremail2.agere.com>

"Rick Ferrante" <rferrante@tnsi.com> wrote in message
news:726cd874.0306041033.4438b289@posting.google.com...
> Given a string like "Level 87.34"
>
> Is there a regex that will match if the string is like "Level
> ([0-9.]+)" and \1 is > 75 ?
>
> Rick


Here is a simple regex approach:

$tmp = "75.75665";
if ($tmp =~ /\.[7][5-9][0-9]+/ || $tmp =~ /\.[8-9][0-9]+/) { print
">X.75\n";}






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

Date: Wed, 04 Jun 2003 20:24:14 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Regex with numeric evaluation
Message-Id: <bbldo1$av0hi$1@ID-184292.news.dfncis.de>

Rick Ferrante wrote:
> Can anyone modify the regex below in line 2 to have it also check
> that level is greater than threshold?
> 
> 1: $msg="Level 86.3 (threshold 85)";
> 2: ($level,$threshold) = $msg=~/Level ([0-9.]+) \(threshold 
> ([0-9.]+)\)/;
> 3: print "$level $threshold\n" if ($level >=$threshold);

That's typically not done in regexes. Why would you want it?

/ Gunnar

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl



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

Date: Wed, 4 Jun 2003 11:42:09 -0700
From: Steven Kuo <skuo@mtwhitney.nsc.com>
Subject: Re: Regex with numeric evaluation
Message-Id: <Pine.GSO.4.21.0306041138360.981-100000@mtwhitney.nsc.com>

On 4 Jun 2003, Rick Ferrante wrote:

> Can anyone modify the regex below in line 2 to have it also check that
> level is greater than threshold?
> 
> 1: $msg="Level 86.3 (threshold 85)";
> 2: ($level,$threshold) = $msg=~/Level ([0-9.]+) \(threshold
> ([0-9.]+)\)/;
> 3: print "$level $threshold\n" if ($level >=$threshold);
> 
> Thanks,
> Rick
> 


Using code eval in a regex is not very efficient, but if you're
interested in the arcane and experimental, try:


#! /usr/local/bin/perl
use strict;
use warnings;
while (<DATA>) {
    if (/Level ([\d.]+) \(threshold ((?>[\d.]+))(??{($1>=$2)? '\)' : '\d'})/) {
	print;
    }
}

__DATA__
Level 86.3 (threshold 85)
Level 85 (threshold 86.3)
Level 85 (threshold 85.0)


And, of course, consult 'perldoc perlre'.
-- 
Hope this helps,
Steven



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

Date: Thu, 05 Jun 2003 00:54:17 GMT
From: "Brad Walton" <sammie@greatergreen.com>
Subject: Replacing Unsafe HTML Characters
Message-Id: <ZwwDa.22481$d51.69265@sccrnsc01>

I am trying to replace unsafe HTML characters with safe ones. The following
works, but it only replaces the first instance, and I need to replace ALL of
them. For example, if I had a word:

'<something><somethingmore>'

It would replace the first '<' with '&lt;', but leave everything else
untouched.

This is the code:
----
my %entities = ( '&' => '&amp;', '<' => '&lt;', '>' => '&gt;' );
 ...
@htmlparams[1] =~ s/[&<>]/$entities{$&}/e;
----

Thanks for any help,
Brad





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

Date: Thu, 05 Jun 2003 01:04:36 GMT
From: "HongKongHooker" <ericisgreat@attbroadband.com>
Subject: Re: Replacing Unsafe HTML Characters
Message-Id: <EGwDa.71722$M01.42296@sccrnsc02>

This code will finish on the first eval, add the g "global" specifier like
so..

@htmlparams[1] =~ s/[&<>]/$entities{$&}/eg;


"Brad Walton" <sammie@greatergreen.com> wrote in message
news:ZwwDa.22481$d51.69265@sccrnsc01...
> I am trying to replace unsafe HTML characters with safe ones. The
following
> works, but it only replaces the first instance, and I need to replace ALL
of
> them. For example, if I had a word:
>
> '<something><somethingmore>'
>
> It would replace the first '<' with '&lt;', but leave everything else
> untouched.
>
> This is the code:
> ----
> my %entities = ( '&' => '&amp;', '<' => '&lt;', '>' => '&gt;' );
> ...
> @htmlparams[1] =~ s/[&<>]/$entities{$&}/e;
> ----
>
> Thanks for any help,
> Brad
>
>
>




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

Date: Thu, 5 Jun 2003 05:24:21 +1000
From: "Sisyphus" <kalinabears@hdc.com.au>
Subject: Re: Replacing users password input with asterisks
Message-Id: <3ede4887$0$21287@echo-01.iinet.net.au>


"Steven Danna" <MissingWords@hotmail.com> wrote in message

>
>    I only had to make two changes.  First I set $|=1 ,so the *'s would
> come up right after the person typed the letter(then had to look up
> what $| should be normaly so I could set it back), and I also changed
> the way it handles backspace characters.
>

I didn't have to do that on my (Win2k) box - the output was automatically
flushed - but setting $| to 1 doesn't break anything for me, so I've no
problem with using it.
I was testing on perl 5.6.1 (where it shouldn't be necessary). Presumably
you're using perl 5.8.0.

>    In your example, the script first returns to the beginning of the
> line, and then adds the "\0" characters; however, I need the "Enter
> Password:" or equivalent string on the same line as the input. After
> playing with your code and learning what everything was doing, I came
> up with this:
>
> -------------------------------------
> sub mask_pwd {
> $|=1;
> my $password = '';
> my $key;
> while(1) {
>       while(not defined($key = ReadKey(-1))) {};
>       if(ord($key) != 13) {
>          if(ord($key) == 8) {
>             print "\b\0\b";

That doesn't quite work for me - hitting the backspace button once removes
one asterisk from the screen, but moves the cursor back 2 spaces (not sure
why that's happening). If I hit the backspace button twice in succession
things start to look really weird.
'print "\b \b";' does however produce the desired result. I would suggest
changing to that (assuming it also works for you).

Using "\b" instead of "\r" is a much cleaner solution, btw.

After I posted I started to wonder whether, with some shells, it might still
be necessary to ReadMode('noecho') ...... don't know ... still wondering :-)

>             chop($password);
>             }
>          else {
>             $password .= $key;
>             print "*";
>             }
>          }
>       else {last;}
>       }
> $|=0;
> return $password;
> }
> --------------------------------------------

Cheers,
Rob





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

Date: Wed, 04 Jun 2003 17:23:21 -0400
From: zentara <zentara@highstream.net>
Subject: Re: Replacing users password input with asterisks
Message-Id: <8losdvglh441lik13pabtipli3ppu9a80q@4ax.com>

On Wed, 4 Jun 2003 12:20:51 +1000, "Sisyphus" <kalinabears@hdc.com.au>
wrote:

>
>"Steven Danna" <MissingWords@hotmail.com> wrote in message
>
><snip>
>> However, I would still like the user to be able to see how
>> many characters he or she has entered(or backspaced).  Preferably the
>> users input would echo onto the screen as asterisks, or any other
>> symbolic character(# or ~ perhaps).
>>
><snip>
>
>Just some ideas on how you can approach this:

Well I took your idea, and made it work. :-)  Here you go.

#!/usr/bin/perl
use strict;
use Term::ReadKey;

my $pwd = mask_pwd();
print "\n$pwd\n";

##############################
sub mask_pwd {
ReadMode(3);
    my $password = '';
    my $key;
    print "Enter password\n";

while (1) {
     my $key = ReadKey(0);
         if ( ord($key) == 10 ){last}    # "Enter"
         if ( ord($key) == 127 ) { # ie "Backspace"
                chop($password);
                print chr(8),' ',chr(8);
	   }else{
             $password .= $key;
              print "*";
            }
}
ReadMode(0);
return $password;
}
__END__





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

Date: Wed, 4 Jun 2003 15:34:19 -0400
From: "Joe Hasting" <hasting@agere.com>
Subject: SORT  sort hash with alpha-numeric keys
Message-Id: <bblhjs$ebq$1@alageremail2.agere.com>

I've been through the FAQs and mauals, but cannot find info on sort using
alphanumeric.

Let's use this has table:
%hash = ( "L8" => "Left 8", "L9" => "Left 9", "L80" => "Left 80", "L90" =>
"Left 90", "R8" => "Right 8", "R9" => "Right 9", "R80" => "Right 80", "R90"
=> "Right 90") ;

NOTE: these are coordinates,  I would like to be able to use L08 & L09 for
example, but cannot.

I want the sort output to be  (sorting of hash keys or hash values would be
fine):
L8   Left 8
L9   Left 9
L80 Left 80
L90 Left 90
R8   Right 8
R9   Right 9
R80 Right 80
R90 Right 90

I have tried all permutations of <=>, cmp, and lc,  with sort but I get
either something completly messed up or this:
L8   Left 8
L80 Left 80
L9   Left 9
L90 Left 90
R8   Right 8
R80 Right 80
R9   Right 9
R90 Right 90

Can anyone give some guidance on how I can do this alphnumeric sort?  I have
attached a piece of my test code below with the testing of the 3 sort
operators.

Thanks for any help.
Joe Hasting


-------------------------------------------------------------
@sorted = sort { $a cmp $b } keys %hash;
foreach $hashref (@sorted) {
  print "ASCII-betical key sort  $hashref $hash{$hashref}\n";
}
print "\n\n";


@sorted = sort { $a <=> $b } keys %hash;
foreach $hashref (@sorted) {
  print "numerical key sort      $hashref $hash{$hashref}\n";
}
print "\n\n";


@sorted = sort { lc($a) cmp lc($b) } keys %hash;
foreach $hashref (@sorted) {
  print "alphabetical key sort   $hashref $hash{$hashref}\n";
}
print "\n\n";


@sorted = sort { $hash{$a} cmp $hash{$b} } keys %hash;
foreach $hashref (@sorted) {
  print "ASCII-betical value sort  $hashref $hash{$hashref}\n";
}
print "\n\n";


@sorted = sort { $hash{$a} <=> $hash{$b} } keys %hash;
foreach $hashref (@sorted) {
  print "numerical value sort      $hashref $hash{$hashref}\n";
}
print "\n\n";


@sorted = sort { lc($hash{$a}) cmp lc($hash{$b}) } keys %hash;
foreach $hashref (@sorted) {
  print "alphabetical value sort   $hashref $hash{$hashref}\n";
}
print "\n\n";






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

Date: Wed, 04 Jun 2003 22:26:42 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: SORT  sort hash with alpha-numeric keys
Message-Id: <bblku9$aujm2$1@ID-184292.news.dfncis.de>

Joe Hasting wrote:
> Let's use this has table:
> %hash = ( "L8" => "Left 8", "L9" => "Left 9", "L80" => "Left 80", "L90" =>
> "Left 90", "R8" => "Right 8", "R9" => "Right 9", "R80" => "Right 80", "R90"
> => "Right 90") ;
> 
> NOTE: these are coordinates,  I would like to be able to use L08 & L09 for
> example, but cannot.

Why not?

> I want the sort output to be  (sorting of hash keys or hash values would be
> fine):
> L8   Left 8
> L9   Left 9
> L80 Left 80
> L90 Left 90
> R8   Right 8
> R9   Right 9
> R80 Right 80
> R90 Right 90

One solution:

     my @sorted = sort {
         substr($a,0,1) cmp substr($b,0,1)
     or
         substr($a,1) <=> substr($b,1)
     } keys %hash;

/ Gunnar

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl



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

Date: Wed, 4 Jun 2003 20:31:43 GMT
From: "Michael P. Broida" <michael.p.broida@boeing.com>
Subject: Re: SORT  sort hash with alpha-numeric keys
Message-Id: <3EDE572F.AB2F73B@boeing.com>

Joe Hasting wrote:
> 
> I've been through the FAQs and mauals, but cannot find info on sort using
> alphanumeric.
> 
> Let's use this has table:
> %hash = ( "L8" => "Left 8", "L9" => "Left 9", "L80" => "Left 80", "L90" =>
> "Left 90", "R8" => "Right 8", "R9" => "Right 9", "R80" => "Right 80", "R90"
> => "Right 90") ;
> 
> NOTE: these are coordinates,  I would like to be able to use L08 & L09 for
> example, but cannot.
> 
> I want the sort output to be  (sorting of hash keys or hash values would be
> fine):
> L8   Left 8
> L9   Left 9
> L80 Left 80
> L90 Left 90
> R8   Right 8
> R9   Right 9
> R80 Right 80
> R90 Right 90
> 
> I have tried all permutations of <=>, cmp, and lc,  with sort but I get
> either something completly messed up or this:
> L8   Left 8
> L80 Left 80
> L9   Left 9
> L90 Left 90
> R8   Right 8
> R80 Right 80
> R9   Right 9
> R90 Right 90
> 
> Can anyone give some guidance on how I can do this alphnumeric sort?  I have
> attached a piece of my test code below with the testing of the 3 sort
> operators.

	Looks like you want a combination of alpha and numeric sorting.
	You want the ALPHA part of the hashkey to be sorted alphabetically,
	and, as a secondary sort key, you want the NUMERIC part of the
	hashkey to be sorted numerically.

	You'll have to SEPARATE the two parts of the key within your
	sort function and compare each part:  (UNTESTED AIR CODE)

	# There will be MANY other ways to do this,
	# but this approach should at least work.
	# Comments on why it won't work are welcome, as are improvement suggestions.
	# Flames because you don't LIKE the method are NOT welcome.  :)
	sub mysort
	{
		# Get key parts to avoid repetitive substr calls
		my $aSTART = substr($a,0,1);
		my $aEND = substr($a,1);
		my $bSTART = substr($b,0,1);
		my $bEND = substr($b,1);
		# Do comparisons
		if ($aSTART eq $bSTART)
		{
		   if ($aEND == $bEND)
		   {
		      return 0;
		   }
		   elsif ($aEND < $bEND)
		   {
		      return -1;
		   }
		   if ($aEND > $bEND)
		   {
		      return 1;
		   }
		}
		elsif (substr($a,0,1) lt substr($b,0,1))
		{
		   return -1;
		}
		elsif (substr($a,0,1) gt substr($b,0,1))
		{
		   return 1;
		}
	}
	# Not pretty, but I think that will get the job done,
	# ASSUMING your hash keys are always ONE alpha char,
	# and always followed by a number with NOTHING after it.

		Mike


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

Date: Wed, 4 Jun 2003 22:48:32 +0100
From: Keith Anderson <roobarb@fastmail.fm>
Subject: Re: SORT  sort hash with alpha-numeric keys
Message-Id: <FTtDa.33796$9C6.1503463@wards.force9.net>

On Wed, 4 Jun 2003 20:31:43 GMT
in news:<3EDE572F.AB2F73B@boeing.com>
Michael P. Broida wrote:

> Joe Hasting wrote:
>> 

 ...

> 	sub mysort
> 	{
> 		# Get key parts to avoid repetitive substr calls
> 		my $aSTART = substr($a,0,1);
> 		my $aEND = substr($a,1);
> 		my $bSTART = substr($b,0,1);
> 		my $bEND = substr($b,1);

You could reorder the comparisons to defer calculating $aEND and $bEND
(because they are needed only if $aSTART eq $bSTART). But a more complex
algorithm might be clearer coded this way.

 ...

> 	# Not pretty, but I think that will get the job done,
> 	# ASSUMING your hash keys are always ONE alpha char,
> 	# and always followed by a number with NOTHING after it.

If the data needs validating or cleaning up, I would usually do that
before sorting it.

 ...

-- 
Keith Anderson


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

Date: Wed, 04 Jun 2003 17:11:42 -0700
From: TruthXayer <TruthXayer@yahoo.com>
To: Joe Hasting <hasting@agere.com>
Subject: Re: SORT  sort hash with alpha-numeric keys
Message-Id: <3EDE8ABE.9215BC06@yahoo.com>

Joe Hasting wrote:
> 
> I've been through the FAQs and mauals, but cannot find info on sort using
> alphanumeric.
> 
> Let's use this has table:
> %hash = ( "L8" => "Left 8", "L9" => "Left 9", "L80" => "Left 80", "L90" =>
> "Left 90", "R8" => "Right 8", "R9" => "Right 9", "R80" => "Right 80", "R90"
> => "Right 90") ;
> 
> NOTE: these are coordinates,  I would like to be able to use L08 & L09 for
> example, but cannot.
> 
> I want the sort output to be  (sorting of hash keys or hash values would be
> fine):
> L8   Left 8
> L9   Left 9
> L80 Left 80
> L90 Left 90
> R8   Right 8
> R9   Right 9
> R80 Right 80
> R90 Right 90
> 


Try the following sort routine...its a more general one but
works great...


map { print "$_ $hash{$_}\n" } sort { by_num_bit($a,$b) }
keys %hash;

sub my_sort {
        my ($a,$b)=@_;
        my(@A) = ($a =~ /(\.|\d+|[^\.\d]+)/g);
        my(@B) = ($b =~ /(\.|\d+|[^\.\d]+)/g);
        my($A,$B);
        while(@A and @B) {
                $A=shift @A;
                $B=shift @B;
                if($A =~ /^\d+$/ and $B =~ /^\d+$/) {
                        return $A <=> $B if $A <=> $B; 
                } else {
                        $A = uc $A;
                        $B = uc $B;
                        return $A cmp $B if $A cmp $B; 
                }
        }
        @A <=> @B; 
}
 
sub by_num_bit {
        my($a,$b)=@_;
        my_sort($a,$b);
}


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

Date: Thu, 5 Jun 2003 00:22:39 GMT
From: "Michael P. Broida" <michael.p.broida@boeing.com>
Subject: Re: SORT  sort hash with alpha-numeric keys
Message-Id: <3EDE8D4F.14731E06@boeing.com>

Keith Anderson wrote:
> 
> Michael P. Broida wrote:
> 
> >       sub mysort
> >       {
> >               # Get key parts to avoid repetitive substr calls
> >               my $aSTART = substr($a,0,1);
> >               my $aEND = substr($a,1);
> >               my $bSTART = substr($b,0,1);
> >               my $bEND = substr($b,1);
> 
> You could reorder the comparisons to defer calculating $aEND and $bEND
> (because they are needed only if $aSTART eq $bSTART). But a more complex
> algorithm might be clearer coded this way.

	Good point.  Move those $aEND and $bEND calculations
	inside the appropriate "if" block so they don't happen
	if they are not needed.

		Mike


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

Date: Wed, 04 Jun 2003 21:35:18 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: strange perl behaviour with cgi
Message-Id: <qCtDa.44172$da1.10449@nwrddc03.gnilink.net>

Tassilo v. Parseval wrote:
> Also sprach Jürgen Exner:
>
>> Jes Naulen wrote:
>
>>>   #!/usr/bin/perl
>>>   print "Content-type: text/html\n\n";
>>>   print $test;
>>>
>>> and it starts counting {1,2,3...). When I open another browser and
>>> call the script, that brouwser starts counting from 1 again. Here
>>> too, obviously the variable $test is being remembered between
>>> browser sessions.
>>>
>>> What is wrong (with perl, with apache?) and what can I do about it?
>>
>> You never define any value for $test. That is very bad programming
>> style. Therefore perl is correct when it prints some random value.
>
> Perhaps from an educational standpoint correct. But it's not what
> actually happens. Querying an uninitialized value will result in undef
> which depending on the actual context (numerical or stringish) results
> in 0 or "". Perl wont ever return a 'random' value, unlike C for
> instance.

Well, in that case things don't fit together at all.
Based on your explanation the OPs program should always print the empty
string because there is no assignment to $test at all.
Then why is it being increment as the OP claims?

jue




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

Date: Thu, 5 Jun 2003 00:46:02 +0000 (UTC)
From: Vlad Tepes <minceme@start.no>
Subject: Re: use()ing a module by pasting it in code
Message-Id: <bbm3s9$876$1@troll.powertech.no>

> Alfonso <piz_dek@yahoo.com.au> wrote:
>
> I heard that use A is the same as pasting in the code
> of the module, but I can't get that to work.

What you've heard was not correct. Try

    perl -MCPAN -e 'install PAR'

instead.

-- 
                                                    (,_    ,_,    _,)   
                                                    /|\`\._( )_./'/|\   
                                                   · ·  \/ L /\ D  · ·  
                                                  /__|.-'`-\_/-`'-.|__\ 
                                                 `          "          `


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

Date: 4 Jun 2003 11:35:50 -0700
From: ksu@amaranthllc.com (Kevin Su)
Subject: Re: while loop
Message-Id: <5dd555c5.0306041035.6367dc07@posting.google.com>

Hey thanks for anwsering, yeah the body is a big string with new lines
already in it.  I'm going to give you split idea a shot.


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

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


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