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

Hey Guys ,, I have this path inside a file :
\nb\v5\srcccc\ans.cpp@@\main\
I want to capture the following :
$dir = nb; $subDir = v5; $file = ans.cpp
I am doing the following :
while (<INFILE>) { if(/\\([^\\]+).*?(\w+\.cpp)/) { push @thing, [$1,$2,$3]; } } for (@thing) { my ($dir,$subDir,$file) = @$_;
I am not able to get the $subDir why ????

Replies are listed 'Best First'.
(tye)Re: parsing a path
by tye (Sage) on Jul 17, 2002 at 16:59 UTC
Re: parsing a path
by kvale (Monsignor) on Jul 17, 2002 at 16:52 UTC
    You are extract three groupings, but have only two sets of parens. Try
    if(/^\\([^\\]+)\\([^\\]+).*?(\w+\.cpp)/)
    -Mark
      tahnks to you all
(joshua)Re: parsing a path
by joshua (Pilgrim) on Jul 17, 2002 at 17:12 UTC
    This might be easier than all that stuff you do with the array...
    #!/usr/bin/perl -w use strict; my $path = '\nb\v5\srcccc\ans.cpp@@\main\\'; my ($dir, $subDir, $file) = $path =~ /^\\([^\\]+)\\([^\\]+).*?(\w+\.cp +p)/; print "$dir\n$subDir\n$file\n";
    Joshua

      /^\\([^\\]+)\\([^\\]+).*?(\w+\.cpp)/

      And you say that might be _easier_? :)

      Anyway, I think you missed a $ (or even better: \z) there... \foo\bar\baz.cpp.cpp.cpp.blah.cpp.

      - Yes, I reinvent wheels.
      - Spam: Visit eurotraQ.
      

        Ah, good point. I was just building on kvale's code though.