{OT} Perl Question

Discussion in 'UK Motorcycles' started by Desmond Coughlan, Sep 29, 2003.

  1. Ginge wrote
    Blockade Management?
     
    steve auvache, Sep 29, 2003
    #21
    1. Advertisements

  2. Desmond Coughlan

    Simian Guest

    Verdigris :
    A friend of mine once wrote a version of The Game Of Life, in assembler.
    Only, rather than assemble it, and then run it, you ran the asm
    pre-processor over it repeatedly and it displayed each iteration as a
    neat little square of spaces and Xs.
     
    Simian, Sep 29, 2003
    #22
    1. Advertisements

  3. Desmond Coughlan

    Badger Guest

    Wearing perfume?
     
    Badger, Sep 29, 2003
    #23
  4. Verdigris wrote
    The ole school enrolment scenario has been used in computing 101 as a
    project theme since the dawn of time.
     
    steve auvache, Sep 29, 2003
    #24
  5. Truck firing.

    --

    Dave

    GS 850 x2 / SE 6a
    SbS#6? DIAABTCOD#16 APOSTLE#16? FUB#3
    FUB KotL OSOS#12? UKRMMA#19
     
    Grimly Curmudgeon, Sep 29, 2003
    #25
  6. Desmond Coughlan

    mups Guest

    6 - Avoiding soap
    7 - Roadside BBQ's
    8 - Shopping for chicory
     
    mups, Sep 30, 2003
    #26
  7. Desmond Coughlan

    CT Guest

    6 - Driving Seats.
    7 - Running over English people.
    8 - Fitting new windscreens.

    HTH
     
    CT, Sep 30, 2003
    #27
  8. Desmond Coughlan

    Sean Doherty Guest

    Good. Read them then.
    Erm, yes. As my elder brother once said to me when I was leaning all
    this IT malarky: 'do your own homework sonny' ;)

    Good.
     
    Sean Doherty, Sep 30, 2003
    #28
  9. Desmond Coughlan

    paul Guest

    /me obviously has too much time on his hands...

    Test files:

    slots:

    Karate London Wed 7 9
    Swimming Swindon Tue 5 6
    Judo Leicester Mon 2 3
    Knitting Swindon Wed 6 9
    Fencing Glasgow Wed 11 15


    travel: (how long it takes to go from one location to another)

    Default 2
    Glasgow Leicester 3
    Glasgow London 5
    Glasgow Swindon 5
    Leicester London 2
    Leicester Swindon 4
    London Swindon 3


    bookings:

    Fred Karate
    Joe Swimming
    Jim Judo
    Fred Fencing
    Jim Knitting

    Perl:


    #!/usr/bin/perl
    use strict;
    my (%bookings, %location, %day, %start, %end, %times);
    my $default;

    open SLOT, "<slots";
    while (<SLOT>) {
    chomp;
    my ($activity, $location, $day, $start, $end ) = split;
    $location{$activity} = $location;
    $day{$activity} = $day;
    $start{$activity} = $start;
    $end{$activity} = $end;
    }
    close SLOTS;

    open TRAVEL, "<travel";
    while (<TRAVEL>) {
    chomp;
    if (/Default/) {
    (my $junk, $default) = split;
    } else {
    my ($from, $to, $time) = split;
    $times{$from,$to} = $time;
    $times{$to,$from} = $time;
    }
    }
    close TRAVEL;

    open BOOKING, "<bookings";
    while (<BOOKING>) {
    chomp;
    my ($who, $activity) = split;
    if (!isbooked($who, $activity)) {
    book($who, $activity);
    }
    }
    close BOOKING;

    sub isbooked {
    my ($who, $activity) = @_;
    foreach my $booking (@{$bookings{$who}}) {
    if ($day{$activity} eq $day{$booking}) {
    my $time = &timeto($location{$booking}, $location{$activity});
    if ($start{$activity} > $start{$booking}) {
    if ($start{$activity} < $end{$booking} + $time) {
    print "conflict $who $activity with booked $booking\n";
    return 1;
    }
    } else {
    if ($end{$activity} + $time > $start{$booking}) {
    print "conflict $who $activity with booked $booking\n";
    return 1;
    }
    }
    }
    }
    return 0;
    }

    sub book {
    my ($who, $activity) = @_;
    push @{$bookings{$who}}, $activity;
    print "booking $who for $activity on $day{$activity}\n";
    }

    sub timeto {
    my ($from, $to) = @_;
    if ($from eq $to) {
    return 0;
    } elsif (exists $times{$from,$to}) {
    return $times{$from,$to};
    } else {
    return $default;
    }
    }

    # vim: set ai:

    Bugs:

    Doesn't handle timeslots going over midnight.
    Needs to be extended to handle minutes rather than just hours

    Test run:

    $ ./check.pl
    booking Fred for Karate on Wed
    booking Joe for Swimming on Tue
    booking Jim for Judo on Mon
    conflict Fred Fencing with booked Karate
    booking Jim for Knitting on Wed
     
    paul, Sep 30, 2003
    #29
  10. Desmond Coughlan

    Ben Guest

    6 - How to avoid any semblance of personal hygiene
    7 - 'ow to make a de sweet lurving
    8 - Arthouse
     
    Ben, Sep 30, 2003
    #30
  11. Desmond Coughlan

    Verdigris Guest

    It's not one I've come across. The first training I had was all about
    doing statistical nonsense. Later on it was all boring invoices and
    other commercial gubbins.
     
    Verdigris, Sep 30, 2003
    #31
  12. Desmond Coughlan

    paul Guest

    I take it you didn't like my perl solution then?

    Paul
     
    paul, Oct 6, 2003
    #32
  13. The way I looked at it was that the Perl way would work once, but that if I
    could combine Perl to dynamically update the SQL database, then a permanent
    record could be had, of all enrollments, conflicts, etc. etc.
     
    Desmond Coughlan, Oct 6, 2003
    #33
    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.