[29747] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 991 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Oct 30 21:09:41 2007

Date: Tue, 30 Oct 2007 18:09:06 -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           Tue, 30 Oct 2007     Volume: 11 Number: 991

Today's topics:
        cpan module install woes - UTF-8 problem? <usenet-0710@piggo.com>
    Re: How to convert timestamp to epoch? <jl_post@hotmail.com>
    Re: How to convert timestamp to epoch? <void.no.spam.com@gmail.com>
    Re: How to convert timestamp to epoch? <kingskippus@gmail.com>
    Re: How to convert timestamp to epoch? usenet@DavidFilmer.com
    Re: Regex Help <rvtol+news@isolution.nl>
    Re: Regular Expression and Useage <inderpaul_s@yahoo.com>
    Re: Regular Expression and Useage <inderpaul_s@yahoo.com>
    Re: Regular Expression and Useage <inderpaul_s@yahoo.com>
    Re: Regular Expression and Useage <jimsgibson@gmail.com>
    Re: Regular Expression and Useage <inderpaul_s@yahoo.com>
        Sorting AofH over hash key(s)... <newsbot@cox.net>
    Re: Sorting AofH over hash key(s)... <simon.chao@fmr.com>
    Re: Sorting AofH over hash key(s)... <glex_no-spam@qwest-spam-no.invalid>
    Re: Sorting AofH over hash key(s)... <newsbot@cox.net>
    Re: Sorting AofH over hash key(s)... <wahab-mail@gmx.de>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 31 Oct 2007 00:46:28 +0000 (UTC)
From: Troy Piggins <usenet-0710@piggo.com>
Subject: cpan module install woes - UTF-8 problem?
Message-Id: <Xns99DA6D3ADDCB8usenet@85.214.62.108>

Not sure if this is the correct group.  Please let me know if there's a
more applicable one.

I'm trying to install amavisd-new, spamassassin, and clamav on my ubuntu
7.10 system.  One of amavisd-new's prerequisites is BerkeleyDB and there's
also an optional Mail::ClamAV that I want to install.  All other modules
installed fine.

Not being that knowledgeable with perl, I usually install modules at the
commandline by:

$ sudo cpan BerkeleyDB

type commands.  Trouble is for those 2 modules mentioned above, and also
some others required for spamassassin, I keep getting many errors[1].

I have read that there may be some problems with perl modules if the
environment is UTF-8.  Mine is.  I read that a solution may be to unset
LANG. Tried that with both:

$ env LANG= sudo cpan BerkeleyDB

and

$ LANG=
$ sudo cpan BerkeleyDB

to no avail.  Any ideas?  Please?


[1] Some of the output for BDB below.  I can provide full if necessary.
---------------------------------------------
CPAN: Storable loaded ok
Going to read /var/cache/cpan/Metadata
  Database was generated on Tue, 30 Oct 2007 12:36:36 GMT
Running install for module BerkeleyDB
Running make for P/PM/PMQS/BerkeleyDB-0.32.tar.gz
CPAN: Digest::MD5 loaded ok
CPAN: Compress::Zlib loaded ok
Checksum for
/var/cache/cpan/sources/authors/id/P/PM/PMQS/BerkeleyDB-0.32.tar.gz ok 
Scanning cache /var/cache/cpan/build for sizes BerkeleyDB-0.32/
BerkeleyDB-0.32/constants.
 ...
[snipped]
 ...
BerkeleyDB-0.32/Makefile.PL
Removing previously used /var/cache/cpan/build/BerkeleyDB-0.32

  CPAN.pm: Going to build P/PM/PMQS/BerkeleyDB-0.32.tar.gz

