Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > hashing for absent but not present cases

Reply
Thread Tools

hashing for absent but not present cases

 
 
Marc Girod
Guest
Posts: n/a
 
      10-14-2010
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
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      10-14-2010
"ela" <> 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
 
Reply With Quote
 
 
 
 
ela
Guest
Posts: n/a
 
      10-14-2010
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?


 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      10-14-2010
On Thu, 14 Oct 2010 10:44:13 -0700, "ela" <> 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
 
Reply With Quote
 
ela
Guest
Posts: n/a
 
      10-14-2010
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.


 
Reply With Quote
 
ela
Guest
Posts: n/a
 
      10-15-2010
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?


 
Reply With Quote
 
ela
Guest
Posts: n/a
 
      10-15-2010

"Marc Girod" <> wrote in message
news:e9de214e-10e8-47b6-bce7-...
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.


 
Reply With Quote
 
ela
Guest
Posts: n/a
 
      10-15-2010
Thanks Tad McClellan & Marc Girod, after several rounds of trial & error, I
understood the codes finally~


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
OS present but not present. XPD NZ Computing 4 04-11-2007 11:22 PM
Home server cases and external firewire cases thingy@nowhere.commy NZ Computing 5 03-14-2006 07:56 AM
[XML Schema] Including a schema document with absent target namespace to a schema with specified target namespace Stanimir Stamenkov XML 3 04-25-2005 09:59 AM
Absent DLL file Computer Support 7 06-14-2004 10:11 PM
Schema: express that "@a present if and only if @b present", where @a, @b are attributes Ralf Wahner XML 5 12-24-2003 11:37 AM



Advertisments