[32989] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4265 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Aug 12 18:09:20 2014

Date: Tue, 12 Aug 2014 15:09:07 -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, 12 Aug 2014     Volume: 11 Number: 4265

Today's topics:
        capture backspace in perl script sreeeperll@gmail.com
    Re: capture backspace in perl script <jurgenex@hotmail.com>
    Re: capture backspace in perl script <rweikusat@mobileactivedefense.com>
    Re: capture backspace in perl script <news@todbe.com>
    Re: DISCUSSION: Lowering comp.lang.perl.misc volume mripodguy279@gmail.com
    Re: How can I point to different Perl compilers based o <rweikusat@mobileactivedefense.com>
    Re: How can I point to different Perl compilers based o sharma__r@hotmail.com
    Re: How can I point to different Perl compilers based o <rweikusat@mobileactivedefense.com>
        perl6 too much pointless functionality <gravitalsun@hotmail.foo>
    Re: perl6 too much pointless functionality <peter@makholm.net>
    Re: perl6 too much pointless functionality <gravitalsun@hotmail.foo>
    Re: perl6 too much pointless functionality <rweikusat@mobileactivedefense.com>
    Re: perl6 too much pointless functionality <justin.1401@purestblue.com>
        Sorting Keys in Tie::IxHash::Easy benswitala@gmail.com
    Re: Sorting Keys in Tie::IxHash::Easy <gravitalsun@hotmail.foo>
    Re: Sorting Keys in Tie::IxHash::Easy <rweikusat@mobileactivedefense.com>
    Re: Sorting Keys in Tie::IxHash::Easy <benswitala@gmail.com>
    Re: Sorting Keys in Tie::IxHash::Easy <benswitala@gmail.com>
    Re: Sorting Keys in Tie::IxHash::Easy <benswitala@gmail.com>
    Re: Sorting Keys in Tie::IxHash::Easy <gravitalsun@hotmail.foo>
    Re: Sorting Keys in Tie::IxHash::Easy <rweikusat@mobileactivedefense.com>
    Re: Sorting Keys in Tie::IxHash::Easy <gravitalsun@hotmail.foo>
    Re: Sorting Keys in Tie::IxHash::Easy <rweikusat@mobileactivedefense.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Mon, 11 Aug 2014 08:20:31 -0700 (PDT)
From: sreeeperll@gmail.com
Subject: capture backspace in perl script
Message-Id: <db4dbf21-c0a0-463d-a241-0df92323a3f8@googlegroups.com>

Hi,

I'm writing a perl program, where i need to accept the
input from the user, i also want to accept the backspace
and delete chars and take action appropriately. here's 
the program i already tried, but i still couldn't capture
the del and backspace keys. please share your ideas.

sub is_backsapce()
{
    if (($key =~ ['\b']) || ($key == 8)) {
        print "Entered backspace\n";
        return 1;
    }
    else {
        print "Entered non-backspace char\n";
        return 0;
    }
}



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

Date: Mon, 11 Aug 2014 09:08:37 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: capture backspace in perl script
Message-Id: <5bqhu9pvlfvfoiakmusd7veonplbrl675r@4ax.com>

sreeeperll@gmail.com wrote:
>I'm writing a perl program, where i need to accept the
>input from the user, i also want to accept the backspace
>and delete chars and take action appropriately.

How does the user enter the text? Are you really writing a raw input
device driver?
Virtually all methods I am familiar with will interpret backspace and
delete themself and therefore those 2 characters will never appear in
any text that is sent to whatever application processes the text in the
first place.

>sub is_backsapce()

Are you sure this is a good name?

jue


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

Date: Mon, 11 Aug 2014 17:16:05 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: capture backspace in perl script
Message-Id: <8761hzc88q.fsf@sable.mobileactivedefense.com>

sreeeperll@gmail.com writes:
> I'm writing a perl program, where i need to accept the
> input from the user, i also want to accept the backspace
> and delete chars and take action appropriately. here's 
> the program i already tried, but i still couldn't capture
> the del and backspace keys. please share your ideas.
>
> sub is_backsapce()
> {
>     if (($key =~ ['\b']) || ($key == 8)) {
>         print "Entered backspace\n";
>         return 1;
>     }
>     else {
>         print "Entered non-backspace char\n";
>         return 0;
>     }
> }