WARNING: LICENSE is not a known parameter.
Parsing config.in...
Looks Good.
Checking if your kit is complete...
Looks good
'LICENSE' is not a known MakeMaker parameter name.
Note (probably harmless): No library found for -ldb
Writing Makefile for BerkeleyDB
cp BerkeleyDB.pm blib/lib/BerkeleyDB.pm
AutoSplitting blib/lib/BerkeleyDB.pm (blib/lib/auto/BerkeleyDB)
cp BerkeleyDB/Hash.pm blib/lib/BerkeleyDB/Hash.pm
cp BerkeleyDB.pod blib/lib/BerkeleyDB.pod
cp BerkeleyDB/Btree.pm blib/lib/BerkeleyDB/Btree.pm
/usr/bin/perl /usr/share/perl/5.8/ExtUtils/xsubpp -noprototypes -typemap
/usr/share/perl/5.8/ExtUtils/typemap -typemap typemap  BerkeleyDB.xs >
BerkeleyDB.xsc && mv BerkeleyDB.xsc BerkeleyDB.c cc -c 
-I/usr/local/BerkeleyDB/include -D_REENTRANT -D_GNU_SOURCE
-DTHREADS_HAVE_PIDS -DDEBIAN -fno-strict-aliasing -pipe
-I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -O2  
-DVERSION=\"0.32\" -DXS_VERSION=\"0.32\" -fPIC "-I/usr/lib/perl/5.8/CORE" 
 BerkeleyDB.c BerkeleyDB.xs:68:16: error: db.h: No such file or directory
BerkeleyDB.xs:76:2: error: #error db.h is not for Berkeley DB at all.
BerkeleyDB.xs:211: error: expected specifier-qualifier-list before
'DB_ENV' BerkeleyDB.xs:222: error: expected specifier-qualifier-list
before 'DBTYPE' BerkeleyDB.xs:264: error: expected
specifier-qualifier-list before 'DBTYPE' BerkeleyDB.xs:306: error:
expected specifier-qualifier-list before 'DB_TXN' BerkeleyDB.xs:342:
error: expected '=', ',', ';', 'asm' or '__attribute__' before 'DBTKEY' 
 ... [snip]
 ...
BerkeleyDB.xs:4517: error: 'my_cxt_t' has no member named 'x_zero'
BerkeleyDB.xs:4518: error: 'my_cxt_t' has no member named 'x_empty'
BerkeleyDB.xs:4518: error: 'db_recno_t' undeclared (first use in this
function) BerkeleyDB.xs:4519: error: 'my_cxt_t' has no member named
'x_empty' make: *** [BerkeleyDB.o] Error 1
  /usr/bin/make  -- NOT OK
Running make test
  Can't test without successful make
Running make install
  make had returned bad status, install seems impossible
------------------------------------------------

-- 
Troy Piggins
using Xnews from work while getting new home connected


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

Date: Tue, 30 Oct 2007 13:16:41 -0700
From:  "jl_post@hotmail.com" <jl_post@hotmail.com>
Subject: Re: How to convert timestamp to epoch?
Message-Id: <1193775401.933927.9160@57g2000hsv.googlegroups.com>

On Oct 30, 1:27 pm, TonyV <kingskip...@gmail.com> wrote:
>
> use Time::Local;
>
> my $ts_to_convert = '22-jun-07 06.16.44.160000 PM';
> print timestamp_to_epoch($ts_to_convert);
>
> sub timestamp_to_epoch {
>   my ($ts) = @_;
>   my %month = (
>     'JAN' => 0, 'FEB' => 1, 'MAR' => 2, 'APR' => 3,
>     'MAY' => 4, 'JUN' => 5, 'JUL' => 6, 'AUG' => 7,
>     'SEP' => 8, 'OCT' => 9, 'NOV' => 10, 'DEC' => 11);
>   my $regex = '^(\d{1,2})-(\w{1,3})-(\d{1,2}) ';
>   $regex .= '(\d{1,2})\.(\d{1,2})\.(\d{1,2})';
>   $regex .= '\.\d+ ([AP])M$';
>   if ($ts =~ m{$regex}i) {
>     my ($dd, $mo, $yy, $hh, $mm, $ss, $ap) =
>       ($1, $2, $3, $4, $5, $6, $7);
>     $yy += 2000; $mo = $month{uc($mo)};
>     $hh += 12 if (uc($ap) eq 'P');
>     return timelocal($ss, $mm, $hh, $dd, $mo, $yy);
>   }
>   else { return 0; }
> }


   Ummm... this code has a subtle error in it:  it won't correctly
convert dates with "12" as the hour.  To see what I mean, try running
your code with this line:

      my $ts_to_convert = '22-JUN-07 12.16.44.160000 PM';

Instead of an epoch time value, you'll see an error message like:

      Hour '24' out of range 0..23

   Converting 12-hour AM/PM time to 24-hour time (and vice-versa) is a
little trickier than many people think.  They often forget to check
the cases where the hour equals 12, thinking that simply adding 12 to
the hour (if they are in PM) is enough to convert from 12-hour to 24-
hour time (like in the code you gave).

   But "12:00 am" needs to be converted to "00:00" and "12:00 pm"
