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.


In reply to Re: Directory Structure parsing by Aragorn
in thread Directory Structure parsing by bscheiman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.