Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Monks; I am at a loss. I need to be able to take a directory structure like /TC/APR-TCCC-0003/APR21-TCCC-0003 that has been assinged into $dir and assing into $dataSet.

Replies are listed 'Best First'.
Re: Directory Parsing
by polettix (Vicar) on Sep 06, 2005 at 19:24 UTC
    Please elaborate on this. Try to forget that you know the context you're working in, and read your question: does it make sense? My obvious answer is:
    $dataSet = $dir;
    but it is clearly not what you're after.

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
Re: Directory Parsing
by davido (Cardinal) on Sep 06, 2005 at 19:27 UTC

    Something like this?

    use strict; use warnings; use File::Find; use Data::Dumper; my $dir = '/TC/APR-TCCC-0003/APR21-TCCC-0003'; my $dataSet = []; find( sub { if( -f ) { push @{$dataSet}, $File::Find::name; } }, $dir ); print Dumper $dataSet;

    Or did you mean...

    $dataSet = $dir;

    Please follow up with clarification as to what you mean by assigning into $dataSet a directory structure that has been assigned into $dir. The lack of clarity of the question will probably cause us to all miss the mark in our replies.


    Dave

Re: Directory Parsing
by Anonymous Monk on Sep 06, 2005 at 19:50 UTC
    I guess that is the problem. I am not sure that the dataset will always be the second directory.
    The directory structure most often looks like the APR21-TCCC-0003. The APR will always appear as standard the 21-TCCC-0003 can change but will always follow the convention of 2 alpha-numerics dash 4 alpha-numerics dash 4 alpha-numerics.
    Thanks for any and all help on this.

      In which case a regex is what you need.

      ($dataSet) = $dir =~ m|/(APR\w\w-\w{4}-\w{4}).*?$|;

      Note that \w matches A-Aa-a0-9_. If you need a subset of that range (upercase only for example), replace the \w's with something like [A-Z0-9].


      Perl is Huffman encoded by design.
Re: Directory Parsing
by Anonymous Monk on Sep 06, 2005 at 20:26 UTC
    Thanks to all of the monks for the help.
    I was able to take the sugestions and landed up on using the RegEx one.
    RegEx either kills me or slays me but the result is still the same it is a weakness of which I am working on.



    Again thans for the help.

      regex's come up often here. Read the various nodes, even the ones where the titles look completely outside any area you think you may be interested in, because many threads wander all over the place and provide a lot if interesting, educational and thought provoking ideas.

      You should browse the following documentation, probably in the order given: perlretut, perlre and perlreref.


      Perl is Huffman encoded by design.
Re: Directory Parsing
by Anonymous Monk on Sep 06, 2005 at 19:33 UTC
    Monks.
    Since I have passed /TC/APR-TCCC-0003/APR21-TCCC-0003 into $dir what I want to further do is pass APR21-TCCC-0003 into $dataSet so that I can print $dataSet out in an error file like, print( DbFile "DATASET BEING TESTED IS $DataSet\n\n");
      If $DataSet should always be the 2nd directory, you can try something like
      @array_split = split("/\//", $dir); $DataSet = $array_split[1];

      See perldoc File::Basename; one of fileparse or basename might be what you're looking for (although you've still got a somewhat vague problem statement there).

      --
      We're looking for people in ATL