If you're on UNIX(*) or anything like that, 'a terminal' (whatever that
specifically mean) will usually operate in so-called 'canoncial mode'
where certain 'special keys' (like backspace in usual configuration) are
interpreted by the kernel terminal driver in order to enable some sort
of line editing. A program which wants to receive them itself needs to
change the terminal mode in a suitable way.




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

Date: Mon, 11 Aug 2014 14:53:49 -0700
From: "$Bill" <news@todbe.com>
Subject: Re: capture backspace in perl script
Message-Id: <lsbe1d$rap$1@dont-email.me>

On 8/11/2014 08:20, sreeeperll@gmail.com wrote:
> Hi,
>
> I'm writing a perl program, where i need to accept the
> input from the user, i also want to accept the backspace
> and delete chars and take action appropriately. here's
> the program i already tried, but i still couldn't capture
> the del and backspace keys. please share your ideas.
>
> sub is_backsapce()
> {
>      if (($key =~ ['\b']) || ($key == 8)) {
>          print "Entered backspace\n";
>          return 1;
>      }
>      else {
>          print "Entered non-backspace char\n";
>          return 0;
>      }
> }

sub is_backspace { my $ch = shift; return 1 if ord $ch == 8; return 0; }

I don't know how to get Del to work (I'm on Doze) - nothing is returned
from the read character/getc from the editing keys.

Ctrl-U (21) should cause to to delete your accumulated text and BS would
just chop the last char.

If the editing keys worked, you could keep a ptr into your accumulated
text string and have the edit keys (Home/End/Delete/LArrow/RArrow
move the ptr around in the string for edinting purposes.  Insert would
toggle the edit mode (overwrite/insert).

We have to use Term::Readkey/Readline on Doze - you could check them
out and see if they're any help on whatever you're on.

Doze example:

use Term::ReadKey;	END { ReadMode ('restore'); }	# just in case we bomb

binmode STDIN;		# handle CR->LF action
ReadMode ('raw');	# put term in raw mode

my $str = '';
while (my $ch = ReadKey ()) {
	last if $ch eq 'q' or $ch eq 'Q';	# use q to stop input
	if (ord $ch == 8) { print "Got BS\n"; chop $str; next; }	# BS
	if (ord $ch == 21) { print "Got ^U\n"; $str = ''; next; }	# ^U
	if (ord $ch == 13) { print "Got CR\n"; $str .= "\n"; next; }	# CR->LF
	$str .= $ch;
#	printf "\nch='%s' (0x%X %u)\n", $ch, ord $ch, ord $ch;
}
ReadMode ('restore');

# print edited string out in hex first
printf "\n%02X", ord $_ foreach split //, $str; printf "\n";
printf "\nstr='$str'\n\n";

__END__




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

Date: Mon, 11 Aug 2014 13:46:47 -0700 (PDT)
From: mripodguy279@gmail.com
Subject: Re: DISCUSSION: Lowering comp.lang.perl.misc volume
Message-Id: <18a3281b-8103-43d3-894a-7c5067384cf6@googlegroups.com>




This group is older than me


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

Date: Mon, 11 Aug 2014 17:42:09 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: How can I point to different Perl compilers based on OS?
Message-Id: <871tsnc71a.fsf@sable.mobileactivedefense.com>

Richard Nicholas <richardn@nospam.com> writes:
> In article <066548db-7b5a-43fe-b764-d2dfcb2d82c1@googlegroups.com>, sharma__r@hotmail.com 
> says...
>> I propose a different approach to your problem, which uses the
>> env variable OSTYPE to determine the OS.
>> 
>> And in case the env OSTYPE is not found,
>> then the "uname" command would do.
>> 
>> eval '
>> case ${OSTYPE-} in
>> "cygwin") perl="/usr/bin/cygwin64/perl";;
>> "linux") perl="/usr/bin/linux64/perl";;
>> ""|*) echo >&2 "Unknown OS:[${OSTYPE-}]"; exit 1;;
>> esac
>> exec "$perl" -w -S -- "$0" ${1+"$@"}
>> '
>> if 0;
>> 
>> ## your perl code follows underneath...
>> print "Hello, world!\n";
>> ....
>> 
>> Note: All this is in one file where your perl code resides.
>> 
>
> Pretty clever - a one script solution!  Thanks.

It is possible to do the same in a lot less clever/ relying on strange
shell hacks' way with Perl alone:

-------------
#!/usr/bin/perl

use POSIX qw(uname);

