subhash1198 has asked for the wisdom of the Perl Monks concerning the following question:

I have several image files in tif format. They are placed in different folders like folder IMAGES>001,IMAGES>002,IMAGES>003 etc. I have a list of BEGDOC and ENDDOC based on that i have to create a opticon load file which gives me out BEGDOC foldername,Image path and the count of child documents and parent image being marked as Y. I have a script something like this.

use strict; #USER INPUTS print "Enter the Loadfile file which contains BegDoc and EndDoc\n"; chomp(my $in = <STDIN>); print "Enter the location of the Image file\n"; chomp(my $tiffpath = <STDIN>); print "Enter the Name of the Production\n"; chomp(my $prodvolumename = <STDIN>); my $delim = ""; my $textd = "þ"; chomp($in); my $out = $in . "\.opt"; my $header = 1; open OUT,">$out" or die "Cannot Create $out\n"; open IN, "<$in" or die "Cannot open $in\n"; while (<IN>) { chomp; next if ($header++==1); my $line = $_; $line =~ s/þ//gi; my @data = split(/$delim/,$line); my $start = $data[0]; $start =~ s/þ//gi; my $end = $data[1]; $end =~ s/þ//gi; my $snum = $start; $snum =~ s/[^0-9]+//gi; my $enum = $end; $enum =~ s/[^0-9]+//gi; my $pref = $end; $pref =~ s/[0-9]+//gi; my $pages = $enum - $snum +1; my $first = 1; for (my $i = $snum; $i <= $enum; $i++) { my $number = sprintf("%0*d",length($enum),$i); #print OUT $pref . $number . "," . $pref.$number . ",$tiffpath +\\$pref$number.tif,"; print OUT $pref . $number . "," . $prodvolumename . ",$tiffpat +h\\$pref$number.tif,"; if ($first == 1) { print OUT "Y,,,$pages\n"; $first++; } else { print OUT ",,,\n"; } } } close(IN); close(OUT);

The problem is the image path is not counting the folder in its path from where the tif file coming. EX: if tif is coming from Images/001/test.tif This script outputs Images/test.tif. Could anyone please solve this ASAP?

Replies are listed 'Best First'.
Re: Traverse directory backwards based on file
by kcott (Archbishop) on Mar 29, 2014 at 21:04 UTC

    G'day subhash1198,

    Welcome to the monastery.

    [For future reference, don't mark your questions to be dealt with ASAP, urgently or anything like that. Questions are answered fairly quickly. Suggesting that we drop what we're doing and rush to your aid won't work. At best, your question will answered just as quickly as it would have been anyway; at worst, you will annoy people who'll delay answering or not answer at all.]

    "This script outputs Images/test.tif."

    I can't see anything in your posted code that would output "Images/test.tif". Have you posted the code you're running (or perhaps an old version)?

    $number appears to be formatted in way that might produce "001":

    $ perl -le 'print sprintf("%0*d", 3, 1)' 001

    If that's the right variable, then you'll need something in your code that looks like:

    print ".../$number/..."

    If you're expecting another variable to hold "001", please tell us.

    If you're concatenating variables to generate your output, check those variables individually, e.g.

    print "number='$number'; tiffpath='$tiffpath'; ...";

    Use similar print statements elsewhere to check variables hold the values you're expecting.

    -- Ken

Re: Traverse directory backwards based on file
by kcott (Archbishop) on Mar 29, 2014 at 22:39 UTC

    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

      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