> Often I need to extract the file name from a path. $current_file = "c:/somedir/test.ps"; @split_path = split("/",$current_file); $current_file = @split_path[-1]; print $current_file;
You want to use the '$' sigil here, not '@' (you're refering to just one element, not the whole array): $current_file = $split_path[-1]; or $current_file = pop @split_path; #!/perl -w $current_file = "c:/somedir/test.ps"; $current_file =~ /.*(\/|\\)(.*)/gi; print $2; Er, you don't want/need the /g (only matches once) or the /i (no letters to ignore case on), you don't want to match in the null context so, at least: #!/perl -w my $current_file = "c:/somedir/test.ps"; if ( $current_file =~ /.*(\/|\\)(.*)/ ) { print $2; } else { warn("whacky file path: $current_file\n"); } If you don't test your match, you'll never know if it worked. There's an 'interesting' feature of perl this way, if the match fails "$2" retains any value it may have had, so you can get a result from a previous match. Not a good thing and hard to figure out. This is relying on the initial "dot star" being greedy enough to eat all preceeding slashes and will fail w/ c:myfile.txt and, on non-DOS /tmp/this\ filename\ has\ embeded\ spaces Probably the best way is to let somebody else do it: File::Basename(3) NAME fileparse - split a pathname into pieces basename - extract just the filename from a path dirname - extract just the directory from a path SYNOPSIS use File::Basename; ($name,$path,$suffix) = fileparse($fullname,@suffixlist); $name = fileparse($fullname,@suffixlist); fileparse_set_fstype($os_string); $basename = basename($fullname,@suffixlist); $dirname = dirname($fullname); ($name,$path,$suffix) = fileparse("lib/File/Basename.pm",qr{\.pm}); fileparse_set_fstype("VMS"); $basename = basename("lib/File/Basename.pm",".pm"); $dirname = dirname("lib/File/Basename.pm"); a Andy Bach Systems Mangler Internet: [EMAIL PROTECTED] VOICE: (608) 261-5738 FAX 264-5932 Punctuality is the virtue of the bored. --Evelyn Waugh (1903-1966) _______________________________________________ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs