Home > Archive > PERL Beginners > July 2005 > slices of hashes
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Gerard Robin 2005-07-24, 8:29 pm |
| Hello,
the slices of hashes work strangely ...
The parts 1, 2 and 4 work fine; but the part 3 doesn't do what I expect.
What is wrong in what I do in part 3 ?
#!/usr/bin/perl
#slice3.pl
use warnings;
use strict;
# 1
my %h1 = (a => 1, b => 2, c => 3, d => 4, e => 5, f => 6);
print sort {$a <=> $b} map {$h1{$_}." "} keys %h1;
print "\n";
print " ", map{ $_." " } @h1{'b'..'e'};
print "\n";
@h1{'b'..'e'} = (20, 30, 40, 50);
print " ", map{ $_." " } @h1{'b'..'e'};
print "\n";
print "="x13, "\n";
# 2
my %h3;
@h3{"Foo", "Bar", "Mamadoo", "Toto", "Lulu", "Momo"} = (1, 2, 3, 4, 5, 6);
print map {$_." "} values %h3;
print "\n";
print "~"x13, "\n";
# 3
@h3{"Mamadoo".."Lulu"} = (30, 40, 50 );
print map {$_." "} values %h3;
print "\n";
print "~"x13, "\n";
# 4
@h3{"Mamadoo","Lulu"} = (30, 50);
print map {$_." "} values %h3;
print "\n";
tia
PS
if I write:
@h3{"Mamadoo".."Lulu"} = (30, 40, 50 );
the script hangs (no error is displayed)
--
Gérard
| |
| Scott R. Godin 2005-07-24, 8:29 pm |
| Gerard Robin wrote:
> Hello,
> the slices of hashes work strangely ...
>
> PS
> if I write:
> @h3{"Mamadoo".."Lulu"} = (30, 40, 50 );
> the script hangs (no error is displayed)
>
yes because that's not a hash slice there:
what happens when you do these: ?
perl -wle 'print "Mamadoo","Lulu"'
perl -wle 'print "Mamadoo".."Lulu"'
perl -wle 'print "Lolo".."Lulu"'
should be a big hint as to where you went wrong. :-)
see perldoc perlop and the section on the Range Operators for further
details.
| |
| Jeff 'japhy' Pinyan 2005-07-24, 8:29 pm |
| On Jul 21, Gerard Robin said:
> my %h1 = (a => 1, b => 2, c => 3, d => 4, e => 5, f => 6);
[snip]
> print " ", map{ $_." " } @h1{'b'..'e'};
[snip]
> @h1{'b'..'e'} = (20, 30, 40, 50);
> print " ", map{ $_." " } @h1{'b'..'e'};
What do you think 'b'..'e' is producing? It's producing the list ('b',
'c', 'd', 'e'), but it has NOTHING to do with the hash. It appears to you
that it's returning a list the keys in the hash from 'b' to 'e', but
nothing could be further from the truth. Your hash keys aren't stored in
the order you placed them in the hash, anyway.
> @h3{"Mamadoo".."Lulu"} = (30, 40, 50 );
The list returned by "Mamadoo".."Lulu" is empty.
--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
| |
| Gerard Robin 2005-07-24, 8:29 pm |
| On Thu, Jul 21, 2005 at 04:04:36PM -0400 Scott R. Godin wrote:
> Gerard Robin wrote:
>
>
> yes because that's not a hash slice there:
>
> what happens when you do these: ?
>
> perl -wle 'print "Mamadoo","Lulu"'
> perl -wle 'print "Mamadoo".."Lulu"'
> perl -wle 'print "Lolo".."Lulu"'
>
> should be a big hint as to where you went wrong. :-)
I am very very wrong ;-)...:-)
in the PS I wanted to write "Lulu".."Mamadoo" and there is a lot of
results ...
Many thanks to you all.
--
Gérard
|
|
|
|
|