Here are some functions that will do this, but more important maybe, a harness that will let you try your own:

#!/usr/bin/perl -w use strict; use Test::More qw(no_plan); my @data = read_data(); my @expected_results = read_data(); foreach my $name qw(cut_join_split cut_regexp cut_substr) { no strict 'refs'; my $func= \&$name; my @results= map { $func->($_) } @data; ok( eq_array(\@results, \@expected_results), $name) or diag( "expected: " . join( ' - ', @expected_results) . "\n", "got : " . join( ' - ', @results) . "\n"); } sub cut_join_split { my $filename= shift; return join( '_', (split /_/, $filename)[0..1]); } sub cut_regexp { my $filename= shift; return $filename=~ m{^([^_]*_[^_]*)}; } sub cut_substr { my $filename= shift; my( $index, $sep, $sep_number)= ( 0, '_', 2); # to find _ number 2 foreach (1..$sep_number) { $index = index( $filename, $sep, $index+1); } return substr( $filename, 0, $index); } sub read_data { local $/="\n\n"; return grep {$_} map { s{#.*}{}; chomp; $_ } spli +t( "\n", <DATA>); } __DATA__ # data smith_13_503_de7.p smith_13_502_de7.v jones_104_503_de7.p jones_104_502_de7.v # you might want to add more filenames to your test set # expected results smith_13 smith_13 jones_104 jones_104

In reply to Re: Filename Parsing by mirod
in thread Filename Parsing by The_Jesus

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.