#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Location
    UK
    Posts
    196
    Rep Power
    15

    Replace Text in a Variable


    This is a simple question, I would like to get into a variable the directory name; e.g. Items2 when I run the script I should be in that directory.

    Code:
    my $directory = getcwd();
    So $directory should be "/home/account3/Designer/ABCD/Item2/"

    I want variable $dirName to be "Item2"

    I then want to open file in a sub-directory which use part of the directory name; e.g.

    Code:
    $name_out = $directory . "/INT/" . $dirName . "_name_out.dat";
    open(FILE, $name_out) or die("Unable to open file");
    The file is Item2_name_out.dat.

    How to extract the Items2 out of the $directory variable.

    Thanks,

    Andy.
  2. #2
  3. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Oct 2004
    Location
    Sunny Southern California
    Posts
    2,687
    Rep Power
    866
    Code:
    $directory = '/home/account3/Designer/ABCD/Item2/';
    my ($dirName) = $directory =~ /\/(\w+)\/?$/;
    print $dirName;
    Wait for more suggestions and pick the one you think is best
  4. #3
  5. wizard
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2009
    Location
    The Great White North
    Posts
    83
    Rep Power
    142
    See `perldoc File::Basename` and `perldoc File::Spec`.

    Comments on this post

    • ishnid agrees

IMN logo majestic logo threadwatch logo seochat tools logo