[31837] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 3100 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Aug 26 18:09:32 2010

Date: Thu, 26 Aug 2010 15:09:11 -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           Thu, 26 Aug 2010     Volume: 11 Number: 3100

Today's topics:
        complicated sort <monkey@joemoney.net>
    Re: complicated sort <m@rtij.nl.invlalid>
    Re: complicated sort <willem@turtle.stack.nl>
    Re: complicated sort <monkey@joemoney.net>
    Re: Help writing 'simple' loop Joey@invalid.net
    Re: Help writing 'simple' loop Joey@invalid.net
    Re: Help writing 'simple' loop sln@netherlands.com
    Re: Help writing 'simple' loop <tadmc@seesig.invalid>
    Re: Help writing 'simple' loop (Randal L. Schwartz)
    Re: Help writing 'simple' loop <cartercc@gmail.com>
    Re: Help writing 'simple' loop (Randal L. Schwartz)
    Re: Help writing 'simple' loop Joey@I.am.not.an.invalid.net
    Re: Help writing 'simple' loop <tadmc@seesig.invalid>
    Re: Help writing 'simple' loop (Randal L. Schwartz)
    Re: Help writing 'simple' loop <sherm.pendley@gmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Thu, 26 Aug 2010 16:39:36 -0400
From: monkeys paw <monkey@joemoney.net>
Subject: complicated sort
Message-Id: <BpCdndfhurGUTOvRnZ2dnUVZ_tudnZ2d@insightbb.com>

In the following array of hashrefs, i need to sort by state and sort 
tag. Problem i am having is that the records without sort tags should 
sort first (by state), then the rest with tags sort by state and then 
sort_tag. With the sort routine i have, it sorts the ones without 
sort_tags first, but it is divided by state. The correct sorting would be:

AK(no tag)
CA(no tag)
CO(no tag)
AK sorted by tag
CA sorted by tag

I can't get the records with no sort_tags to filter out without having 
the sort_tag override my state sort needs. Any ideas?


#!/bin/perl

use strict;

my @unsorted = (
{
                                   'state' => 'CA',
                                   'id_type' => 'ballot_measure',
                                   'id' => 'CA201011',
                                   'sort_tag' => 'Adam',
},
{
                                   'state' => 'CA',
                                   'id_type' => 'ballot_measure',
                                   'id' => 'CA201014',
                                   'sort_tag' => 'John',
},
{
                                   'state' => 'AK',
                                   'id' => 'AK2009000H19',
                                   'id_type' => 'bill',
},
{
                                   'state' => 'AK',
                                   'id' => 'AK2009000H45',
                                   'id_type' => 'bill',
                                   'sort_tag' => 'Tess',
},
{
                                   'state' => 'CA',
                                   'id' => 'CA2009000A3',
                                   'id_type' => 'bill',
},
{
                                   'state' => 'CA',
                                   'id_type' => 'exec_order',
                                   'id' => 'CA201026',
},
{
                                   'state' => 'CA',
                                   'id_type' => 'exec_order',
                                   'id' => 'CA201025',
                                   'sort_tag' => 'Adam',
},
{
                                   'state' => 'CO',
                                   'id' => 'CO2010000H1390',
                                   'id_type' => 'bill',
},
);

my @sorted = sort heading_sort @unsorted;

use Data::Dumper;die 'DDDEBUG' .  Dumper(\@sorted);

sub heading_sort {
   return ($a->{sort_tag} || 1) cmp ($b->{sort_tag} || 1)
      || $a->{state} cmp $b->{state};
}


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

Date: Thu, 26 Aug 2010 22:57:40 +0200
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: complicated sort
Message-Id: <431jk7-slo.ln1@news.rtij.nl>

On Thu, 26 Aug 2010 16:39:36 -0400, monkeys paw wrote:

