Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Maybe something simple can be done with a hash

Status
Not open for further replies.

mountainbiker

Programmer
Aug 21, 2002
122
0
0
GB
Given a list like:

@x = qw(
/a1/a.html
/a1/b.html
/a1/g.html
/a1/x/d.html
/a1/x/e.html
/a1/x/r.html
/a2/d.html
/a2/c.html
/a2/r.html
/a2/x/h.html
/a2/x/f.html
/a2/x/w.html
/a3/d.html
/a3/c.html
/a3/r.html
/a5/r.html
/a5/x/h.html
...and so on...
);

Parsing the path/filename, the first occurrence of A1, A2, A3,... should be used to print a section header. Under that directory, when the first occurrence of a file is found in the x subdirectory, it should cause "SUB-SECTION" to be printed. In short, the output desired is something like:

SECTION A1
/a1/a.html
/a1/b.html
/a1/g.html
SUB-SECTION
/a1/x/d.html
/a1/x/e.html
/a1/x/r.html
SECTION A2
/a2/d.html
/a2/c.html
/a2/r.html
SUB-SECTION
/a2/x/h.html
/a2/x/f.html
/a2/x/w.html
SECTION A3
/a3/d.html
/a3/c.html
/a3/r.html
SECTION A5
/a2/r.html
SUB-SECTION
/a2/x/h.html
 
As long as the order of your list is consistent, this works

# tek-tips thread219-407527

use diagnostics;

@x = qw(
/a1/a.html
/a1/b.html
/a1/g.html
/a1/x/d.html
/a1/x/e.html
/a1/x/r.html
/a2/d.html
/a2/c.html
/a2/r.html
/a2/x/h.html
/a2/x/f.html
/a2/x/w.html
/a3/d.html
/a3/c.html
/a3/r.html
/a5/r.html
/a5/x/h.html
);

$begin = "nothing";
$mid = "";
$end = "";

foreach $x(@x)
{
(undef,$start,$mid,$end) = split('\/',$x);
if ($start ne $begin)
{
$begin = $start;
$flag_sub = 0; # determines if title Sub-Section prints
print "Section $start\n";
print " /$start/$mid\n";

}
else
{
if (length($end) == 0)
{
print " /$start/$mid\n";
}
else
{
if ($flag_sub == 0)
{
print " Sub-Section\n";
$flag_sub = 1;
}
print " /$start/$mid/$end\n";
}
}
}

print "finished\n";

hth,

Parke
 
Thanks Parke,

Yes, the array is sorted prior to arrival to this code. I ended up using a two hash like the following:
[tt]
use strict;

my @files = qw(
/position1/n.html
/position1/b.html
/position1/g.html
/position1/in_brief/d.html
/position1/in_brief/e.html
/position1/in_brief/r.html
/position2/d.html
/position2/c.html
/position2/r.html
/position2/in_brief/h.html
/position2/in_brief/f.html
/position2/in_brief/w.html
/position3/d.html
/position3/c.html
/position3/r.html
/position5/r.html
/position5/in_brief/h.html
/position6/in_brief/test.html
/position7/ree.html
/position7/rdf.html
/position7/rrr.html
);

my %region_seen;
my %inbrief_seen;

for ( @files ) {

my ($pos, $inbrief) = /\/position(\d+)(\/in_brief)?\/.*/;

if ( ! exists $region_seen{ $pos } ) {
print "\nRegion $pos\n";
$region_seen{ $pos } = 1;
}

if ( $inbrief && ! exists $inbrief_seen{ $pos } ) {
print "\nIn Brief $pos\n";
$inbrief_seen{ $pos } = 1;
}

print "$_\n";

}
[/tt]
*p.s. forgot in original post to state there could be chapters/regions without any files, but it still could have sub-sections/inbriefs.
 
or using an array...
[tt]
my @seen;

for ( @files ) {

my ($pos, $inbrief) = /\/position(\d+)(\/in_brief)?\/.*/;

if ( ! $seen[ 0 ][ $pos ] ) {
print "\nRegion $pos\n";
$seen[ 0 ][ $pos ] = 1;
}

if ( $inbrief && ! $seen[ 1 ][ $pos ] ) {
print "\nIn Brief $pos\n";
$seen[ 1 ][ $pos ] = 1;
}

print "$_\n";

}
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top