Just another Perl shrine PerlMonks

### change the end of a path inside file

by Sara (Acolyte)
 on Jul 17, 2002 at 18:50 UTC ( #182553=perlquestion: print w/ replies, xml ) Need Help??
Sara has asked for the wisdom of the Perl Monks concerning the following question:

Hello guys ,, if I have this
```\nb\hot\src\hot2.cpp@@\main\gold\4
[download]```
I want to use this command called cleardiff for that path as follow:
```cleardiff \nb\hot\src\hot2.cpp@@\main\gold\3 \nb\hot\src\hot2.cpp@@\ma
+in\gold\4
[download]```
so what I ma doing is getting the whole line and subtracting the last number by 1 then calling cleardiff for the two path in one command line. I am not sure how to get to the last value .. split doesn't seem to be a solution ,, also =~ s/\.gold\d/\gold\d+1/ not working ,, any easir way .. thanks for help

Comment on change the end of a path inside file
Select or Download Code
Re: change the end of a path inside file
by ichimunki (Priest) on Jul 17, 2002 at 19:10 UTC
```sub dec {return(shift)-1};
\$path =~ s/(\d+)\$/dec(\$1)/e;
[download]```
will change path the way you want, be sure to copy the value before doing this so your system call has access to both values. You may also want to check for success, in case filenames do not end in digits.

thanks to ferrency for the /e tip.

thank you all ,, ;) nice one
Re: change the end of a path inside file
by jwest (Friar) on Jul 17, 2002 at 19:13 UTC
use File::Spec.

The splitpath method will seperate the file name from the directory. Take the file it returns, decrement it, and use the catpath method to rebuild the path.

This solution will be portable across most systems.

```
use File::Spec;

my (\$volume, \$path, \$file) = File::Spec->catpath(\$path_to_decrement);

--\$file;

my \$newpath = File::Spec->catpath(\$volume, \$path, \$file);

[download]```
This way is a little pedestrian, but it gets the job done.

Hope this helps!

--jwest

```-><- -><- -><- -><- -><-
All things are Perfect
To every last Flaw
And bound in accord
With Eris's Law
- HBT; The Book of Advice, 1:7
```

Log In?
 Username: Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://182553]
Approved by ichimunki
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (12)
As of 2013-12-05 07:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?