hashing for absent but not present cases

Discussion in 'Perl Misc' started by Marc Girod, Oct 14, 2010.

  1. Marc Girod

    Marc Girod Guest

    perldoc -f map
    perldoc perlop
    Look for 'range operator'

    Marc
     
    Marc Girod, Oct 14, 2010
    #1
    1. Advertisements

  2. 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
     
    Jürgen Exner, Oct 14, 2010
    #2
    1. Advertisements

  3. Marc Girod

    ela Guest

    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?
     
    ela, Oct 14, 2010
    #3
  4. Marc Girod

    sln Guest

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

    -sln
     
    sln, Oct 14, 2010
    #4
  5. Marc Girod

    ela Guest

    While your example has the key and value pair on the same row, mine is
    different and may span several rows, e.g.
    ....

    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, Oct 14, 2010
    #5
  6. Marc Girod

    ela Guest

    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, Oct 15, 2010
    #6
  7. Marc Girod

    ela Guest

    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, Oct 15, 2010
    #7
  8. Marc Girod

    ela Guest

    Thanks Tad McClellan & Marc Girod, after several rounds of trial & error, I
    understood the codes finally~
     
    ela, Oct 15, 2010
    #8
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.