Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Need to change directory string.

by huchister (Acolyte)
on Jan 02, 2013 at 16:15 UTC ( #1011302=perlquestion: print w/ replies, xml ) Need Help??
huchister has asked for the wisdom of the Perl Monks concerning the following question:

I would like to change directory string. Here is the example URL.
my $dir_str = "test/home/players/demo//thumbs/super.jpg";
I am trying to remove 'demo//thumbs' part to 'demo/thumbs' Is there easier way to put this? I am trying to use s/ method.

Comment on Need to change directory string.
Download Code
Re: Need to change directory string.
by toolic (Chancellor) on Jan 02, 2013 at 16:20 UTC
    Here's one way using s///:
    use warnings; use strict; my $dir_str = "test/home/players/demo//thumbs/super.jpg"; $dir_str =~ s{//+}{/}g; print "$dir_str\n"; __END__ test/home/players/demo/thumbs/super.jpg

    UPDATE: fixed typo. Thanks Athanasius

      Well, this produces .../demothumbs/..., but the OP states that he wants .../demo/thumbs/.... So a small amendment is needed:

      2:29 >perl -wE "$d = 'test/home/players/demo//thumbs/super.jpg'; $d = +~ s[/{2,}][/]g; say $d;" test/home/players/demo/thumbs/super.jpg 2:34 >

      Hope that helps,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Need to change directory string.
by 2teez (Priest) on Jan 02, 2013 at 20:54 UTC

    "..Is there easier way to put this?.."

    Another way is to use canonpath method from File::Spec like so:

    use strict; use warnings; use File::Spec qw(canonpath); my $dir_str = "test/home/players/demo//thumbs/super.jpg"; print File::Spec->canonpath($dir_str);
    Output:
    test/home/players/demo/thumbs/super.jpg

    NOTE:
    No physical check on the filesystem, but a logical cleanup of a path.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1011302]
Approved by toolic
Front-paged by 2teez
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2014-12-20 22:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (99 votes), past polls