in reply to screen out an untained array from a special array

How is this:

use strict; use warnings; my @a = qw(0 6:4 12:8 19:31); my %b; for (@a){ if (!/:/){ $b{$_}=1; } else { my($a,$b) = split(/:/, $_); ($a,$b)=($b,$a) if $a>$b; $b{$_}=1 for($a..$b); } } print join(" ", grep {!exists $b{$_}} (0..31)), "\n";

Update:

In avoidance of tax filing, I carried on... You asked for 13:18, and later 18:13. This converts 0 6:4 12:8 19:31 to 1:3 7 13:18.

use strict; use warnings; my @a = qw(0 6:4 12:8 19:31); my $s = ''; vec($s,0,32) = 0; for (@a){ if (/(\d+):(\d+)/){ vec($s,$_,1) = 1 for(($1<$2) ? $1..$2 : $2..$1); } else { vec($s,$_,1) = 1; } } my @bits = split(//,unpack("b*",$s)); my $series = 0; for(0..31){ if ($bits[$_] eq 0) { if (!$series) { print; $series = $_; } } else { if ($series) { print ( ($series+1 < $_) ? ':'.($_-1).' ' : ' '); } $series = 0; } }

Replies are listed 'Best First'.
Re^2: screen out an untained array from a special array
by Hanken (Acolyte) on Feb 08, 2009 at 02:05 UTC
    Yes, the output is: 1 2 3 7 13 14 15 16 17 18
    can it be showed like: '3:1' '7' '18:13'?
    You know the format conversion is what the difficulty lies.
      can it be showed like: '3:1' '7' '18:13'?
      Of course it can! This is probably where you should start trying to write the code yourself. Since you requested some hints in your OP, take the hash that other monks have shown you and loop through the sorted keys.

      What a perfect opportunity for you to learn some more Perl!

      And you didn't even know bears could type.

Re^2: screen out an untained array from a special array
by Hanken (Acolyte) on Feb 08, 2009 at 11:46 UTC
    Oh, what a nice solution. Thanks.
    But if the output is the single digit series, it might be printed together. Say the input is: 1 9:2 21:10, 22:30, the outpout is: 031 (instead of 0, 31). Anyway, it's not a big deal.

      I'm not sure if you were replying to me, but my updated solution using vec converts "1 9:2 21:10 22:30" to "0 31".