> In the following array of hashrefs, i need to sort by state and sort
> tag. Problem i am having is that the records without sort tags should
> sort first (by state), then the rest with tags sort by state and then
> sort_tag. With the sort routine i have, it sorts the ones without
> sort_tags first, but it is divided by state. The correct sorting would
> be:
> 
> AK(no tag)
> CA(no tag)
> CO(no tag)
> AK sorted by tag
> CA sorted by tag
> 
> I can't get the records with no sort_tags to filter out without having
> the sort_tag override my state sort needs. Any ideas?
> 
(snip)
> 
> my @sorted = sort heading_sort @unsorted;

Something like:

my @untagged = sort { $a->{state} cmp $b->{state} }
	grep !exists $_->{sort_tag} @unsorted;
my @tagged = sort heading_sort 
	grep exists $_->{sort_tag} @unsorted;

my @sorted = (@untagged, @tagged);

HTH,
M4


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

Date: Thu, 26 Aug 2010 21:17:45 +0000 (UTC)
From: Willem <willem@turtle.stack.nl>
Subject: Re: complicated sort
Message-Id: <slrni7dmfp.pdu.willem@turtle.stack.nl>

monkeys paw wrote:
) In the following array of hashrefs, i need to sort by state and sort 
) tag. Problem i am having is that the records without sort tags should 
) sort first (by state), then the rest with tags sort by state and then 
) sort_tag. With the sort routine i have, it sorts the ones without 
) sort_tags first, but it is divided by state. The correct sorting would be:
)
) AK(no tag)
) CA(no tag)
) CO(no tag)
) AK sorted by tag
) CA sorted by tag

The easiest would be to split first and then sort separately.

After that, you can use the trick where you first add a key to each
record and then sort on the keys.  I forgot what the name is.

If you absolutely totally positively want a single sort step,
and you can't bother to crate a key first (which usually is faster
 than doing the work in the comparison function) then you need
something like:

 (exists $a->{tag} <=> exists $b->{tag})
 || ($a->{state} cmp $b->{state})
 || ((exists $a->{tag} && $a->{tag} cmp $b->{tag}))

Which is suitably obscure that it needs several lines of comments
if you want to use it in production code.


SaSW, Willem
-- 
Disclaimer: I am in no way responsible for any of the statements
            made in the above text. For all I know I might be
            drugged or something..
            No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT


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

Date: Thu, 26 Aug 2010 17:53:30 -0400
From: monkeys paw <monkey@joemoney.net>
Subject: Re: complicated sort
Message-Id: <i_adncRK0sHGf-vRnZ2dnUVZ_oudnZ2d@insightbb.com>

On 8/26/2010 4:57 PM, Martijn Lievaart wrote:
> On Thu, 26 Aug 2010 16:39:36 -0400, monkeys paw wrote:
>
>> In the following array of hashrefs, i need to sort by state and sort
>> tag. Problem i am having is that the records without sort tags should
>> sort first (by state), then the rest with tags sort by state and then
>> sort_tag. With the sort routine i have, it sorts the ones without
>> sort_tags first, but it is divided by state. The correct sorting would
>> be:
>>
>> AK(no tag)
>> CA(no tag)
>> CO(no tag)
>> AK sorted by tag
>> CA sorted by tag
>>
>> I can't get the records with no sort_tags to filter out without having
>> the sort_tag override my state sort needs. Any ideas?
>>
> (snip)
>>
>> my @sorted = sort heading_sort @unsorted;
>
> Something like:
>
> my @untagged = sort { $a->{state} cmp $b->{state} }
> 	grep !exists $_->{sort_tag} @unsorted;
> my @tagged = sort heading_sort
> 	grep exists $_->{sort_tag} @unsorted;
>
> my @sorted = (@untagged, @tagged);
>
> HTH,
> M4

Yes that works. I had considered that this must be the way, but not 
quite so elegantly. Thanks!


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

Date: Thu, 26 Aug 2010 11:12:48 -0700
From: Joey@invalid.net
Subject: Re: Help writing 'simple' loop
Message-Id: <7ibd76t7oa6h4hignoi6mt7to0fa69s3g9@4ax.com>