if ($ARGV[0] ne 'osified') {
    my %perls = (
		 Linux =>	'/usr/local/bin/perl',
		 AIX =>		'/usr/aix/bin/perl');

    my ($perl, $sys);

    $sys = (uname())[0];
    $perl = $perls{$sys};
    if ($perl) {
	exec($perl, $0, 'osified', @ARGV);
    } else {
	print STDERR ("found no perl for $sys --assuming everything's fine\n");
    }
} else {
    shift;
}

print("$0 ", join(', ', @ARGV), "\n");
--------------


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

Date: Mon, 11 Aug 2014 11:50:32 -0700 (PDT)
From: sharma__r@hotmail.com
Subject: Re: How can I point to different Perl compilers based on OS?
Message-Id: <5a2dfe4c-4026-431b-94f7-881a0a24919e@googlegroups.com>

On Sunday, August 10, 2014 10:15:20 PM UTC+5:30, Rainer Weikusat wrote:
> sharma__r@hotmail.com writes:
> 
> [select a perl based on OS]
> 
> > I propose a different approach to your problem, which uses the
> 
> > env variable OSTYPE to determine the OS.
> 
> > And in case the env OSTYPE is not found,
> 
> > then the "uname" command would do.
> 
> > eval '
> 
> > case ${OSTYPE-} in
> 
> > "cygwin") perl="/usr/bin/cygwin64/perl";;
> 
> > "linux") perl="/usr/bin/linux64/perl";;
> 
> > ""|*) echo >&2 "Unknown OS:[${OSTYPE-}]"; exit 1;;
> 
> > esac
> 
> > exec "$perl" -w -S -- "$0" ${1+"$@"}
> 
> > '
> 
> > if 0;
> 
> > ## your perl code follows underneath...
> 
> > print "Hello, world!\n";
> 
> > ....
> 
> OSTYPE is not an environment variable, it's a 'special variable'
> 
> supported by bash. Assuming I save this to a file, make it executable
> 
> and try to run it from an interactive bash, the result is
> 
> [rw@sable]/tmp#./a 
> 
> Unknown OS:[linux-gnu]
> 
> Doing the with dash yields
> 
> $ ./a
> 
> Unknown OS:[]
> 
> because that doesn't know anything about OSTYPE. Considering this, the
> 
> ${1+"$@} is useless voodoo, cf
> 
> http://www.in-ulm.de/~mascheck/various/bourne_args/


You didn't read fully what I wrote: we can use "uname" command in case OSTYPE is not available.

And the piece of code that I used is what I picked from "perldoc perlrun" under the "-S" switch.

The change that I injected in it was the case
statement that decides the perl binary location based on the OS & then feed that to the exec.

The case-esac was more in the YMMV mould, in that one should fill in the proper OS names based on the values of the OS names & their particular locations for the user.

So, in your ./a file you should do the following before the case statement.
OSTYPE=`uname -o`
and dont forget to plug in the various OS options/locations in the case-esac structure.

HTH

-Rakesh


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

Date: Mon, 11 Aug 2014 20:25:14 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: How can I point to different Perl compilers based on OS?
Message-Id: <87oavqbzhh.fsf@sable.mobileactivedefense.com>

sharma__r@hotmail.com writes:
> On Sunday, August 10, 2014 10:15:20 PM UTC+5:30, Rainer Weikusat wrote:
>> sharma__r@hotmail.com writes:
>> 
>> [select a perl based on OS]
>> 
>> > I propose a different approach to your problem, which uses the
>> 
>> > env variable OSTYPE to determine the OS.
>> 
>> > And in case the env OSTYPE is not found,
>> 
>> > then the "uname" command would do.
>> 
>> > eval '
>> 
>> > case ${OSTYPE-} in
>> 
>> > "cygwin") perl="/usr/bin/cygwin64/perl";;
>> 
>> > "linux") perl="/usr/bin/linux64/perl";;
>> 
>> > ""|*) echo >&2 "Unknown OS:[${OSTYPE-}]"; exit 1;;
>> 
>> > esac
>> 
>> > exec "$perl" -w -S -- "$0" ${1+"$@"}
>> 
>> > '
>> 
>> > if 0;
>> 
>> > ## your perl code follows underneath...
>> 
>> > print "Hello, world!\n";
>> 
>> > ....
>> 
>> OSTYPE is not an environment variable, it's a 'special variable'
>> 
>> supported by bash. Assuming I save this to a file, make it executable
>> 
>> and try to run it from an interactive bash, the result is
>> 
>> [rw@sable]/tmp#./a 
>> 
>> Unknown OS:[linux-gnu]
>> 
>> Doing the with dash yields
>> 
>> $ ./a
>> 
>> Unknown OS:[]
>> 
>> because that doesn't know anything about OSTYPE. Considering this, the
>> 
>> ${1+"$@} is useless voodoo, cf
>> 
>> http://www.in-ulm.de/~mascheck/various/bourne_args/
>
>
> You didn't read fully what I wrote: we can use "uname" command in case
> OSTYPE is not available.

That you seemingly didn't know that OSTYPE is not an environment
variable and additionally specific to bash doesn't lend itself to the
conclusion that I didn't read something I didn't comment on.

> And the piece of code that I used is what I picked from "perldoc
> perlrun" under the "-S" switch.

I already assumed you copied that from somewhere without understanding what it
means ...


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

Date: Tue, 12 Aug 2014 13:45:13 +0300
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: perl6 too much pointless functionality
Message-Id: <lscr6n$7ut$1@news.ntua.gr>

For example look here   http://doc.perl6.org/language/variables

     my $lexical   = 1;
     my $*dynamic1 = 10;
     my $*dynamic2 = 100;

     sub say-all() {
         say "$lexical, $*dynamic1, $*dynamic2";
     }

     # prints 1, 10, 100
     say-all();

     {
         my $lexical   = 2;
         my $*dynamic1 = 11;
         $*dynamic2    = 101;

         # prints 1, 11, 101
         say-all();
     }

     # prints 1, 10, 101
     say-all();


who asked them for all this __it ? Who needs all this pointless 
complexity ? Only those who develop this stuff in their nightmares.
Do they ask anyone ?

No thanks. If needed I prefer to jump in Python 3 than this .


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

Date: Tue, 12 Aug 2014 14:09:10 +0200
From: Peter Makholm <peter@makholm.net>
Subject: Re: perl6 too much pointless functionality
Message-Id: <87ha1h6hax.fsf@vps1.hacking.dk>

George Mpouras <gravitalsun@hotmail.foo> writes:

> For example look here   http://doc.perl6.org/language/variables

This isn't really different from perl5 except that 'my $*dynamic1' is
spelled 'local $dynamic1' in perl5.

//Makholm


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

Date: Tue, 12 Aug 2014 16:58:40 +0300
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: perl6 too much pointless functionality
Message-Id: <lsd6he$123h$1@news.ntua.gr>

On 12/8/2014 15:09, Peter Makholm wrote:
> George Mpouras <gravitalsun@hotmail.foo> writes:
>
>> For example look here   http://doc.perl6.org/language/variables
>
> This isn't really different from perl5 except that 'my $*dynamic1' is
> spelled 'local $dynamic1' in perl5.
>
> //Makholm
>



they should implement the virtual machine (which is something necessary) 
without touching the syntax of the language.
Only simple changes as needed.
They change almost everything, there is no point to that.
I mean if everything is changed, why someone keep following ? They do 
not learn the lesson of slow adoption of python 3 where the developers 
pissed off.




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

Date: Tue, 12 Aug 2014 15:36:07 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: perl6 too much pointless functionality
Message-Id: <877g2d4vxk.fsf@sable.mobileactivedefense.com>

George Mpouras <gravitalsun@hotmail.foo> writes:
> For example look here   http://doc.perl6.org/language/variables
>
>     my $lexical   = 1;
>     my $*dynamic1 = 10;
>     my $*dynamic2 = 100;
>
>     sub say-all() {
>         say "$lexical, $*dynamic1, $*dynamic2";
>     }
>
>     # prints 1, 10, 100
>     say-all();
>
>     {
>         my $lexical   = 2;
>         my $*dynamic1 = 11;
>         $*dynamic2    = 101;
>
>         # prints 1, 11, 101
>         say-all();
>     }
>
>     # prints 1, 10, 101
>     say-all();
>
>
> who asked them for all this __it ? Who needs all this pointless
> complexity ?

I don't see much 'pointless complexity' in this particular example, at
least insofar features go (as Peter Makholm already mentioned). But I
consider the Perl way of expressing this a lot less arcane. There, it
would be

our $dynamic;

to declare a 'special variable' (one residing in a symbol table),

local $dynamic = 1;

to create a localized/ block-scoped binding of a symbol table entry and

my $lexical;

to create a lexically scoped variable.

That's this happens to be different from established (Lisp) terminology
is not in itself something which has to be changed.

OTOH, perl6 is no serious contender, anyway, because it also uses
McCarthy's "don't care about that, can dump this on some student later
on" hack to work around limitations of the word size of an IBM 704
computer instead of a reasonable system for automatic resource
management, so "who friggin' cares"?


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

Date: Tue, 12 Aug 2014 16:21:10 +0100
From: Justin C <justin.1401@purestblue.com>
Subject: Re: perl6 too much pointless functionality
Message-Id: <60opbb-a6k.ln1@zem.masonsmusic.co.uk>

On 2014-08-12, George Mpouras <gravitalsun@hotmail.foo> wrote:
>
> they should implement the virtual machine (which is something necessary) 
> without touching the syntax of the language.
> Only simple changes as needed.
> They change almost everything, there is no point to that.
> I mean if everything is changed, why someone keep following ? They do 
> not learn the lesson of slow adoption of python 3 where the developers 
> pissed off.

When Perl 6 arrives Perl 5 will not be obsolete. They're so different
that 5 will go on for a very, very long time. There will be no need to
jump to 6 as soon as it arrives (if at all) - it's not like a security
patch to your OS.

   Justin.

-- 
Justin C, by the sea.


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

Date: Mon, 11 Aug 2014 16:55:08 -0700 (PDT)
From: benswitala@gmail.com
Subject: Sorting Keys in Tie::IxHash::Easy
Message-Id: <ccfb5b2b-29c8-4d08-89b1-a8d0bc49b54e@googlegroups.com>

Hi,

How can I sort the keys in a multi-dimensional hash using Tie::IxHash::Easy?  

It seems that SortByKey is what I want, but wouldn't that just sort the outer layer of keys?

Thanks,
Ben

http://search.cpan.org/~pinyan/Tie-IxHash-Easy-0.01/lib/Tie/IxHash/Easy.pm

http://search.cpan.org/~chorny/Tie-IxHash-1.23/lib/Tie/IxHash.pm#Standard_TIEHASH_Interface


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

Date: Tue, 12 Aug 2014 09:48:48 +0300
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: Sorting Keys in Tie::IxHash::Easy
Message-Id: <lscdbe$291e$1@news.ntua.gr>

why don't you do the obvious ; give a sample.
I never use these tie modules when I can do the task directly with one 
more line of code.


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

Date: Tue, 12 Aug 2014 15:24:20 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Sorting Keys in Tie::IxHash::Easy
Message-Id: <87bnrp4wh7.fsf@sable.mobileactivedefense.com>

benswitala@gmail.com writes:
> How can I sort the keys in a multi-dimensional hash using
> Tie::IxHash::Easy?

Perl doesn't support multi-dimensional containers. What are you using to
emulate them?



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

Date: Tue, 12 Aug 2014 09:48:34 -0700 (PDT)
From: Ben Switala <benswitala@gmail.com>
Subject: Re: Sorting Keys in Tie::IxHash::Easy
Message-Id: <22458da6-f135-4e47-8bbf-e9251bea5dc1@googlegroups.com>

Hi George,

One reason I didn't give a sample is that I don't really have one yet.  The farthest I've gotten was to do something like

use Tie::IxHash::Easy;
$tie_object = tie %bens_hash, 'Tie::IxHash::Easy';
#some more code
$tie_object->SortByKey;

but that didn't really do anything intelligible to me.  I think at that point I was dealing with a hash of hashes, which basically confused the meaning of "SortByKey" for me.

Today I'm simply trying to print out the hash of hashes that I have, and find out what various kinds of sorting (i.e. sorting the "inner keys" versus sorting the "outer keys" versus sorting both) would look like.

So, for right now, I'm taking your advice to accomplish this task directly and not using the module.

Thanks,
Ben

On Monday, August 11, 2014 11:48:48 PM UTC-7, George Mpouras wrote:
> why don't you do the obvious ; give a sample.
> 
> I never use these tie modules when I can do the task directly with one 
> 
> more line of code.



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

Date: Tue, 12 Aug 2014 09:50:51 -0700 (PDT)
From: Ben Switala <benswitala@gmail.com>
Subject: Re: Sorting Keys in Tie::IxHash::Easy
Message-Id: <9a15d994-f3bd-4382-ad8d-f118e28d1d03@googlegroups.com>

I meant to say, "I think at that point I realized I was dealing with a hash of hashes"

On Tuesday, August 12, 2014 9:48:34 AM UTC-7, Ben Switala wrote:
> Hi George,
> 
> 
> 
> One reason I didn't give a sample is that I don't really have one yet.  The farthest I've gotten was to do something like
> 
> 
> 
> use Tie::IxHash::Easy;
> 
> $tie_object = tie %bens_hash, 'Tie::IxHash::Easy';
> 
> #some more code
> 
> $tie_object->SortByKey;
> 
> 
> 
> but that didn't really do anything intelligible to me.  I think at that point I was dealing with a hash of hashes, which basically confused the meaning of "SortByKey" for me.
> 
> 
> 
> Today I'm simply trying to print out the hash of hashes that I have, and find out what various kinds of sorting (i.e. sorting the "inner keys" versus sorting the "outer keys" versus sorting both) would look like.
> 
> 
> 
> So, for right now, I'm taking your advice to accomplish this task directly and not using the module.
> 
> 
> 
> Thanks,
> 
> Ben
> 
> 
> 
> On Monday, August 11, 2014 11:48:48 PM UTC-7, George Mpouras wrote:
> 
> > why don't you do the obvious ; give a sample.
> 
> > 
> 
> > I never use these tie modules when I can do the task directly with one 
> 
> > 
> 
> > more line of code.



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

Date: Tue, 12 Aug 2014 09:53:28 -0700 (PDT)
From: Ben Switala <benswitala@gmail.com>
Subject: Re: Sorting Keys in Tie::IxHash::Easy
Message-Id: <fe8d9955-d303-45ee-8876-93eb60106385@googlegroups.com>

Hi Rainer,

My code has variables that are referenced like, 

$bens_hash{$key1}{$key2}

Then I found this documentation page:
http://perldoc.perl.org/perldsc.html#HASHES-OF-HASHES

That's what I mean when I said "multi-dimensional hash"; I meant "hash of hashes".  Sorry for the confusion.


Ben

On Tuesday, August 12, 2014 7:24:20 AM UTC-7, Rainer Weikusat wrote:
> ben writes:
> 
> > How can I sort the keys in a multi-dimensional hash using
> 
> > Tie::IxHash::Easy?
> 
> 
> 
> Perl doesn't support multi-dimensional containers. What are you using to
> 
> emulate them?



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

Date: Tue, 12 Aug 2014 23:05:46 +0300
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: Sorting Keys in Tie::IxHash::Easy
Message-Id: <lsds3r$2jfp$1@news.ntua.gr>

>>
>>
>> accomplish this task directly
>>

# An example that may help


use strict; use warnings; use feature 'say';

my %hash = (
'A1' => { c1 => 3, c2 => 2, c0 => 1 },
'A2' => { a2 => 1, a0 => 2, a1 => 3 },
'A0' => { b1 => 3, b0 => 2, b2 => 1 } );

# sort by keys    desc  AND
# sort by subkeys asc   AND
foreach my $key (sort {$b cmp $a} keys %hash)
{
     foreach my $subkey (sort {$a cmp $b} keys %{$hash{$key}})
     {
     say "$key , $subkey , $hash{$key}->{$subkey}"
     }
}

say '--------------';

# sort by keys      asc AND
# sort by subvalues asc
foreach my $key (sort {$a cmp $b} keys %hash)
{
     foreach my $subkey (sort {$hash{$key}->{$b} cmp $hash{$key}->{$a}} 
keys %{$hash{$key}})
     {
     say "$key , $subkey , $hash{$key}->{$subkey}"
     }
}



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

Date: Tue, 12 Aug 2014 21:40:09 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Sorting Keys in Tie::IxHash::Easy
Message-Id: <87wqad30ie.fsf@sable.mobileactivedefense.com>

George Mpouras <gravitalsun@hotmail.foo> writes:

[sort a hash]

>>>
>>>
>>> accomplish this task directly
>>>
>
> # An example that may help
>
>
> use strict; use warnings; use feature 'say';
>
> my %hash = (
> 'A1' => { c1 => 3, c2 => 2, c0 => 1 },
> 'A2' => { a2 => 1, a0 => 2, a1 => 3 },
> 'A0' => { b1 => 3, b0 => 2, b2 => 1 } );
>
> # sort by keys    desc  AND
> # sort by subkeys asc   AND
> foreach my $key (sort {$b cmp $a} keys %hash)
> {
>     foreach my $subkey (sort {$a cmp $b} keys %{$hash{$key}})
>     {
>     say "$key , $subkey , $hash{$key}->{$subkey}"
>     }
> }
>
> say '--------------';
>
> # sort by keys      asc AND
> # sort by subvalues asc
> foreach my $key (sort {$a cmp $b} keys %hash)
> {
>     foreach my $subkey (sort {$hash{$key}->{$b} cmp $hash{$key}->{$a}}
> keys %{$hash{$key}})
>     {
>     say "$key , $subkey , $hash{$key}->{$subkey}"
>     }
> }

This doesn't really 'sort a hash of hashes', it performs a specific
operation on all key/value pairs which are members of an anonymous hash
which itself a member of a 'top-level' hash. Assuming that it is OK to
restrict the operation to hashes, ie, without dealing with hash-based
objects (anonymous hashes blessed into some class) and hash-like objects
(instances of a classes overloading %{}), a generalization of that could
look like this:

----------------
my %h = (
	 c => {qw(c 3 b 2 a 1)},
	 b => {qw(f 3 e 2 d 1)},
	 a => {
	       m => {qw(p 3 o 2 n 1)},
	       l => {qw(t 3 s 2 r 1)}}
	);

sub traverse_sorted
{
    my ($h, $cb, @ks) = @_;
    my $v;

    for (sort(keys(%$h))) {
	$v = $h->{$_};
	traverse_sorted($v, $cb, @ks, $_), next if ref($v) eq 'HASH';
	$cb->($v, @ks, $_);
    }
}

sub path_print
{
    my ($v, @ks) = @_;

    print(join(',', @ks), "\t$v\n");
}

traverse_sorted(\%h, \&path_print);


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

Date: Tue, 12 Aug 2014 23:49:14 +0300
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: Sorting Keys in Tie::IxHash::Easy
Message-Id: <lsdulc$2ovc$1@news.ntua.gr>

On 12/8/2014 11:40 μμ, Rainer Weikusat wrote:
> George Mpouras <gravitalsun@hotmail.foo> writes:
>
> [sort a hash]
>
>>>>
>>>>
>>>> accomplish this task directly
>>>>
>>
>> # An example that may help
>>
>>
>> use strict; use warnings; use feature 'say';
>>
>> my %hash = (
>> 'A1' => { c1 => 3, c2 => 2, c0 => 1 },
>> 'A2' => { a2 => 1, a0 => 2, a1 => 3 },
>> 'A0' => { b1 => 3, b0 => 2, b2 => 1 } );
>>
>> # sort by keys    desc  AND
>> # sort by subkeys asc   AND
>> foreach my $key (sort {$b cmp $a} keys %hash)
>> {
>>      foreach my $subkey (sort {$a cmp $b} keys %{$hash{$key}})
>>      {
>>      say "$key , $subkey , $hash{$key}->{$subkey}"
>>      }
>> }
>>
>> say '--------------';
>>
>> # sort by keys      asc AND
>> # sort by subvalues asc
>> foreach my $key (sort {$a cmp $b} keys %hash)
>> {
>>      foreach my $subkey (sort {$hash{$key}->{$b} cmp $hash{$key}->{$a}}
>> keys %{$hash{$key}})
>>      {
>>      say "$key , $subkey , $hash{$key}->{$subkey}"
>>      }
>> }
>
> This doesn't really 'sort a hash of hashes', it performs a specific
> operation on all key/value pairs which are members of an anonymous hash
> which itself a member of a 'top-level' hash. Assuming that it is OK to
> restrict the operation to hashes, ie, without dealing with hash-based
> objects (anonymous hashes blessed into some class) and hash-like objects
> (instances of a classes overloading %{}), a generalization of that could
> look like this:
>
> ----------------
> my %h = (
> 	 c => {qw(c 3 b 2 a 1)},
> 	 b => {qw(f 3 e 2 d 1)},
> 	 a => {
> 	       m => {qw(p 3 o 2 n 1)},
> 	       l => {qw(t 3 s 2 r 1)}}
> 	);
>
> sub traverse_sorted
> {
>      my ($h, $cb, @ks) = @_;
>      my $v;
>
>      for (sort(keys(%$h))) {
> 	$v = $h->{$_};
> 	traverse_sorted($v, $cb, @ks, $_), next if ref($v) eq 'HASH';
> 	$cb->($v, @ks, $_);
>      }
> }
>
> sub path_print
> {
>      my ($v, @ks) = @_;
>
>      print(join(',', @ks), "\t$v\n");
> }
>
> traverse_sorted(\%h, \&path_print);
>



if you want to follow this path, then do it correctly using the 
recursive data walker

use Data::Rmap;
 ...


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

Date: Tue, 12 Aug 2014 22:00:53 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Sorting Keys in Tie::IxHash::Easy
Message-Id: <87sil12zju.fsf@sable.mobileactivedefense.com>

George Mpouras <gravitalsun@hotmail.foo> writes:
> On 12/8/2014 11:40 ¦Ì¦Ì, Rainer Weikusat wrote:
>> George Mpouras <gravitalsun@hotmail.foo> writes:
>>
>> [sort a hash]
>>
>>>>>
>>>>>
>>>>> accomplish this task directly
>>>>>
>>>
>>> # An example that may help
>>>
>>>
>>> use strict; use warnings; use feature 'say';
>>>
>>> my %hash = (
>>> 'A1' => { c1 => 3, c2 => 2, c0 => 1 },
>>> 'A2' => { a2 => 1, a0 => 2, a1 => 3 },
>>> 'A0' => { b1 => 3, b0 => 2, b2 => 1 } );
>>>
>>> # sort by keys    desc  AND
>>> # sort by subkeys asc   AND
>>> foreach my $key (sort {$b cmp $a} keys %hash)
>>> {
>>>      foreach my $subkey (sort {$a cmp $b} keys %{$hash{$key}})
>>>      {
>>>      say "$key , $subkey , $hash{$key}->{$subkey}"
>>>      }
>>> }
>>>
>>> say '--------------';
>>>
>>> # sort by keys      asc AND
>>> # sort by subvalues asc
>>> foreach my $key (sort {$a cmp $b} keys %hash)
>>> {
>>>      foreach my $subkey (sort {$hash{$key}->{$b} cmp $hash{$key}->{$a}}
>>> keys %{$hash{$key}})
>>>      {
>>>      say "$key , $subkey , $hash{$key}->{$subkey}"
>>>      }
>>> }
>>
>> This doesn't really 'sort a hash of hashes', it performs a specific
>> operation on all key/value pairs which are members of an anonymous hash
>> which itself a member of a 'top-level' hash. Assuming that it is OK to
>> restrict the operation to hashes, ie, without dealing with hash-based
>> objects (anonymous hashes blessed into some class) and hash-like objects
>> (instances of a classes overloading %{}), a generalization of that could
>> look like this:
>>
>> ----------------
>> my %h = (
>> 	 c => {qw(c 3 b 2 a 1)},
>> 	 b => {qw(f 3 e 2 d 1)},
>> 	 a => {
>> 	       m => {qw(p 3 o 2 n 1)},
>> 	       l => {qw(t 3 s 2 r 1)}}
>> 	);
>>
>> sub traverse_sorted
>> {
>>      my ($h, $cb, @ks) = @_;
>>      my $v;
>>
>>      for (sort(keys(%$h))) {
>> 	$v = $h->{$_};
>> 	traverse_sorted($v, $cb, @ks, $_), next if ref($v) eq 'HASH';
>> 	$cb->($v, @ks, $_);
>>      }
>> }
>>
>> sub path_print
>> {
>>      my ($v, @ks) = @_;
>>
>>      print(join(',', @ks), "\t$v\n");
>> }
>>
>> traverse_sorted(\%h, \&path_print);
>>
>
>
>
> if you want to follow this path, then do it correctly using the
> recursive data walker
>
> use Data::Rmap;
> ...

'Correct' actually has a formal definition[*] and that's not 'includes
every conceivable feature which is at least loosely/ theoretically'
related to a given problem. [**]

[**] Data::Rmap[***] is supposed to produce a list of values. The code above
isn't.

[***] And who told you that J. Random Abandonware would be correct just
because J. Random Dontcareaboutthatanymore dumped it (and his very
important name) on a publically accessible web server?

[*] As translation from German (which might or might not make sense in
English), this would be "code is said to be correct if the
postconditions will be true after execution provided the preconditions
were true before execution started and the invariant conditions remained
true during execution". It follows that every given code can be 'proven
correct' or 'proven incorrect' by inventing suitable condition sets.


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

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


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