in reply to Traverse directory backwards based on file

Some further thoughts on this.

If $pref is supposed to hold 001, you're removing all digits from it with:

$pref =~ s/[0-9]+//gi;

I'm wondering if that substitution is supposed to be the same as the previous two (i.e. s/[^0-9]+//gi). If not, the 'i' (case insensitivity) modifier is pointless.

On a more general point regarding those substitutions, s/chars//g is a slow way to do it and s/chars//gi is even slower. A transliteration (or, in this case, transobliteration) is faster. See the "Search and replace or tr" section of "Perl Performance and Optimization Techniques".

You have six instances in your code. Here's how you could replace them:

$ perl -le 'my $x = "1q2w3e"; $x =~ y/0-9//cd; print $x' 123
$ perl -le 'my $x = "1q2w3e"; $x =~ y/0-9//d; print $x' qwe
$ perl -le 'my $x = "ÞORNþorn"; $x =~ y/Þþ//d; print $x' ORNorn

In case you didn't know, y/// and tr/// are synonymous. See "perlop: Quote-Like Operators" for more details.

-- Ken

Replies are listed 'Best First'.
Re^2: Traverse directory backwards based on file
by subhash1198 (Initiate) on Mar 30, 2014 at 03:30 UTC

    Hi Ken, Thank you for replying. The code is right. To make it more clear, let me give you more details on data. The input file looks like this

    þBEGDOCþþENDDOCþ þHJH1411255þþHJH1411255þ þHJH1411256þþHJH1411256þ þHJH1411257þþHJH1411257þ þHJH1411258þþHJH1411258þ þHJH1411259þþHJH1411260þ þHJH1411261þþHJH1411262þ
    and output from the above script looks like this
    þHJH1411255,NAME,\\SHARE\OUTPUT3\HOST\CLIENT\NAME\AFT-053348\VOL_006\I +MAGES\HJH1411255.tif,Y,,,1 þHJH1411256,NAME,\\SHARE\OUTPUT3\HOST\CLIENT\NAME\AFT-053348\VOL_006\I +MAGES\HJH1411256.tif,Y,,,1 þHJH1411257,NAME,\\SHARE\OUTPUT3\HOST\CLIENT\NAME\AFT-053348\VOL_006\I +MAGES\HJH1411257.tif,Y,,,1 þHJH1411258,NAME,\\SHARE\OUTPUT3\HOST\CLIENT\NAME\AFT-053348\VOL_006\I +MAGES\HJH1411258.tif,Y,,,1 þHJH1411259,NAME,\\SHARE\OUTPUT3\HOST\CLIENT\NAME\AFT-053348\VOL_006\I +MAGES\HJH1411259.tif,Y,,,2 þHJH1411260,NAME,\\SHARE\OUTPUT3\HOST\CLIENT\NAME\AFT-053348\VOL_006\I +MAGES\HJH1411260.tif,,,,
    The problem is after IMAGES i should get the subfolders also. like HJH1411260,NAME,\\SHARE\OUTPUT3\HOST\CLIENT\NAME\AFT-053348\VOL_006\IMAGES\001\HJH1411260.tif,,,, I have just 1 level folder structure after IMAGES folder. Now the point is based on the input file and the image path the script has to output complete image path till the file name. Let me know if you have questions or need clarifications.

      "Let me know if you have questions or need clarifications."

      Well, I've already done that but you haven't responded yet.

      I asked you to try some print statements; I even provided sample code. Did you do this? What was the result?

      I asked about which variable was supposed to hold 001. You haven't told us.

      [Separate issue.] Your output is dependent on user input. You don't show what that user input is.

      "and output from the above script looks like this"

      No, that's not possible! Look at this line:

      print OUT $pref . $number . "," . $prodvolumename . ",$tiffpath\\$pref +$number.tif,";

      How can this concatenation

      $pref . $number

      produce

      þHJH1411255

      when the same concatenation (albeit coded slightly differently)

      $pref$number

      produces

      HJH1411255

      Where did that thorn character come from in the first instance; or, conversely, how did it disappear in the second instance?

      Either that's not the output you're getting or you're running different code to what you posted.

      All I can do now is make guesses! Maybe the user isn't including 001 when entering $tiffpath. Maybe your code doesn't generate 001 for the output. Maybe something else.

      -- Ken

        Hi Ken, I cannot show the input folders or filename here as its a confidential data. Let me frame you the scenario: I have Folder containing IMAGE files in TIF format. They are spread across different subfolders like 001,002,003,004,005...etc . I am giving a path till the parent folder that is IMAGE folder. Then i have a load file containg starting page and ending page numbers as BEGDOC and ENDDOC respectively. These might have a difference of 1 or 2 or 3 or any number of pages. Remember, the IMAGE folder contains files in sequence which matches the file naming convention of load file. The output should be BEGDOC,prodvolume,Imagepath,Y, Here Y reperesents the parent file that is first page. I have shown how the input loadfile looks like previously. Based on that numbers the output should contain BEGDOC in single pages along with the corresponding tif path which matches with the names.