Ben Morrow wrote:

>
>Quoth JoeyF1276@earthlink.net:
>> Sherm Pendley wrote:
>> >JoeyF1276@earthlink.net writes:
>> >
>> >> $h1_right through $h10_right are 10 independent integer values. 
>> >
>> >That's your problem right there.
>> >
>> >> $hTotalRight = $h1_right + $h2_right + $h3_right + $h4_right + $h5_right +
>> >> $h6_right + $h7_right + $h8_right + $h9_right + $h10_right;
>> >
>> >When you find yourself using variable names like $h1, $h2, ... $hN,
>> >that's a VERY good sign you should be using an array @n instead.
>> >
>> >> I can't figure out the syntax for $XXX to write a loop to do the addition.
>> >> $hTotalRight = 0;
>> >> for ($i=1;$i<11;$i++) {
>> >> $hTotalRight = $hTotalRight + $XXX;
>> >> }
>> >
>> >  my $hTotalRight;
>> >  foreach my $h (@hRight) {
>> >    $hTotalRight += $h;
>> >  }
>> >
>> Thank you, but I don't understand how you are defining @hRight.
>
>Go through your program. Wherever you have $h1_right replace it with
>$hRight[0]. Wherever you have $h2_right replace it with $hRight[1]. And
>so on. Declare @hRight wherever it was you declared those $h*_right
>variables originally.
>
>Yes, this might be quite a bit of work, but that's what happens whn you
>make a fundamental mistake up-front. Any decent editor will be able to
>do the bulk of it for you.
>
Thanks...I reached the same conclusion.

Joey


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

Date: Thu, 26 Aug 2010 11:14:14 -0700
From: Joey@invalid.net
Subject: Re: Help writing 'simple' loop
Message-Id: <dlbd76hvgqll26s7selcj99k9edgn5dun6@4ax.com>

Randal L. Schwartz wrote:

>
>>>>>> "JoeyF1276" == JoeyF1276  <JoeyF1276@earthlink.net> writes:
>[some stuff]
>
>By the way, "Joey", it's not cool to impersonate an email address
>that doesn't belong to you (since it just bounced when I tried).
>
>If you want an invalid address, please use a domain of ".invalid", so
>that we don't try to start an email conversation with you.

Sorry 'bout that. Spam, etc., is my excuse.

Joey


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

Date: Thu, 26 Aug 2010 11:15:34 -0700
From: sln@netherlands.com
Subject: Re: Help writing 'simple' loop
Message-Id: <e1ad76lc6l0qvmpd7t33g78n5hebvulcrd@4ax.com>

On Thu, 26 Aug 2010 08:45:44 -0700, JoeyF1276@earthlink.net wrote:

>$h1_right through $h10_right are 10 independent integer values. 
>
>$hTotalRight = $h1_right + $h2_right + $h3_right + $h4_right + $h5_right +
>$h6_right + $h7_right + $h8_right + $h9_right + $h10_right;
>
>I can't figure out the syntax for $XXX to write a loop to do the addition.
>$hTotalRight = 0;
>for ($i=1;$i<11;$i++) {
>$hTotalRight = $hTotalRight + $XXX;
>}
>
>What is the syntax for $XXX?
>
>TIA,
>
>Joey

I guess there could be the littany of rehtorical questions.
You seem to know about variables but don't mention them.
You seem to know about a loop using variables but you never
it seems, used them.
You seem to want to sum $XXX, a variable within a loop, but
you don't know what it is.
You mention "syntax" but it is being used without reference to
any syntax that I can see.

So, from your description, code or sentences, there is no way
to know what you are trying to accomplish.

As far as I can tell, there is nothing wrong with your code
snippet, except you forgot to initialize variable $XXX.

  ## add this initialization:
  $XXX = 3; # or whatever
  ##
  $hTotalRight = 0;
  for ($i=1;$i<11;$i++) {
  $hTotalRight = $hTotalRight + $XXX;
  }

