Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   hashing for absent but not present cases (http://www.velocityreviews.com/forums/t913974-hashing-for-absent-but-not-present-cases.html)

Marc Girod 10-14-2010 01:00 PM

Re: hashing for absent but not present cases
 
On Oct 14, 8:46*pm, "ela" <e...@yantai.org> wrote:

> a) map
> b) {$_ => 1}


perldoc -f map

> c) 1..keys


perldoc perlop
Look for 'range operator'

Marc

Jürgen Exner 10-14-2010 02:12 PM

Re: hashing for absent but not present cases
 
"ela" <ela@yantai.org> wrote:
[...]
>#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

ela 10-14-2010 05:44 PM

hashing for absent but not present cases
 
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?



sln@netherlands.com 10-14-2010 06:53 PM

Re: hashing for absent but not present cases
 
On Thu, 14 Oct 2010 10:44:13 -0700, "ela" <ela@yantai.org> wrote:

>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

ela 10-14-2010 07:46 PM

Re: hashing for absent but not present cases
 
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.



ela 10-15-2010 04:30 AM

Re: hashing for absent but not present cases
 
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?



ela 10-15-2010 04:36 AM

Re: hashing for absent but not present cases
 

"Marc Girod" <marc.girod@gmail.com> wrote in message
news:e9de214e-10e8-47b6-bce7-71741ebf166c@v12g2000vbh.googlegroups.com...
On Oct 14, 8:46 pm, "ela" <e...@yantai.org> wrote:

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.



ela 10-15-2010 05:12 AM

Re: hashing for absent but not present cases (SOLVED)
 
Thanks Tad McClellan & Marc Girod, after several rounds of trial & error, I
understood the codes finally~




All times are GMT. The time now is 03:13 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.