needs to be converted to "12:00", and just adding 12 to the hour value
won't correctly convert either.



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

Date: Tue, 30 Oct 2007 14:42:16 -0700
From:  "void.no.spam.com@gmail.com" <void.no.spam.com@gmail.com>
Subject: Re: How to convert timestamp to epoch?
Message-Id: <1193780536.112535.22830@57g2000hsv.googlegroups.com>

Oops, actually this is the working version:

sub timestamp_to_epoch {
  my ($ts) = @_;
  my %month = (
    'JAN' => 0, 'FEB' => 1, 'MAR' => 2, 'APR' => 3,
    'MAY' => 4, 'JUN' => 5, 'JUL' => 6, 'AUG' => 7,
    'SEP' => 8, 'OCT' => 9, 'NOV' => 10, 'DEC' => 11);
  my $regex = '^(\d{1,2})-(\w{1,3})-(\d{1,2}) ';
  $regex .= '(\d{1,2})\.(\d{1,2})\.(\d{1,2})';
  $regex .= '\.\d+ ([AP])M$';
  if ($ts =~ m{$regex}i) {
    my ($dd, $mo, $yy, $hh, $mm, $ss, $ap) =
      ($1, $2, $3, $4, $5, $6, $7);
    $yy += 2000; $mo = $month{uc($mo)};
    if (uc($ap) eq 'P' && $hh != 12) {
      $hh += 12;
    }
    elsif (uc($ap) eq 'A' && $hh == 12) {
      $hh -= 12;
    }
    return timelocal($ss, $mm, $hh, $dd, $mo, $yy);
  }
  else { return 0; }
}



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

Date: Tue, 30 Oct 2007 14:55:44 -0700
From:  TonyV <kingskippus@gmail.com>
Subject: Re: How to convert timestamp to epoch?
Message-Id: <1193781344.386783.306480@o80g2000hse.googlegroups.com>

On Oct 30, 5:42 pm, "void.no.spam....@gmail.com"
<void.no.spam....@gmail.com> wrote:
> Oops, actually this is the working version:

Great catch, I usually deal exclusively with 24-hour time, which is
why I probably introduced that bug in.  I wish we could get rid of
that pesky AM/PM format entirely, even from our everyday, normal
lives.  :-P



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

Date: 30 Oct 2007 18:03:05 -0700
From: usenet@DavidFilmer.com
Subject: Re: How to convert timestamp to epoch?
Message-Id: <1193774426.307003.55910@q3g2000prf.googlegroups.com>

On Oct 30, 11:47 am, Paul Lalli <mri...@gmail.com> wrote:
> $date =~ s/\./:/ for 1..2;

That's a nifty little idiom that I would not have thought of (I would
have probably constructed some ugly regexp).  I'm gonna write that
down on my Perl cheat-sheet!