Actually, loop constructs are higher order concepts.
Did you really jump into a higher order concept without
knowing what data types are?
If not, then this is a troll question, if so, restart the
documentation LOOP with data types.


-sln


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

Date: Thu, 26 Aug 2010 13:24:24 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: Help writing 'simple' loop
Message-Id: <slrni7dc26.6po.tadmc@tadbox.sbcglobal.net>

JoeyF1276@earthlink.net <JoeyF1276@earthlink.net> wrote:
> Sherm Pendley wrote:
>
>>JoeyF1276@earthlink.net writes:
>>
>>> $h1_right through $h10_right are 10 independent integer values. 
>>
>>That's your problem right there.
>>
>>> $hTotalRight = $h1_right + $h2_right + $h3_right + $h4_right + $h5_right +
>>> $h6_right + $h7_right + $h8_right + $h9_right + $h10_right;
>>
>>When you find yourself using variable names like $h1, $h2, ... $hN,
>>that's a VERY good sign you should be using an array @n instead.


When you find yourself using variable names like $h1_right, $h2_right, ... $h10_right, 
that's a VERY good sign you should be using an array @hRight instead.


>>> I can't figure out the syntax for $XXX to write a loop to do the addition.
>>> $hTotalRight = 0;
>>> for ($i=1;$i<11;$i++) {
>>> $hTotalRight = $hTotalRight + $XXX;
>>> }
>>
>>  my $hTotalRight;
>>  foreach my $h (@hRight) {
>>    $hTotalRight += $h;
>>  }
>>
> Thank you, but I don't understand how you are defining @hRight.

my @hRight = ($h1_right, $h2_right, $h3_right, $h4_right, $h5_right,
              $h6_right, $h7_right, $h8_right, $h9_right, $h10_right);

Or, even better, instead of assigning to $h1_right assign to $hRight[0]
in the first place.


That is: you are using a cumbersome data structure ("independent" scalars).

Modify your program so that it uses a data structure more suited
to the structure of your data, namely an array.


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.


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

Date: Thu, 26 Aug 2010 11:44:46 -0700
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: Help writing 'simple' loop
Message-Id: <864oehxlgh.fsf@red.stonehenge.com>

>>>>> "Joey" == Joey  <Joey@invalid.net> writes:

Joey> Sorry 'bout that. Spam, etc., is my excuse.

No, now you're using "invalid.net", which happens to be registered
to someone in my hometown.

PLEASE PAY ATTENTION.

Use a TLD of ".invalid"... like "foo.invalid".

Since I can't email you, I have to chastise you publicly.

Again.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion


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

Date: Thu, 26 Aug 2010 12:17:16 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: Help writing 'simple' loop
Message-Id: <ffa78948-5a1b-4675-96ec-5627ab3af481@g17g2000yqe.googlegroups.com>

On Aug 26, 2:44=A0pm, mer...@stonehenge.com (Randal L. Schwartz) wrote:
> Since I can't email you, I have to chastise you publicly.

Psalm 6:1.

CC.


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

Date: Thu, 26 Aug 2010 12:35:39 -0700
From: merlyn@stonehenge.com (Randal L. Schwartz)
To: ccc31807 <cartercc@gmail.com>
Subject: Re: Help writing 'simple' loop
Message-Id: <86lj7tw4j8.fsf@red.stonehenge.com>

>>>>> "ccc31807" == ccc31807  <cartercc@gmail.com> writes:

ccc31807> On Aug 26, 2:44 pm, mer...@stonehenge.com (Randal L. Schwartz) wrote:
>> Since I can't email you, I have to chastise you publicly.

ccc31807> Psalm 6:1.

Not relevant.  Perhaps you missed that this is a man addressing his God
(starting with "O LORD").  I don't think the original poster has any
delusions that I am his God. :)

Thank you for playing, though.  Have a cookie, on me.

print "Just another Perl hacker,"; # the original

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion


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

