[32993] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4269 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Aug 16 09:14:15 2014

Date: Sat, 16 Aug 2014 06:14: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           Sat, 16 Aug 2014     Volume: 11 Number: 4269

Today's topics:
    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>
    Re: Sorting Keys in Tie::IxHash::Easy <gravitalsun@hotmail.foo>
    Re: Sorting Keys in Tie::IxHash::Easy <gravitalsun@hotmail.foo>
    Re: Sorting Keys in Tie::IxHash::Easy <gravitalsun@hotmail.foo>
    Re: Sorting Keys in Tie::IxHash::Easy <hjp-usenet3@hjp.at>
    Re: Sorting Keys in Tie::IxHash::Easy <gravitalsun@hotmail.foo>
    Re: Sorting Keys in Tie::IxHash::Easy <hjp-usenet3@hjp.at>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 15 Aug 2014 15:22:43 +0300
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: Sorting Keys in Tie::IxHash::Easy
Message-Id: <lsku3p$22gu$1@news.ntua.gr>

# putting also the arrays in play , Something like Data::Dumper of the 
poor man.




use strict; use warnings;

my %data= (

k1b => 'v1b',
k1c => ['v11','v12',{D01=>'p01',D02=>'p02'}],
k1d => {
        k2a => 'v2a',
        k2c => ['p20','p22','p23',{deepH2k1=>'p2v1'}],
        k2d => {
               k3a => 'v3a',
               k3b => 'v3b',
               k3d => {
                      k4a => 'v4a',
                      k4b => 'v4b'
                      }
               },

        k2b => 'v2b'
        },

k1a => 'v1a'
);


DataWalker(\%data , \&DoSomeJob);

# KEY_UPPER_PATH_ARRAYREF, KEY_CURRENT , VALUE
sub DoSomeJob
{
my $upper = join '/', @{$_[0]}; $upper = '/' if $upper eq '';
my $key   = $_[1];
my $value = $_[2];
print "upper=$upper  key=$key ->  $value\n"
}





sub DataWalker
{
my ($data, $callback, $key_current, @key_upper) = @_;
@key_upper = '' unless @key_upper;


	if ('ARRAY' eq ref $data)
	{
		foreach (@{$data})
		{
			if ('HASH' eq ref $_)
			{
			push @key_upper, $key_current;
			DataWalker($_, $callback, $key_current, @key_upper);
			pop @key_upper
			}
			elsif ('ARRAY' eq ref $_)
			{			
			DataWalker($_, $callback, $key_current, @key_upper)
			}
			else
			{
			$callback->(\@key_upper, $key_current, $_)
			}
		}
   	}
   	elsif ('HASH' eq ref $data)
	{
		foreach (sort keys %{$data})
		{
			if ('HASH' eq ref $data->{$_})
			{
			push @key_upper, $_;
			DataWalker($data->{$_}, $callback, $_, @key_upper);
			pop @key_upper;
			}
			elsif ('ARRAY' eq ref $data->{$_})
			{
			
			DataWalker($data->{$_}, $callback, $_, @key_upper)
			}
			else
			{
			$callback->(\@key_upper, $_, $data->{$_})
			}
		}
	}
}


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

Date: Fri, 15 Aug 2014 16:54:26 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Sorting Keys in Tie::IxHash::Easy
Message-Id: <87wqa9sq8d.fsf@sable.mobileactivedefense.com>

George Mpouras <gravitalsun@hotmail.foo> writes:
> # putting also the arrays in play , Something like Data::Dumper of the
> poor man.

[...]

> use strict; use warnings;
>
> my %data= (
>
> k1b => 'v1b',
> k1c => ['v11','v12',{D01=>'p01',D02=>'p02'}],
> k1d => {
>        k2a => 'v2a',
>        k2c => ['p20','p22','p23',{deepH2k1=>'p2v1'}],
>        k2d => {
>               k3a => 'v3a',
>               k3b => 'v3b',
>               k3d => {
>                      k4a => 'v4a',
>                      k4b => 'v4b'
>                      }
>               },
>
>        k2b => 'v2b'
>        },
>
> k1a => 'v1a'
> );

[...]

> sub DataWalker
> {

[...]


> }

