hashing for absent but not present cases

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

  1. Marc Girod

    Marc Girod Guest

    On Oct 14, 8:46 pm, "ela" <> wrote:

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


    perldoc -f map

    > c) 1..keys


    perldoc perlop
    Look for 'range operator'

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

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

  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

    Guest

    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
    , 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.

    >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, 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

    "Marc Girod" <> wrote in message
    news:...
    On Oct 14, 8:46 pm, "ela" <> 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, Oct 15, 2010
    #7
  8. Marc Girod

    ela Guest

    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~
    ela, Oct 15, 2010
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ralf Wahner
    Replies:
    5
    Views:
    614
    Bob Foster
    Dec 24, 2003
  2. Stanimir Stamenkov
    Replies:
    3
    Views:
    1,158
    Stanimir Stamenkov
    Apr 25, 2005
  3. Replies:
    0
    Views:
    312
  4. shaanxxx

    Waring for string is absent.

    shaanxxx, Aug 26, 2006, in forum: C Programming
    Replies:
    5
    Views:
    381
    Simon Biber
    Aug 26, 2006
  5. Guest
    Replies:
    0
    Views:
    339
    Guest
    Jul 9, 2007
Loading...

Share This Page