--
The best way to get a good answer is to ask a good question.
David Filmer (http://DavidFilmer.com)



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

Date: Tue, 30 Oct 2007 22:02:13 +0100
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: Regex Help
Message-Id: <fg89op.io.1@news.isolution.nl>

Dr.Ruud schreef:
> inderpaul_s:

>> I'm a newbie to perl expression more or less. What I want to do is to
>> use a regex pattern to find the presence of an IP address within a
>> file or string.
>
> Consider Regexp::Common, specifically $RE{net}{ipv4}.
> http://search.cpan.org/~abigail/Regexp-Common/lib/Regexp/Common/net.pm

s/ipv4/IPv4/

-- 
Affijn, Ruud

"Gewoon is een tijger."



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

Date: Tue, 30 Oct 2007 13:10:23 -0700
From:  "inderpaul_s@yahoo.com" <inderpaul_s@yahoo.com>
Subject: Re: Regular Expression and Useage
Message-Id: <1193775023.808315.19870@y27g2000pre.googlegroups.com>

On Oct 30, 11:31 am, TonyV <kingskip...@gmail.com> wrote:
> On Oct 30, 1:54 pm, "inderpau...@yahoo.com" <inderpau...@yahoo.com>
> wrote:
>
> > I'm somewhat new to regular expression and want to know how to extract
> > any strings which match an IP address.
>
> > I found this on the net and wanted to know if this is the most
> > efficient (easiest/shortest) way to write the expression or pattern to
> > match. Also in the discovered solution why do they use the \b word
> > boundary switch since the characters are of a numeric type ? I'm not
> > sure about this.
>
> > \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
>
> > Many thanks in advance.
>
> > Victor
>
> Please take a look at the answers to the exact same question you
> posted less than 24 hours ago in the thread titled, "Regex Help."
>
> --TV

Sorry to ask but if you can send me the URL to the post I would be
happy to read that. For some reason I have been unable to find my own
post either through my profile or just plain searching the NG where I
posted. Hence the reason for my posting again. I'm not sure what the
issue is.



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

Date: Tue, 30 Oct 2007 13:12:51 -0700
From:  "inderpaul_s@yahoo.com" <inderpaul_s@yahoo.com>
Subject: Re: Regular Expression and Useage
Message-Id: <1193775171.253555.160610@e9g2000prf.googlegroups.com>

On Oct 30, 11:41 am, Paul Lalli <mri...@gmail.com> wrote:
> On Oct 30, 1:54 pm, "inderpau...@yahoo.com" <inderpau...@yahoo.com>
> wrote:
>
> > I'm somewhat new to regular expression and want to know how to extract
> > any strings which match an IP address.
>
> > I found this on the net and wanted to know if this is the most
> > efficient (easiest/shortest) way to write the expression or
> > pattern to match.
>
> Shouldn't you first be concerned about whether it's the most *correct*
> before you worry about efficiency?
>
> > Also in the discovered solution why do they use the \b word
> > boundary switch since the characters are of a numeric type ?
> > I'm not sure about this.
>
> A "word" character in Perl is any letter, number, or underscore.
> Therefore, the \b prevents other numbers from being next to the IP
> address.  That is, it prevents 921128.0.0.123423 from matching.  Of
> course, it also prevents HOME128.0.0.1, which may or may not be what
> you want.
>
> > \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
>
> It could be shortened to:
> \b(?:\d{1,3}\.){3}\d{1,3}\b
>
> Or you could/should use Regexp::Common from CPAN and just write:
> /\b$RE{net}{IPv4}\b/
>
> Which not only is more easily readable, but also prevents such false-
> matches as 318.99.183.999.  That is, it takes care of checking the
> individual components' sizes for you.
>
> Paul Lalli


Thanks Paul for the help. I tested the above solution the one I had
found and discovered that it does not work for me. I'm not sure why.



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

Date: Tue, 30 Oct 2007 13:24:58 -0700
From:  "inderpaul_s@yahoo.com" <inderpaul_s@yahoo.com>
Subject: Re: Regular Expression and Useage
Message-Id: <1193775898.489424.50020@q5g2000prf.googlegroups.com>

On Oct 30, 11:41 am, Paul Lalli <mri...@gmail.com> wrote:
> On Oct 30, 1:54 pm, "inderpau...@yahoo.com" <inderpau...@yahoo.com>
> wrote:
>
> > I'm somewhat new to regular expression and want to know how to extract
> > any strings which match an IP address.
>
> > I found this on the net and wanted to know if this is the most
> > efficient (easiest/shortest) way to write the expression or
> > pattern to match.
>
> Shouldn't you first be concerned about whether it's the most *correct*
> before you worry about efficiency?
>
> > Also in the discovered solution why do they use the \b word
> > boundary switch since the characters are of a numeric type ?
> > I'm not sure about this.
>
> A "word" character in Perl is any letter, number, or underscore.
> Therefore, the \b prevents other numbers from being next to the IP
> address.  That is, it prevents 921128.0.0.123423 from matching.  Of
> course, it also prevents HOME128.0.0.1, which may or may not be what
> you want.
>
> > \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
>
> It could be shortened to:
> \b(?:\d{1,3}\.){3}\d{1,3}\b
>
> Or you could/should use Regexp::Common from CPAN and just write:
> /\b$RE{net}{IPv4}\b/
>
> Which not only is more easily readable, but also prevents such false-
> matches as 318.99.183.999.  That is, it takes care of checking the
> individual components' sizes for you.
>
> Paul Lalli

Paul does it matter if I'm using these Perl Regular Expressions in C+
+ ? Is the implementation of this RegEx engine not the same uner any
platform ? Its not working in C++.



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

Date: Tue, 30 Oct 2007 13:39:43 -0700
From: Jim Gibson <jimsgibson@gmail.com>
Subject: Re: Regular Expression and Useage
Message-Id: <301020071339439297%jimsgibson@gmail.com>

In article <1193775898.489424.50020@q5g2000prf.googlegroups.com>,
<"inderpaul_s@yahoo.com"> wrote:

> On Oct 30, 11:41 am, Paul Lalli <mri...@gmail.com> wrote:
> > On Oct 30, 1:54 pm, "inderpau...@yahoo.com" <inderpau...@yahoo.com>
> > wrote:
> >
> > > I'm somewhat new to regular expression and want to know how to extract
> > > any strings which match an IP address.
> >

[good advice from Paul snipped]

> 
> Paul does it matter if I'm using these Perl Regular Expressions in C+
> + ? Is the implementation of this RegEx engine not the same uner any
> platform ? Its not working in C++.
> 

Yes, it matters. Since this group is about the Perl language, everybody
who read your post assumed you were talking about the Perl regular
expression engine. Other regex engines will have their own
implementation and their own rules. You cannot assume that what is true
for Perl is also true for the regex engine you are using. You need to
read the documentation for the implementation you are using.

-- 
Jim Gibson

 Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------        
                http://www.usenet.com


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

Date: Tue, 30 Oct 2007 14:02:49 -0700
From:  "inderpaul_s@yahoo.com" <inderpaul_s@yahoo.com>
Subject: Re: Regular Expression and Useage
Message-Id: <1193778169.133967.174710@v23g2000prn.googlegroups.com>

On Oct 30, 12:39 pm, Jim Gibson <jimsgib...@gmail.com> wrote:
> In article <1193775898.489424.50...@q5g2000prf.googlegroups.com>,
>
> <"inderpau...@yahoo.com"> wrote:
> > On Oct 30, 11:41 am, Paul Lalli <mri...@gmail.com> wrote:
> > > On Oct 30, 1:54 pm, "inderpau...@yahoo.com" <inderpau...@yahoo.com>
> > > wrote:
>
> > > > I'm somewhat new to regular expression and want to know how to extract
> > > > any strings which match an IP address.
>
> [good advice from Paul snipped]
>
>
>
> > Paul does it matter if I'm using these Perl Regular Expressions in C+
> > + ? Is the implementation of this RegEx engine not the same uner any
> > platform ? Its not working in C++.
>
> Yes, it matters. Since this group is about the Perl language, everybody
> who read your post assumed you were talking about the Perl regular
> expression engine. Other regex engines will have their own
> implementation and their own rules. You cannot assume that what is true
> for Perl is also true for the regex engine you are using. You need to
> read the documentation for the implementation you are using.
>
> --
> Jim Gibson
>
>  Posted Via Usenet.com Premium Usenet Newsgroup Services
> ----------------------------------------------------------
>     ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
> ----------------------------------------------------------        
>                http://www.usenet.com

My apologies everyone I am newbie to programming including Perl.



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

Date: Tue, 30 Oct 2007 20:40:41 -0000
From:  "/usr/ceo" <newsbot@cox.net>
Subject: Sorting AofH over hash key(s)...
Message-Id: <1193776841.333955.104300@57g2000hsv.googlegroups.com>

I've searched this NG, the web, and CPAN and haven't seen anything to
do this, and while I have a solution, I'm wondering what other
solutions might be living inside some other pretty brilliant brain
cages here on c.l.p.m...

The problem seems fairly straight-forward: I want to be able to sort
an Array of Hashes by an arbitrary number of hash keys.  For instance,
given this hash:

my $h = [
   { color => 'red', size => 1, width => 640, height => 480 },
   { color => 'blue', size => 4, width => 800, height => 600 },
   { color => 'green', size => 2, width => 1024, height => 768 },
   { color => 'orange', size => 5, width => 320, height => 280 },
   { color => 'purple', size => 3, width => 40, height => 50 },
];

I'd like to sort by the keys: color, size, width, and/or height.  I
realize for only ONE hash key, I can do:

for my $info (sort { $a->{color} cmp $b->{color} } @{$h}) { blah() }
#...

But...  What if I want to sort by say... color and then size?  Or
height and then width?  Or all the keys in some order?  Again haven't
seen anything to do this.  It's hard to imagine this has never been
solved, but then again, I've done a fair amount of Perl coding in my
day and I haven't run across the need to do this until recently.

My solution?  Somewhat similar to a mod I saw on CPAN once that
flattened deep hashes into a dotted namespace.  It was a serialization
(or a namespace transformation, more like it):

$h->{norman}->{width}-{arm}->{finger}->{pinky} = 1.5;

became:

$h{norman.width.arm.finger.pinky} = 1.5;

Or something like that.  Anyway, my approach was to serialize the data
(not the keys) in the hashes in order of keys I wanted to sort on,
sort THAT array, and deserialize back in the right key order:

my $temp = [
   'red\xFF1\xFF640\xFF480',
   'blue\xFF4\xFF800\xFF600',
   'green'\xFF2\xFF1024\xFF768',
   'orange\xFF5\xFF320\xFF280',
   'purple\xFF\xFF3\xFF40\xFF50',
];

my @sortedArray = sort @{$temp};

## Then deserialize in correct order...

Also, my example, with a lot of numeric data is somewhat bad, I
realize.  I realize my solution doesn't sort numeric data properly
(due to ordinal values), but for the solution I needed it for,
immediately, the serialize -> sort -> deserialize method works for
now.  The numeric/ordinal issue is one of the reasons I am asking
simply from a purist standpoint.

Anyone got something different/better?  A mod to recommend that I am
not seeing on CPAN?

Thanks!
/usr/ceo



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

Date: Tue, 30 Oct 2007 21:03:12 -0000
From:  nolo contendere <simon.chao@fmr.com>
Subject: Re: Sorting AofH over hash key(s)...
Message-Id: <1193778192.264600.119380@o80g2000hse.googlegroups.com>

On Oct 30, 4:40 pm, "/usr/ceo" <news...@cox.net> wrote:
> I've searched this NG, the web, and CPAN and haven't seen anything to
> do this, and while I have a solution, I'm wondering what other
> solutions might be living inside some other pretty brilliant brain
> cages here on c.l.p.m...
>
> The problem seems fairly straight-forward: I want to be able to sort
> an Array of Hashes by an arbitrary number of hash keys.  For instance,
> given this hash:
>
> my $h = [
>    { color => 'red', size => 1, width => 640, height => 480 },
>    { color => 'blue', size => 4, width => 800, height => 600 },
>    { color => 'green', size => 2, width => 1024, height => 768 },
>    { color => 'orange', size => 5, width => 320, height => 280 },
>    { color => 'purple', size => 3, width => 40, height => 50 },
> ];
>
> I'd like to sort by the keys: color, size, width, and/or height.  I
> realize for only ONE hash key, I can do:
>
> for my $info (sort { $a->{color} cmp $b->{color} } @{$h}) { blah() }
> #...
>
> But...  What if I want to sort by say... color and then size?  Or
> height and then width?  Or all the keys in some order?  Again haven't
> seen anything to do this.  It's hard to imagine this has never been
> solved, but then again, I've done a fair amount of Perl coding in my
> day and I haven't run across the need to do this until recently.
>
> My solution?  Somewhat similar to a mod I saw on CPAN once that
> flattened deep hashes into a dotted namespace.  It was a serialization
> (or a namespace transformation, more like it):
>
> $h->{norman}->{width}-{arm}->{finger}->{pinky} = 1.5;
>
> became:
>
> $h{norman.width.arm.finger.pinky} = 1.5;
>
> Or something like that.  Anyway, my approach was to serialize the data
> (not the keys) in the hashes in order of keys I wanted to sort on,
> sort THAT array, and deserialize back in the right key order:
>
> my $temp = [
>    'red\xFF1\xFF640\xFF480',
>    'blue\xFF4\xFF800\xFF600',
>    'green'\xFF2\xFF1024\xFF768',
>    'orange\xFF5\xFF320\xFF280',
>    'purple\xFF\xFF3\xFF40\xFF50',
> ];
>
> my @sortedArray = sort @{$temp};
>
> ## Then deserialize in correct order...
>
> Also, my example, with a lot of numeric data is somewhat bad, I
> realize.  I realize my solution doesn't sort numeric data properly
> (due to ordinal values), but for the solution I needed it for,
> immediately, the serialize -> sort -> deserialize method works for
> now.  The numeric/ordinal issue is one of the reasons I am asking
> simply from a purist standpoint.

# from the Perl Cookbook

my @sorted =
  sort { $a->name cmp $b->name
                   ||
         $b->age <=> $a->age } @employees;




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

Date: Tue, 30 Oct 2007 16:14:22 -0500
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: Sorting AofH over hash key(s)...
Message-Id: <47279eaf$0$499$815e3792@news.qwest.net>

/usr/ceo wrote:
> I've searched this NG, the web, and CPAN and haven't seen anything to
> do this, and while I have a solution, I'm wondering what other
> solutions might be living inside some other pretty brilliant brain
> cages here on c.l.p.m...
> 
> The problem seems fairly straight-forward: I want to be able to sort
> an Array of Hashes by an arbitrary number of hash keys.  For instance,
> given this hash:
> 
> my $h = [
>    { color => 'red', size => 1, width => 640, height => 480 },
>    { color => 'blue', size => 4, width => 800, height => 600 },
>    { color => 'green', size => 2, width => 1024, height => 768 },
>    { color => 'orange', size => 5, width => 320, height => 280 },
>    { color => 'purple', size => 3, width => 40, height => 50 },
> ];
> 
> I'd like to sort by the keys: color, size, width, and/or height.  I
> realize for only ONE hash key, I can do:
> 
> for my $info (sort { $a->{color} cmp $b->{color} } @{$h}) { blah() }
> #...
> 
> But...  What if I want to sort by say... color and then size?  Or
> height and then width?  Or all the keys in some order?  

Just modify the sort to take into account the other keys.

sort {
	$a->{ 'color' } cmp $b->{ 'color' } ||
	$a->{ 'size' } <=> $b->{ 'size' }
}@{$h};

perldoc -q "How do I sort"


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

Date: Tue, 30 Oct 2007 21:16:10 -0000
From:  "/usr/ceo" <newsbot@cox.net>
Subject: Re: Sorting AofH over hash key(s)...
Message-Id: <1193778970.057157.184140@z9g2000hsf.googlegroups.com>

On Oct 30, 4:03 pm, nolo contendere <simon.c...@fmr.com> wrote:
> On Oct 30, 4:40 pm, "/usr/ceo" <news...@cox.net> wrote:
>
> [Problem description snipped]
>
> # from the Perl Cookbook
>
> my @sorted =
>   sort { $a->name cmp $b->name
>                    ||
>          $b->age <=> $a->age } @employees;

Dad-gummit, I *looked* in the PC, and didn't see this...  That syntax
makes perfect sense.  It never occurred to me to extended the { sort
$a->{key} cmp $b->{key} } syntax into something broader with logical
operators.  You get stuck in a rut with what you see as 90% used
sometimes.

NOW the solution is on the internet... :-)