I could write various things in reply that but I will restrict myself to
this 'how recursion and advanced data structures make stuff easy'
demonstration (in the interest of clarity, this uses my variable more
generously then I'd usually use them):

-------------------------
my %data= (
k1b => 'v1b',
k1c => ['v11','v12',{D01=>'p01',D02=>'p02'}],
k1d => {
       k2a => 'v2a',
       k2c => ['p20','p22','p23',{deepH2k1=>'p2v1'}],
       k2d => {
              k3a => 'v3a',
              k3b => 'v3b',
              k3d => {
                     k4a => 'v4a',
                     k4b => 'v4b'
                     }
              },

       k2b => 'v2b'
       },

k1a => 'v1a'
);

my %walkers = (
	       ARRAY => \&walk_array,
	       HASH => \&walk_hash);

sub walk
{
    my ($it, $cb, @rest) = @_;
    my $action;

    $action = $walkers{ref($it)} || sub { $cb->($it, @rest); };
    $action->($it, $cb, @rest);
}

sub walk_hash
{
    my ($it, $cb, @rest) = @_;
    walk($it->{$_}, $cb, @rest, $_) for sort(keys(%$it));
}

sub walk_array
{
    my ($it, $cb, @rest) = @_;
    walk($it->[$_], $cb, @rest, $_) for 0 .. $#$it;
}


walk(\%data, sub {
	 my ($it, @rest) = @_;

	 printf('[%s] ', join('/', @rest)) if @rest;
	 print("$it\n");
     });


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

Date: Fri, 15 Aug 2014 20:16:25 +0300
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: Sorting Keys in Tie::IxHash::Easy
Message-Id: <lslfag$87t$1@news.ntua.gr>

On 15/8/2014 6:54 μμ, Rainer Weikusat wrote:
> my %data= (
> k1b => 'v1b',
> k1c => ['v11','v12',{D01=>'p01',D02=>'p02'}],
> k1d => {
>         k2a => 'v2a',
>         k2c => ['p20','p22','p23',{deepH2k1=>'p2v1'}],
>         k2d => {
>                k3a => 'v3a',
>                k3b => 'v3b',
>                k3d => {
>                       k4a => 'v4a',
>                       k4b => 'v4b'
>                       }
>                },
>
>         k2b => 'v2b'
>         },
>

This is good ! but something set confused at the results

[k1a] v1a
[k1b] v1b
	[k1c/0] v11
	[k1c/1] v12
	[k1c/2/D01] p01
	[k1c/2/D02] p02
[k1d/k2a] v2a
[k1d/k2b] v2b
[k1d/k2c/0] p20
[k1d/k2c/1] p22
[k1d/k2c/2] p23
[k1d/k2c/3/deepH2k1] p2v1
[k1d/k2d/k3a] v3a
[k1d/k2d/k3b] v3b
[k1d/k2d/k3d/k4a] v4a
[k1d/k2d/k3d/k4b] v4b

at first array there are the indexes as last keys but not at the second 
  [k1c/2/D01] p01


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

Date: Fri, 15 Aug 2014 21:01:26 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Sorting Keys in Tie::IxHash::Easy
Message-Id: <87fvgx8quh.fsf@sable.mobileactivedefense.com>

George Mpouras <gravitalsun@hotmail.foo> writes:

> On 15/8/2014 6:54 ¦Ì¦Ì, Rainer Weikusat wrote:
>> my %data= (
>> k1b => 'v1b',
>> k1c => ['v11','v12',{D01=>'p01',D02=>'p02'}],
>> k1d => {
>>         k2a => 'v2a',
>>         k2c => ['p20','p22','p23',{deepH2k1=>'p2v1'}],
>>         k2d => {
>>                k3a => 'v3a',
>>                k3b => 'v3b',
>>                k3d => {
>>                       k4a => 'v4a',
>>                       k4b => 'v4b'
>>                       }
>>                },
>>
>>         k2b => 'v2b'
>>         },
>>
>
> This is good ! but something set confused at the results
>
> [k1a] v1a
> [k1b] v1b
> 	[k1c/0] v11
> 	[k1c/1] v12
> 	[k1c/2/D01] p01
> 	[k1c/2/D02] p02
> [k1d/k2a] v2a
> [k1d/k2b] v2b
> [k1d/k2c/0] p20
> [k1d/k2c/1] p22
> [k1d/k2c/2] p23
> [k1d/k2c/3/deepH2k1] p2v1
> [k1d/k2d/k3a] v3a
> [k1d/k2d/k3b] v3b
> [k1d/k2d/k3d/k4a] v4a
> [k1d/k2d/k3d/k4b] v4b
>
> at first array there are the indexes as last keys but not at the
> second [k1c/2/D01] p01

I have to admit that I've no idea what you're talking about. AFAICT, the
output is as expected/ intended.

?


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

Date: Fri, 15 Aug 2014 23:26:33 +0300
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: Sorting Keys in Tie::IxHash::Easy
Message-Id: <lslqf1$116c$1@news.ntua.gr>

>
> I have to admit that I've no idea what you're talking about. AFAICT, the
> output is as expected/ intended.
>
> ?
>

[k1d/k2c/0] p20
[k1d/k2c/1] p22
[k1d/k2c/2] p23

At data there are not any 0 1 or 2 keys


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

Date: Fri, 15 Aug 2014 23:52:15 +0300
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: Sorting Keys in Tie::IxHash::Easy
Message-Id: <lslrv7$14r5$1@news.ntua.gr>

On 15/8/2014 11:26 ¦Ì¦Ì, George Mpouras wrote:
>>
>> I have to admit that I've no idea what you're talking about. AFAICT, the
>> output is as expected/ intended.
>>
>> ?
>>
>
> [k1d/k2c/0] p20
> [k1d/k2c/1] p22
> [k1d/k2c/2] p23
>
> At data there are not any 0 1 or 2 keys

I mean if you change the
walk($it->[$_], $cb, @rest, $_) for 0 .. $#$data
to
walk($it->[$_], $cb, @rest) for 0 .. $#$data
the result will be, which looks fine.

[k1a] v1a
[k1b] v1b
[k1c] v11
[k1c] v12
[k1c/D01] p01
[k1c/D02] p02
[k1d/k2a] v2a
[k1d/k2b] v2b
[k1d/k2c] p20
[k1d/k2c] p22
[k1d/k2c] p23
[k1d/k2c/deepH2k1] p2v1
[k1d/k2d/k3a] v3a
[k1d/k2d/k3b] v3b
[k1d/k2d/k3d/k4a] v4a
[k1d/k2d/k3d/k4b] v4b



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

Date: Sat, 16 Aug 2014 00:44:27 +0300
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: Sorting Keys in Tie::IxHash::Easy
Message-Id: <lslv13$1bsk$1@news.ntua.gr>

On 15/8/2014 11:26 ¦Ì¦Ì, George Mpouras wrote:

The following equivelant syntax is more familiar to me. It can written 
smaller but it become too much abstructed


walk(\%data, sub {
my ($data, @rest) = @_;
printf('[%s] ', join('/', @rest)) if @rest;
print("$data\n")
});

sub walk
{
my ($data, $cb) = (shift,shift);

	if ('ARRAY' eq ref $data)
	{	
	walk($data->[$_], $cb, @_) for 0 .. $#$data
	}
	elsif ('HASH' eq ref $data)
	{
	walk($data->{$_}, $cb, @_, $_) for sort keys %$data
	}
	else
	{
	$cb->($data, @_)
	}
}


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

Date: Sat, 16 Aug 2014 09:35:23 +0200
From: "Peter J. Holzer" <hjp-usenet3@hjp.at>
Subject: Re: Sorting Keys in Tie::IxHash::Easy
Message-Id: <slrnluu2dr.hl4.hjp-usenet3@hrunkner.hjp.at>

On 2014-08-15 20:52, George Mpouras <gravitalsun@hotmail.foo> wrote:
> On 15/8/2014 11:26 μμ, George Mpouras wrote:
>>> I have to admit that I've no idea what you're talking about. AFAICT, the
>>> output is as expected/ intended.
>>>
>>> ?
>>>
>>
>> [k1d/k2c/0] p20
>> [k1d/k2c/1] p22
>> [k1d/k2c/2] p23
>>
>> At data there are not any 0 1 or 2 keys
>
> I mean if you change the
> walk($it->[$_], $cb, @rest, $_) for 0 .. $#$data
> to
> walk($it->[$_], $cb, @rest) for 0 .. $#$data
> the result will be, which looks fine.
>
> [k1a] v1a
> [k1b] v1b
> [k1c] v11
> [k1c] v12

It doesn't look fine to me. You are missing the index here. It looks
like there are two elements $data->{k1c} instead of one $data->{k1c}[0]
and one $data->{k1c}[1].

> [k1c/D01] p01
> [k1c/D02] p02

And here the missing index makes it look like 'D01' and 'D02' are both
keys of the hash  $data->{k1c}  when they are in fact keys of
$data->{k1c}[2].

It gets worse if you have several nested arrays, like this:

my @data= (
    [1, 0, 0],
    [0, 1, 0],
    [0, 0, 1],
);

where your version prints:

1
0
0
0
1
0
0
0
1

which doesn't show us the shape of the matrix at all (is this a 6
element vector? A 2x3 matrix, a 3x2 matrix?) whereas Rainer's version
prints this:

[0/0] 1
[0/1] 0
[0/2] 0
[1/0] 0
[1/1] 1
[1/2] 0
[2/0] 0
[2/1] 0
[2/2] 1

which is very clear.

        hp


-- 
   _  | Peter J. Holzer    | Fluch der elektronischen Textverarbeitung:
|_|_) |                    | Man feilt solange an seinen Text um, bis
| |   | hjp@hjp.at         | die Satzbestandteile des Satzes nicht mehr
__/   | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel


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

Date: Sat, 16 Aug 2014 12:55:49 +0300
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: Sorting Keys in Tie::IxHash::Easy
Message-Id: <lsn9r5$f66$1@news.ntua.gr>


> which doesn't show us the shape of the matrix at all (is this a 6
> element vector? A 2x3 matrix, a 3x2 matrix?) whereas Rainer's version
> prints this:
>
> [0/0] 1
> [0/1] 0
> [0/2] 0
> [1/0] 0
> [1/1] 1
> [1/2] 0
> [2/0] 0
> [2/1] 0
> [2/2] 1
>
> which is very clear.
>
>          hp
>
>


