in reply to Directory Structure parsing

A possible solution:
#!/usr/bin/perl -w use strict; # Here go all the top-level categories my @categories = qw(clocks daisies); # And the subcategories. my @subcats = qw(vert red); # Directory contents. You could also loop through STDIN or # something. See the comment below the foreach loop. my @names = qw(clocks001.jpg clocks002.jpg clocksvert001.jpg clocksvert002.jpg clocksvertred001.jpg daisies001.jpg); foreach my $name (@names) { # while (my $name = <STDIN>) { # chomp($name) # Get image number and strip off the extension my $output = "$name -> "; my ($im_no) = $1 if $name =~ s/(\d+)\..+$//g; foreach my $cat (@categories) { if ($name =~ s/^$cat//) { $output .= "category: $cat; "; next; } } $output .= "subcats:"; foreach my $subcat (@subcats) { if (not $name) { $output .= " null/null"; last; } if ($name =~ s/^$subcat//) { $output .= " $subcat/1"; } } $output .= " IM#: $im_no"; print "$output\n"; }

Arjen

Update: Fixed a small bug in the output.

Replies are listed 'Best First'.
Re: Re: Directory Structure parsing
by Aragorn (Curate) on Apr 01, 2003 at 20:18 UTC
    Thinking some more about it; if there can more of the same subcategorie (as suggested by the <subcat>/number), then you can replace the code
    if ($name =~ s/^$subcat//) { $output .= " $subcat/1"; }
    with
    if ($name =~ /^$subcat/) { $output .= " $subcat/"; my $c = 0; $c++ while $name =~ s/^$subcat//; $output .= $c; } # Special case: we don't want "null/null" after # adding valid subcats. last if not $name;
    This way, filenames like "clocksvertvertred001.jpg" are displayed like

    clocksvertvertred001.jpg -> category: clocks; subcats: vert/2 red/1 IM#: 001

    Arjen