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

Locate missing number in a series

by htmanning (Pilgrim)
on Apr 21, 2016 at 18:50 UTC ( #1161138=perlquestion: print w/replies, xml ) Need Help??

htmanning has asked for the wisdom of the Perl Monks concerning the following question:

I have a database of condo units. Some of the units rent a storage locker, and their locker number is in their database record if they rent one. I'm trying to find out which lockers are not rented. It's easy for me to display the ones which are rented out because I just search the database for records whose locker field is not empty. There are 25 lockers total. If I have 80 database records and 24 have lockers, how do I find the locker number that is not already taken? Make sense? Thanks.

Replies are listed 'Best First'.
Re: Locate missing number in a series
by choroba (Archbishop) on Apr 21, 2016 at 19:31 UTC
    This is a DB question rather than a Perl one. Here's the answer:

    And here's a full demonstration:

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      Awesome! Thanks so much everyone who replied. This leads me in the right direction.
Re: Locate missing number in a series
by stevieb (Canon) on Apr 21, 2016 at 19:05 UTC

    You could put your entire locker list into an array, put the leased lockers that you harvested from the DB into a hash, then generate a new array for locker numbers that have not been leased.

    use warnings; use strict; my @lockers = (1..25); my %leased = map {$_ => 1} (1..12, 14..25); my @avail = grep {! defined $leased{$_}} @lockers; print "$_\n" for @avail; __END__ 13
Re: Locate missing number in a series
by Old_Gray_Bear (Bishop) on Apr 21, 2016 at 23:32 UTC

    You are looking for the disjunction between the set of all lockers and the set of rented lockers. So, in pseudo code is one way:

    • I have an array of lockers, 1 .. 24, initialized to zeroes
    • I have a database containing information
    • Loop: Read a Record from the DB
    • If the locker-id is present, set the value in my locker-array to 1.
    • Go to Loop: until you exhaust the DB.
    • scan the locker-array, if the value of the i-th element is 0, print the index (i).
    • Turn the result into your Teacher, you have completed the assignment in a FORTRANly fashion.

    I Go Back to Sleep, Now.


Re: Locate missing number in a series
by Discipulus (Abbot) on Apr 22, 2016 at 07:00 UTC
    difference of two arrays is a Perl Faq


    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Locate missing number in a series
by Anonymous Monk on Apr 21, 2016 at 19:32 UTC
    my %lockers = map { $_, 1 } 1..25; delete @lockers{ @leased }; print "available @{[ keys %lockers ]}\n";
Re: Locate missing number in a series
by Anonymous Monk on Apr 21, 2016 at 20:48 UTC
    #!/usr/bin/perl my $id = 0 x 25; my @leased = ( 17..25, 1..15 );# or however you set them, order not im +portant substr($id, $_ - 1, 1) = 1 for @leased; $id =~ /0/ and print "available $+[0]\n";

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1161138]
Approved by atcroft
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2021-10-16 10:00 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (69 votes). Check out past polls.