hashing for absent but not present cases

J

Jürgen Exner

ela said:
#my %absent = map {$_ => 1} 1..keys %countries;
my %absent = map {$_ => 1} 3..keys %countries;
so what does "3..keys" actually mean?

It means nothing because it is a context error: the function key() is
missing its operand.

However
3..keys %countries
means a list ranging from 3 to the number of elements in the hash
%countries because that is what the function keys() returns in scalar
context. see the second sentence in "perldoc -f keys".

jue
 
E

ela

if i have a table like:

ID
2
19
117
67
8

and information file like:

1
USA
2
China
3
Japan
4
England
....
100000
Australia

then I can first put the information file into hash and then retrieve the
country information later by the hashed ID. This is the "present" case that
can be solved easily. How can the other way, that is, those IDs that are
absent, to retrieve the coresponding "absent" country information?
 
S

sln

if i have a table like:

ID
2
19
117
67
8

and information file like:

1
USA
2
China
3
Japan
4
England
...
100000
Australia

then I can first put the information file into hash and then retrieve the
country information later by the hashed ID. This is the "present" case that
can be solved easily. How can the other way, that is, those IDs that are
absent, to retrieve the coresponding "absent" country information?

What about those IDs that don't exist? Orphan ids

-sln
 
E

ela

While your example has the key and value pair on the same row, mine is
different and may span several rows, e.g.
1 USA, MA, Boston
2 China,
Beijing,
Chaoyang
3
....

so I have to parse the information file first. However, I don't know how

my %absent = map {$_ => 1} 1..keys %countries;

works.

I'd appreciate if you would explain a little for the abc below because
perldoc map examples do not cover the combined concepts below:
a) map
b) {$_ => 1}
c) 1..keys

After knowing what they do, then I can produce my own scripts for different
file formats. Thanks again.
 
E

ela

I have modified the codes to test what they are for:

my @ids = qw/2 1 4/;
my %countries = qw(
3 Japan
1 USA
4 England
2 China
5 Australia
);

#my %absent = map {$_ => 1} 1..keys %countries;
my %absent = map {$_ => 1} 3..keys %countries;

foreach $key (sort(keys %absent)) {print $key, '=', $absent{$key},
"\n";}print "\n";
foreach $key (keys %absent) {print $key, '=', $absent{$key}, "\n";}
delete $absent{$_} for @ids;

print "missing countries:\n";
print "$_\n" for @countries{keys %absent};

and then the result is like this:

3=1
4=1
5=1

4=1
3=1
5=1
missing countries:
Japan
Australia

so while I was expecting only "Australia" is to print, so what does
"3..keys" actually mean?
 
E

ela

perldoc perlop
Look for 'range operator'

Marc

Thanks for your reference, again all the examples use @array but not %array
so I still don't know how 1..keys work out and why the printout result is
different from expected.
 
E

ela

Thanks Tad McClellan & Marc Girod, after several rounds of trial & error, I
understood the codes finally~
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top