in reply to Re^2: Manipulating directory paths
in thread Manipulating directory paths

You can either add xyz/ to the replace part of the s/// operator, or use the concatenation operator (.).

Replies are listed 'Best First'.
Re^4: Manipulating directory paths
by pp (Novice) on Sep 07, 2007 at 19:03 UTC
    I tried the following: $path=~ s{ /log/ .* \z/xyz}{/}xms
    but got an error.
      The replacement expression is the second half, so:
      $path=~ s{ /log/ .* \z}{/xyz/}xms;
        thank you
Re^4: Manipulating directory paths
by pp (Novice) on Sep 13, 2007 at 16:35 UTC
    I tried the options that you have suggested but it seems to work in one case and not in the other.
    I want to be able to replace everything from the log directory onwards with certain other paths as I have explained below.
    I have a conditional statement in the code where I am saying that if $ver_name is present then replace everything from the log directory onwards with $ver_name/abinitio/run into the variable graph_dir and if $ver_name is not present then replace with abinitio/run into the graph_dir.
    I have the following code:
    ( ! -z $ver_name ) ? (our $graph_dir = $before1) =~s{ /log .* \z}{/$ve +r_name/abinitio/run}xms :(our $grap h_dir=$before2) =~s{ /log/ .* \z}{abinitio/run}xms;
    CASE 1:
    my $before1 =/home/user/phase/dir1/log/
    and when passed through the conditional statement I want to derive the below graph_dir
    graph_dir= /home/user/phase/dir1/2.11/ abinitio/run

    CASE 2: my $before2 = /home/user/phase/dir1/log/Illinois/
    and when passed through the conditional statement I want to derive the below graph_dir
    graph_dir =/home/user/phase/dir1/abinitio/run/

    In my output when I see Case1 seems to work fine but when in Case 2 the value I am deriving for graph_dir is /home/user/phase/dir1//abinitio/run

    Can you please point out where I am going wrong with this?

    Thank you.

      "If $ver_name is present" is meaningless, since I'm pretty sure you don't want to check if the variable exists. I'm guessing from your code that you want to check if the file named by $ver_name exists (which -f would do), but it could mean many other things.

      You're using our. Don't use globals.

      Don't use the conditional operator (?:) to conditionally execute entire statements. Using that operator for anything but the simplest expression usually makes it unreadable. It really works best when the returned value is actually used.

      The output you said you get is not the output the code produces.
      It produces '/home/user/phase/dir1abinitio/run'
      and not '/home/user/phase/dir1//abinitio/run'
      You're missing a / because you removed the / before log without replacing it.
      This should have been easy for you to find out by comparing the one that worked and the one that didn't.

      my $replace = (-f $ver_name ? "/$ver_name" : '') . '/abinitio/run'; (my $graph_dir = $before) =~ s{ /log/ .* \z }{$replace}xs;
        $ver_name is a parameter that I am passing to this script. And I want the script to execute in one way if the $ver_name is passed and if it is not passed in an another way.
        And that's why I am checking to see if $ver_name exists or not.

        Also I did try other manipulations with the if-then-else statements to check if $ver_name exists but I was having many problems and that's why I resorted to using (?:)

        In Case 2:
        I am expecting graph_dir to evaluate to = /home/user/phase/dir1/abinitio/run

        but instead I am getting /home/user/phase/dir1//abinitio/run