Thanks!
/usr/ceo



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

Date: Tue, 30 Oct 2007 22:03:03 +0100
From: Mirco Wahab <wahab-mail@gmx.de>
Subject: Re: Sorting AofH over hash key(s)...
Message-Id: <fg87nc$4bh$1@mlucom4.urz.uni-halle.de>

/usr/ceo wrote:
> my $h = [
>    { color => 'red', size => 1, width => 640, height => 480 },
>    { color => 'blue', size => 4, width => 800, height => 600 },
>    { color => 'green', size => 2, width => 1024, height => 768 },
>    { color => 'orange', size => 5, width => 320, height => 280 },
>    { color => 'purple', size => 3, width => 40, height => 50 },
> ];
> 
> I'd like to sort by the keys: color, size, width, and/or height.  I
> realize for only ONE hash key, I can do:
> 
> for my $info (sort { $a->{color} cmp $b->{color} } @{$h}) { blah() }
> #...
> 
> But...  What if I want to sort by say... color and then size?  Or
> height and then width?  Or all the keys in some order?  Again haven't
> seen anything to do this.  It's hard to imagine this has never been
> solved, but then again, I've done a fair amount of Perl coding in my
> day and I haven't run across the need to do this until recently.

The (somehow) canonical solution would be a sorter function like:

  ...
  my @hs = sort mysortorder @$h;

  sub mysortorder {
      $a->{color}  cmp $b->{color}  ||
      $b->{width}  <=> $a->{width}  ||
      $b->{height} <=> $a->{height} ||
      $b->{size}   <=> $a->{size}
  }

  for my $s (@hs) {
      print "$_=>$s->{$_}, " for keys %$s;
      print "\n"
  }
  ...

Regards

M.


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

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.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

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 V11 Issue 991
**************************************


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