On 09/20/2012 04:39 PM, Irfan Sayed wrote:
got it myself :)
thanks a lot
  $line_to_add =~ m/([a-zA-Z]+\.csproj)/;


Hi Irfan,

Your solution will only match files that consist of ASCII alphabetic characters followed by '.csproj'. It will also match these:

* 'c:\p4\car\abc\foo.csproj\file.txt'
* 'c:\p4\car\abc\123xyx.csproj'
* 'c:\p4\car\abc\xyz.csprojabc'

Octavian's solution is much more general. Run this program to see why:

#!/usr/bin/perl

use strict;
use warnings;

my @paths = (
    'c:\p4\car\abc\xyz.csproj',
    'c:\p4\car\abc\foo.csproj\file.txt',
    'c:\p4\car\abc\123xyx.csproj',
    'c:\p4\car\abc\xyz.csprojabc',
);

foreach my $path ( @paths ) {
    my ($irfan) = $path =~ m/([a-zA-Z]+\.csproj)/;
    $irfan ||= 'no match';
    my ($octavian) = $path =~ /([^\\]+)$/;
    $octavian ||= 'no match';
    print <<"EOINFO";
Path: $path
Irfan: $irfan
Octavian: $octavian

EOINFO
}
__END__

Path: c:\p4\car\abc\xyz.csproj
Irfan: xyz.csproj
Octavian: xyz.csproj

Path: c:\p4\car\abc\foo.csproj\file.txt
Irfan: foo.csproj
Octavian: file.txt

Path: c:\p4\car\abc\123xyx.csproj
Irfan: xyx.csproj
Octavian: 123xyx.csproj

Path: c:\p4\car\abc\xyz.csprojabc
Irfan: xyz.csproj
Octavian: xyz.csprojabc

A more idiomatic way to do this is to use the File::Spec module. Inspect the output of this program for inspiration:

#!/usr/bin/perl

use strict;
use warnings;

use File::Spec; # for splitpath

my $path = 'c:\p4\car\abc\xyz.csproj';

my ( $volume, $directories, $file) = File::Spec->splitpath( $path );

print <<"EOINFO";
Volume: $volume
Directories: $directories
File: $file
EOINFO

exit 0;
__END__

Cheers,
Michael


--
Michael Brader                    Senior Software Engineer and Perl Person
                                  Technology/Softdev/M&E Small Change Team
Internode       http://internode.on.net/          mbra...@internode.com.au
iiNet             http://iinet.net.au/         m.bra...@staff.iinet.net.au


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to