Date: Thu, 26 Aug 2010 13:48:26 -0700
From: Joey@I.am.not.an.invalid.net
Subject: Re: Help writing 'simple' loop
Message-Id: <rhkd769bo0qmn2e79s44bhnnk9ttrv7qgb@4ax.com>

Randal L. Schwartz wrote:

>>>>>> "Joey" == Joey  <Joey@invalid.net> writes:
>
>Joey> Sorry 'bout that. Spam, etc., is my excuse.
>
>No, now you're using "invalid.net", which happens to be registered
>to someone in my hometown.
>
>PLEASE PAY ATTENTION.
>
>Use a TLD of ".invalid"... like "foo.invalid".
>
>Since I can't email you, I have to chastise you publicly.
>
>Again.

Sniffle...chastised by the guy whose book I'm reading. 

O, Randall, do not rebuke me in your anger or discipline me in your wrath.
A discount on your next edition will be adequate. :-)

Joey


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

Date: Thu, 26 Aug 2010 16:18:30 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: Help writing 'simple' loop
Message-Id: <slrni7dm8h.7cm.tadmc@tadbox.sbcglobal.net>

Joey@I.am.not.an.invalid.net <Joey@I.am.not.an.invalid.net> wrote:
> Randal L. Schwartz wrote:
  ^^^^^^
  ^^^^^^
>>>>>>> "Joey" == Joey  <Joey@invalid.net> writes:
>>
>>Joey> Sorry 'bout that. Spam, etc., is my excuse.
>>
>>No, now you're using "invalid.net", which happens to be registered
>>to someone in my hometown.
>>
>>PLEASE PAY ATTENTION.
>>
>>Use a TLD of ".invalid"... like "foo.invalid".
>>
>>Since I can't email you, I have to chastise you publicly.
>>
>>Again.
>
> Sniffle...chastised by the guy whose book I'm reading. 
>
> O, Randall, do not rebuke me in your anger or discipline me in your wrath.
     ^^^^^^^
     ^^^^^^^
> A discount on your next edition will be adequate. :-)


s/Randall/Randal/

or

s/Randall/Randal L/


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.


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

Date: Thu, 26 Aug 2010 14:28:59 -0700
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: Help writing 'simple' loop
Message-Id: <86r5hlukpw.fsf@red.stonehenge.com>

>>>>> "Joey" == Joey  <Joey@I.am.not.an.invalid.net> writes:
[snip]

AND AGAIN YOU ARE USING INVALID.NET

please stop.  Please... just stop.

Use "joey@joey.invalid".  ".invalid" as a TLD.  GET IT?  GET IT?

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion


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

Date: Thu, 26 Aug 2010 17:58:08 -0400
From: Sherm Pendley <sherm.pendley@gmail.com>
Subject: Re: Help writing 'simple' loop
Message-Id: <m2zkw9nij3.fsf@sherm.shermpendley.com>

Joey@I.am.not.an.invalid.net writes:

> Randal L. Schwartz wrote:
>
>>>>>>> "Joey" == Joey  <Joey@invalid.net> writes:
>>
>>Joey> Sorry 'bout that. Spam, etc., is my excuse.
>>
>>No, now you're using "invalid.net", which happens to be registered
>>to someone in my hometown.
>>
>>PLEASE PAY ATTENTION.
>>
>>Use a TLD of ".invalid"... like "foo.invalid".
>>
>>Since I can't email you, I have to chastise you publicly.
>>
>>Again.
>
> Sniffle...chastised by the guy whose book I'm reading. 

And yet, you're still not listening. Don't add ".net", ".com", or
".anything-else" at the end. ".invalid" is a top-level domain, and
as such it should appear at the end of any intentionally-invalid
address.

For example: Joey@I.am.not.an.invalid (note the lack of the .net
you're currently using)

Similarly, ".example" is reserved for use in example code. Both are
guaranteed to never be used for any real domains.

sherm--

-- 
Sherm Pendley                
										 <camelbones.sourceforge.net>
Cocoa Developer


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

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:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#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 3100
***************************************


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