Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Re: Finding missing elements in a sequence (code)

by clemburg (Curate)
on Nov 06, 2001 at 16:32 UTC ( [id://123629]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Re: Finding missing elements in a sequence (code)
in thread Finding missing elements in a sequence (code)

Ah, I see.

Well, you need to make things equal before comparing them ... you are putting numbers 1..n as keys into %isthere, but try to get out $isthere{"00001"}. That will not work - the keys in %isthere are "1" etc.

This is what you need to make it work:

$isthere{$_} = "yes" for map {$_+0} @list;

Christian Lemburg
Brainbench MVP for Perl
http://www.brainbench.com

Replies are listed 'Best First'.
Re: Re: Re: Finding missing elements in a sequence (code)
by deprecated (Priest) on Nov 06, 2001 at 16:38 UTC
    Wonderful! This works. Can you please go into a little more detail as to how this works? I am not sure how:
    "00010" + 0
    DWIM's.

    brother dep

    --
    Laziness, Impatience, Hubris, and Generosity.

      Adding 0 to the string "00001" forces the string to be interpreted as a number.

      In your code, when you say:

      my %isthere = map { $_ => 0 } ($low..$high);

      You force "00001" to be treated as a number, too. What *is* interesting is *why* "00001" is treated as a number, since there is also the possibility of doing "aaa" ... "zzz" and having it work, too. So why does "00001" end up being treated as "1" by the ".." operator? Probably the perl interpreter looks at your $low and $high and decides they look like numbers.

      BTW, you should not do this $low .. $high stuff - it can hang you badly if $low and $high end up to be interpreted as strings.

      At least use int($low) .. int($high), to force $low and $high to be interpreted as numbers.

      Christian Lemburg
      Brainbench MVP for Perl
      http://www.brainbench.com

Re: Re: Re: Finding missing elements in a sequence (code)
by pike (Monk) on Nov 06, 2001 at 16:47 UTC
    Or else you might change what you put into the hash:

    my %isthere = map { sprintf ('%05d',$_) => 0 } ($low..$high);
    Assuming that all numbers you want to check are in the same format...

    pike

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://123629]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.