Split function 
Author Message
 Split function

I want to split a file name and path down to its specific parts using the
split function..
for exampe the file is ... c:\windows\something\file.txt

i want to use split with a delimiter of  "\"

i've tried ...

and none of these are working properly please help!



Mon, 30 Aug 2004 23:02:07 GMT  
 Split function
  perldoc File::Basename

  This module methods will split absolute file name
into component parts (.ie basename, path, extension).

RWW



Quote:
> I want to split a file name and path down to its specific parts using the
> split function..
> for exampe the file is ... c:\windows\something\file.txt

> i want to use split with a delimiter of  "\"

> i've tried ...




> and none of these are working properly please help!



Tue, 31 Aug 2004 00:20:47 GMT  
 Split function
I'm sure you'll be kicking yourself for this one.....


the backslash is a special char (used for backslash-escaping) so it needs to
be backslash-escaped.  Otherwise, you are escapeing the next char.
Same goes for in a text string.  of your program reads :

$file = "c:\windows\something\file.txt";

you will have to change it to

$file = "c:\\windows\\something\\file.txt";

or use a front slash -- but then the split pattern is /\// -- got it?



Quote:
> I want to split a file name and path down to its specific parts using the
> split function..
> for exampe the file is ... c:\windows\something\file.txt

> i want to use split with a delimiter of  "\"

> i've tried ...




> and none of these are working properly please help!



Tue, 31 Aug 2004 00:35:18 GMT  
 Split function


Quote:
> I want to split a file name and path down to its specific parts using the
> split function..
> for exampe the file is ... c:\windows\something\file.txt

> i want to use split with a delimiter of  "\"

> i've tried ...



This doesn't even compile. The backslash escapes the closing forward slash,
thus making it a part of the search pattern.
And this means the RE isn't terminated any more.

This searches for two double quotes (there is no need to escape the second
double quote, therefore the backslash will be ignored).

And this searches for two single quotes (same reason as for the double
quote).

Instead try escaping the backslash itself:

However, if all you need is really the path and the filename then you are
much better of with File:Basename.

jue



Tue, 31 Aug 2004 03:08:54 GMT  
 Split function
Try split /\\/, $file.  Not that they harm anything, but you don't
need all of those extra parentheses unless you're on Perl 4.

HOWEVER, (and forgive my asking), are you sure this is what you want?
Would you rather not prefer something like:

$separator = rindex $string, '\\';
$path = substr $string, 0, $separator;   # c:\windows\something
$file = substr $string, $separator + 1;  # file.txt


Quote:
> I want to split a file name and path down to its specific parts using the
> split function..
> for exampe the file is ... c:\windows\something\file.txt

> i want to use split with a delimiter of  "\"

> i've tried ...




> and none of these are working properly please help!



Tue, 31 Aug 2004 04:20:10 GMT  
 Split function
I don't do windows perl and fortunately I've never had this problem
heh, and I don't know the answer off hand.  However, the problem
you're having is with the first \w in \windows.  Perl things this is a

know the answer - but I don't.  I'm sure someone here will and I would
like to know just as well. :)

happy hackin'
mike


Quote:
> I want to split a file name and path down to its specific parts using the
> split function..
> for exampe the file is ... c:\windows\something\file.txt

> i want to use split with a delimiter of  "\"

> i've tried ...




> and none of these are working properly please help!



Tue, 31 Aug 2004 04:58:28 GMT  
 Split function
ok, this works fine:

#!/usr/bin/perl -w
use strict;
use File::Basename;

my $imagepath = 'C:\windows\crap\image.jpg';
fileparse_set_fstype( 'MSWin32' );
my $file_only = basename( $imagepath );
print($file_only . "\n");

my $path = dirname($imagepath);



    print "I: $i\n";

Quote:
}

I probably went the long way, but for whatever reason, splitting it
from the begining just doesn't work.  Enlightened ones ?

hth.
mike


Quote:
> I want to split a file name and path down to its specific parts using the
> split function..
> for exampe the file is ... c:\windows\something\file.txt

> i want to use split with a delimiter of  "\"

> i've tried ...




> and none of these are working properly please help!



Tue, 31 Aug 2004 06:32:44 GMT  
 Split function
god i'm a dumbass. if you just make the $string be in single quotes

#!/usr/bin/perl -w
use strict;

my $string = 'c:\windows\sucks\seriously.txt';



    print "element: $element\n";

Quote:
}

of course, if you want to do what i did below -- then have at it :)

hth,
mike

--- i hear voices and they don't like you

Quote:

> ok, this works fine:

> #!/usr/bin/perl -w
> use strict;
> use File::Basename;

> my $imagepath = 'C:\windows\crap\image.jpg';
> fileparse_set_fstype( 'MSWin32' );
> my $file_only = basename( $imagepath );
> print($file_only . "\n");

> my $path = dirname($imagepath);



>     print "I: $i\n";
> }

> I probably went the long way, but for whatever reason, splitting it
> from the begining just doesn't work.  Enlightened ones ?

> hth.
> mike


> > I want to split a file name and path down to its specific parts using the
> > split function..
> > for exampe the file is ... c:\windows\something\file.txt

> > i want to use split with a delimiter of  "\"

> > i've tried ...




> > and none of these are working properly please help!



Tue, 31 Aug 2004 12:58:20 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. How to use split function to split on a backslash

2. Problem with join function (and split function)

3. using split function twice on same line

4. Using SPLIT function with a Period

5. Using a period as a delimiter in the split() function

6. split function that handles quoting..

7. Newbie, split function, and counter

8. Split function in Perl

9. Split Function

10. Using a period as a delimiter in the split() function

11. Help - Split Function Blowing My Mind Away!!

12. An Efficient Split Function

 

 
Powered by phpBB® Forum Software