The callback code do not kwow where the data actually came from, so the 
virtual data (indexes) may mislead the end user.
Consider the following example to make clear what I mean

	my %data1= ( key => ['a' , 'b' , 'c'] );
	my %data2= ( key => {0=>'a', 1=>'b', 2=>'c'} );

the Rainer's version prints the same output for both %data1 and %data2

	[key/0] a
	[key/1] b
	[key/2] c
	[key/0] a
	[key/1] b
	[key/2] c

the other version prints

	[key] a
	[key] b
	[key] c
	[key/0] a
	[key/1] b
	[key/2] c

So I think there is no "correct" version but its upon the problem of 
which approach should be used




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

Date: Sat, 16 Aug 2014 14:43:34 +0200
From: "Peter J. Holzer" <hjp-usenet3@hjp.at>
Subject: Re: Sorting Keys in Tie::IxHash::Easy
Message-Id: <slrnluukfm.7k2.hjp-usenet3@hrunkner.hjp.at>

On 2014-08-16 09:55, George Mpouras <gravitalsun@hotmail.foo> wrote:
>> which doesn't show us the shape of the matrix at all (is this a 6
>> element vector? A 2x3 matrix, a 3x2 matrix?) whereas Rainer's version
>> prints this:
>>
>> [0/0] 1
>> [0/1] 0
>> [0/2] 0
>> [1/0] 0
>> [1/1] 1
>> [1/2] 0
>> [2/0] 0
>> [2/1] 0
>> [2/2] 1
>
> The callback code do not kwow where the data actually came from, so the 
> virtual data (indexes) may mislead the end user.
> Consider the following example to make clear what I mean
>
> 	my %data1= ( key => ['a' , 'b' , 'c'] );
> 	my %data2= ( key => {0=>'a', 1=>'b', 2=>'c'} );
>
> the Rainer's version prints the same output for both %data1 and %data2
>
> 	[key/0] a
> 	[key/1] b
> 	[key/2] c
> 	[key/0] a
> 	[key/1] b
> 	[key/2] c

That omits the type (array or hash) but at least I can see that 'b' is
element 1 of some composite structure.


> the other version prints
>
> 	[key] a
> 	[key] b
> 	[key] c

Whereas here I don't see even that, I can only guess, and I might guess
wrong.

my %data1= ( key => ['a' , ['b', 'c']] }

would produce the same output.

> So I think there is no "correct" version but its upon the problem of 
> which approach should be used

Both versions omit some information, but yours omits more. So yours is
less correct than Rainer's.

        hp

PS: I won't expand further on this. Everybody else probably got it by
    now and I know from experience that arguing with you is pointless.

-- 
   _  | Peter J. Holzer    | Fluch der elektronischen Textverarbeitung:
|_|_) |                    | Man feilt solange an seinen Text um, bis
| |   | hjp@hjp.at         | die Satzbestandteile des Satzes nicht mehr
__/   | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel


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

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


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