Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing white space within a string

Status
Not open for further replies.

chrismassey

Programmer
Aug 24, 2007
264
GB
Hello,

I am trying to take a path i.e. z/files/system/system1
I want to remove the last part of the file i.e. /sytem1
So I left with the rest of the pat i.e. z/files/system

However, I am trying to avoid using a module. Below is a section of a larger script which performs this task...

Code:
##### if up one level button pressed
if ($up_one_levelb) {
##### place path onto another variable
$rename_direc_pageb_pathb = $open1;
##### check the file that is opening exists
if  (-e "$initial_path$rename_direc_pageb_pathb") {
##### split the path string into its individual elements
(@rename_direc_pageb_path2)=split(/\//,$rename_direc_pageb_pathb);
##### remove the last element of the array
pop(@rename_direc_pageb_path2);
##### for each of the remaining elements, divide them again
foreach (@rename_direc_pageb_path2) {
$_ = "$_/";
}
##### remove white space between elements by joining them together
my $rename_direc_pageb_path3 = join(// ,@rename_direc_pageb_path2);
##### end check the file that is opening exists
}
##### else
else {
print "<font face=arial size=2><P>Directory Does Not Exist... Error 5.</font>";
##### end else
}
##### path =...
$path = $initial_path .''. $rename_direc_pageb_path3;
##### path_carry =...
$path_carry = $rename_direc_pageb_path3;
##### end if up one level button pressed
}

When I debug the code, I find that some of the white space hasn't been removed by the join function.

- How can I remove this white space?

- What module is best for performing this task? Could this be done using File::Basename?

Thank you,

Chris
 
Join doesn't remove white space.. join joins :) so the white space must have been there before
Removing white space
$var =~ s/\s+//g;
or replace lotsa white space with one space
$var =~ s/\s+/ /g;

should help you out.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
Can you not just use the substitution operator ?
Code:
$_ = '/folder1/subfolder2/abc/filename.extension';
s/(^.*)\/.*$/\1/;
print;
 
Normally there would be no spaces in folder/file names but it is possible.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Especially if you are doing this on windows.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
Hey,

I'm still having problems even though I have now changed the method in which I do this...

Here is the script I am now using
Code:
##################################################
## -Begin- Up One Level BUTTON ($up_one_levelb) ##
##################################################

##### if up one level button pressed
if ($up_one_levelb) {
##### place path onto another variable
$xopen1 = $open1;
##### Use File::Spec module
use File::Spec
##### split the path
@split_xopen1 = File::Spec->splitdir( $xopen1 );
##### remove last element
$removed_element = pop(@split_xopen1);
##### carry new path
$new_xopen1 = File::Spec->catdir( @split_xopen1 );
##### path =...
$path = $initial_path .''. $new_xopen1;
##### path_carry =...
$path_carry = $new_xopen1;
##### DEBUG
print "Content-type: text/html\n\n";
print "<font face=arial size=2><P>open1: $open1</font>";
print "<font face=arial size=2><P>xopen1: $xopen1</font>";
print "<font face=arial size=2><P>split xopen1: @split_xopen1</font>";
print "<font face=arial size=2><P>element removed: $removed_element</font>";
print "<font face=arial size=2><P>new path: $new_xopen1</font>";
print "<font face=arial size=2><P>path: $path</font>";
print "<font face=arial size=2><P>path carry: $path_carry</font>";
##### end if up one level button pressed
}

################################################
## -end- Up One Level BUTTON ($up_one_levelb) ##
################################################

The print statements at the end are to simply help me debug the problem... Here are the debug results...
Code:
open1: z/Properties

xopen1: z/Properties

split xopen1: 

element removed: 

new path: 

path: /kunden/homepages/4/d160423788/htdocs/ChrisMassey.co.uk/Perl/Scripts/ControlPanelV1.0/

path carry:

So as you can see, when I split xopen1, it should print "z Properties", but nothing is printed at all. Therefore processes further on do not function properly.

Any ideas why the value of xopen1 when split is nothing?

Cheers,

Chris
 
In case you need to know what the debug result should look like...

Code:
open1: z/Properties

xopen1: z/Properties

split xopen1: z

element removed: Properties

new path: z

path: /kunden/homepages/4/d160423788/htdocs/ChrisMassey.co.uk/Perl/Scripts/ControlPanelV1.0/z

path carry: z
 
Right, I have fixed my problem now using File::Basename instead...

Code:
##################################################
## -Begin- Up One Level BUTTON ($up_one_levelb) ##
##################################################

##### if up one level button pressed
if ($up_one_levelb) {
##### place path onto another variable
##### Use File::Basename module
use File::Basename;
##### Get new directory path
$dir = dirname($open1);
##### path =...
$path = $initial_path .''. $dir;
##### path_carry =...
$path_carry = $dir;
##### end if up one level button pressed
}

################################################
## -end- Up One Level BUTTON ($up_one_levelb) ##
################################################

Its doing exactly the same thing as the code using File::Spec, so i'm still interested in why it wasn't working.

Thank you
 
I'm confused why you are using a module to do this.
$path = 'c:/test1/test2/test3/test4';

@tmp = split /\//, $path;

pop @tmp;

$new_path = join('/', @tmp);

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
I didn't want to use a module to begin with, but it has now become necessary because of the varying ways in which I need to manipulate file paths. I don't just need to remove last part of a string. Therefore I have found it simpler to use a module which can perform many required tasks.

I think I was having problems because I was using an unnecessary foreach loop to put a / back on the end of each $_. And then my join parameter was (/ ) instead of just (/).

Thanks for all your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top