pow, that did it.  I tried every combination of @ signs and braces I could
think of except that one.  =)

__________________



Ooh, you know, I didn't try it with an array of arrays.  I guess you would
have to dereference the array first.

 print "@{$lines[0]}[0,1]\n";

-----Original Message-----
From: Bryan R Harris [mailto:[EMAIL PROTECTED]]
Sent: Friday, May 31, 2002 3:26 PM
To: [EMAIL PROTECTED]
Subject: RE: union of times algorithm



Nope, the compiler won't allow that...  Does it work for you?

It gives the errors:
  Scalar value @lines[0] better written as $lines[0] at ...
  Can't use subscript on array slice at ...  near "1]"
  Execution ... aborted due to compilation errors.

- B

__________________

No.  I just ran into this problem myself.  If you want to print an array
slice, you will have to use the @ symbol at the beginning.

   print "@lines[0][0,1]\n";


-----Original Message-----
From: Bryan R Harris [mailto:[EMAIL PROTECTED]]
Sent: Friday, May 31, 2002 3:15 PM
To: [EMAIL PROTECTED]
Subject: RE: union of times algorithm



Thanks Ovid, Timothy...

You mention checking out perlref in the perldocs-- I'm familiar with
"perldoc -f keyword", but how would I find information on these things if I
didn't know the keyword "perlref"?  (Apparently I'm the only legitimate
"beginner" here!  =)

Oh, and one more question.  =)

   $lines[0] = [  "dog", "cat" ];
   print "$lines[0][0,1]\n";

This prints "cat".  Shouldn't it print "dogcat"?

TIA, TA.

- Bryan

__________________



Before I start, don't forget to check out perlref in the perldocs.  Some
quick definitions:

Reference - simplified, a reference points to another variable or
structure.
This allows us to use one variable to manipulate another

1. $somevar = [@somearray,$somescalar];
   Creates a reference to an anonymous array consisting of the values of
@somearray and $somescalar.

2. $somevar = $someothervar->[$athirdvar];
   Accesses the $athirdvar element of the array referenced by
$someothervar.
You might have seen it done this way before:

   $somevar = ${$someothervar}[$athirdvar];

3. $somevar = \@somearray;
   Makes $somevar into a reference to the array @somearray.  you see this
format often when passing a reference to an array to a subroutine or
function.

-----Original Message-----
From: Bryan R Harris [mailto:[EMAIL PROTECTED]]
Sent: Friday, May 31, 2002 2:19 PM
To: [EMAIL PROTECTED]
Subject: RE: union of times algorithm



Wow, you guys are amazing...  This most recent seems to work, so I'm set
(though I still don't quite understand it).

A few quick questions:

    1.  What does this do?:      $somevar = [ @somearray, $somescalar ];
    2.  and this?:      $somevar = $someothervar->[$athirdvar];
    3.  and this?:      $somevar = \@somearray;

Thanks again.  I'm starting to see why Perl is so popular...

- B

__________________


> Okay, I've repaired it - it now works fine under warnings and strict.
> Almost all my code is written for strictures, I just posted my code
> in an intermediate form without having debugged it.  I think it still
> isn't working right, as the answer given is 6 but I reckon it should
> be 7!

Hi guys,

Can we have a comment on whether we are spamming the list too much
with this thread?  I do think it is a valuable discussion, but perhaps
not as valuable to the beginners.  I suppose there has been lots of
off-topic stuff without comments made.

I have just eliminated the Last Bug (tm), the algorithm that follows
is hand tested and should be tested further before deploying it.  It
has GOOD complexity analysis, although I'm too lazy to actually do it.

To squeeze performance, sort the arrays by 0th element only... and then
use a single bubble sort pass.  The 2nd element and the $active hash can
be exchanged for a more basic incrementing/decrementing counter of the
number of active elements.

Can someone look in their Knuth books, and tell me if I've just
reinvented the wheel - if not I'll write the research papers and make
the publications :)

Anyway, here goes:

#!/usr/bin/perl -w

use strict;

use constant START => 0;
use constant STOP  => 1;

use constant TIME  => 0;
use constant TYPE  => 1;
use constant ID    => 2;

# Test data
my @timeslices = (
    [2, 9],
    [4, 11]
);

sub encode {
    my @timeslices = @{ (shift) };
    my $index = shift || 0;
    my @encoded;

    # Encode the timeslices into a 3 element pair
    foreach my $item (@timeslices) {
        push @encoded,
            [$item->[START], START, $index  ],
            [$item->[STOP],  STOP,  $index++];
    }

    # Sort the three element pair, and return it as
    # an array reference
    return [ sort { $a->[TIME] <=> $b->[TIME]
                               ||
                    $a->[TYPE] <=> $b->[TYPE]
                  } @encoded
    ];
}

sub calculate {
    my @encoded = @{ (shift) };
    my ($previous, %active);
    my $total   = 0;

    foreach my $item (@encoded) {

        # Add if previous to current time was active
        unless (keys %active == 0 or not defined $previous) {
            $total += $item->[TIME] - $previous;
        }

        # START event, add to hash
        if ($item->[TYPE] == START) {
            $active{$item->[ID]}++;
        }

        # END event, remove from hash
        if ($item->[TYPE] == STOP) {
            delete $active{$item->[ID]};
        }

        $previous = $item->[TIME];
    }
    return $total;
}

# Display total covered
print "Total: " . calculate(encode(\@timeslices)) . "\n";

__END__

I believe this to be working fully, and quickly... please tell
me if there is any further problems.

Jonathan Paton

=====
$_=q|.,&@$$. ,.@$&@$. .&$$@. ,,$ ....!$_=$p.'&$@.',y'&$@' .,';for(/\S+/g){
!|.q| .$ .,@, ,$, .,.. @, ,$ ,,@ .,,.!++$.<22?${'y'.$_}=chr$.+64:[$$=${'y'
!|.q| ,@$@&.,. $$$&, ..@&&$,,, $., ..!.$_},$y.=($.=~/22\|26\|3(3\|7)/x?' '
!|.q|. @  ., ,.&,,, , .$..&. .,$  .,,!.$$:"\l$$")]};$y=~/ (.*)/;warn"$1\n"
!|.q|. $ .,. .,$$&&$...&., @.,.&@$@ .|,map{-$|--?$r:$p.=$_}split'!';eval$r

__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to