It looks like you're trying to do something reminiscent of dealing with schedules or availablity, but that is just supposition. Your output, however, looks like it could be done with a complex data structure. (I have also included the data Jasper inquired about, to show how it would be handled by this untested sample code as well.)
my (%data);
while (<DATA>) {
chomp;
# $_ contains '100-233,MA,150:250', for example
my @initialparts = split( /,/, $_ );
# @initialparts now contains ( '100-233', 'MA', '150:250' )
my @leadparts = split( /-/, $initialparts[0]);
# @leadparts now contains ( '100', '233' )
push( @{$data{$leadparts[0]}{$initialparts[1]}{'items'},
$leadparts[1] );
# @{$data{'100'}{'MA'}{'items'} = ( '233' )
if (scalar(@initialparts) > 2) {
# Do only if there was a last term
my @tailparts = split( /:/, $initialparts[2] );
# @tailparts now contains ( '150', '250' )
push( @{$data{$leadparts[0]}{$initialparts[1]}{'data'},
@tailparts );
}
}
open(OUTF, $0 . '.out') or die("Can't open $0.out for output: $!\n");
foreach my $f1 (sort(keys(%data))) {
# Personally, I prefer data sorted,
# although you could leave out the sorts above, and following
print $f1, "\n";
foreach my $f2 (sort(keys(%{$data{$f1}}))) {
print $f2, "\n";
my $v4 = '|'
if (defined($data{$f1}{$f2}{'data'} {
$v4 .= join( ':', sort(@{$data{$f1}{$f2}{'data'}) );
}
foreach my $v3 (sort(@{$data{$f1}{$f2}{'items'})) {
print $v3, $v4, "\n";
}
}
}
__DATA__
100-233,MA,150:250
100-344,MA,350:
200-400,ER,
200-300,ER,576
100-250,MA,150
75-300,MA,350
And now, a run, based on that data.
If you do not wish duplicates in the last portion of the data, then you could do something like:
if (defined($data{$f1}{$f2}{'data'} {
my (%uniq);
foreach (@{$data{$f1}{$f2}{'data'}) {
$uniq{$_}++;
}
$v4 .= join( ':', sort(keys(%uniq)) );
}
Hope that helps.
Update: 19 Aug 2004
Added <readmore></readmore> tags around the run/variable trace. |