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

I want to create a loop to search a file for a pattern, create a hash, then search the file for other occurrances of the key and createlinks using the corresponding value, then loop to the next file andrepeat the process. I have my pattern match working (although I'm notsure it's the best way for it to match) but can't seem to make anythingfurther work. There are approximately 75 files in the directory, allending with .htm.tmp. I can create an array of the files with noproblem, and print the list back to my screen. When I run the code, thetemporary files are created and the permissions are changed. I getnothing on the screen, no error messages, and no print. I am absolutelylost and have no idea how to proceed. Please help!

#!/pw/prod/svr4/perl/bin/perl -w ARGUMENTS: engine_figurelinks.pl xx_manual_vvv # where xx = manual code, vvv = version # use warnings; use diagnostics; use Env qw(SERVER_NAME); use CGI qw(:standard :netscape); use File::Copy; new CGI; ($manualdir_param) = @ARGV; $working_dir = $manualdir_param; $working_dir =~ s/manualdir=//i; $data_area = "/tmp"; $html_dir = "$data_area/$working_dir"; # Loop to locate HTML files, change permissions, and make working temp +orary copies opendir( HTMLSTORIES, "$html_dir") || die "HTML files do not exist: $1 +"; @FigureArray = grep{/\.htm$/} readdir ( HTMLSTORIES ); foreach $figfile (@FigureArray) { copy ("$html_dir/$figfile", "$html_dir/$figfile.tmp") or die " +Can not make temporary copy of file: $1"; chmod 0600, "$html_dir/$figfile"; } closedir HTMLSTORIES; # Loop to do pattern search, create hash and insert links opendir( HTMLSTORIES, "$html_dir") || die "HTML files do not exist: $1 +"; @TmpArray = grep{/\.htm.tmp$/} readdir ( HTMLSTORIES ); foreach $tmpfile (@TmpArray) { my $figures = "$html_dir/$tmpfile" =~ /^(<IMG.*BR>)/ ... /(Figure\s+\W +*)/i; print $figures;} closedir HTMLSTORIES;

Replies are listed 'Best First'.
Re: Searching on the Key Field of a Hash
by GrandFather (Saint) on Feb 23, 2006 at 20:54 UTC

    Your description is not consistent with your code. There are no hashes in your code.

    What do you expect $1 to contain in the die statements?

    You should use strict; and use my so the scope of variables is a little more obvious.

    What do you expect /(Figure\s+\W*)/i to match against? It is testing $_ which is probably undefined at that point.


    DWIM is Perl's answer to Gödel
Re: Searching on the Key Field of a Hash
by dynamo (Chaplain) on Feb 23, 2006 at 22:47 UTC
    You can create an array of the files no problem, and print the list back to your screen, but when you run the code, stuff happens with the temp files/permissions, but you get nothing on the screen, no error messages, no print.

    Those two are contradictory. Please rephrase what you mean, it's probably two separate situations where one has printing and one doesn't but it doesn't read that way.

    Also the previous poster was right that you're not creating a hash anywhere. I'd suggest creating it roughly at the same place that you currently "print $figures;".

    Does the above code print anything for you, or not?

      No, the code doesn't print anything for me. When I execute it, it brings me right back to the prompt. The temporary files are created and the permissions on the origional files are changed.

      I'm searching the file for the following lines of code:

      <IMG SRC="/CSS/tpgvoli/G-0/7/G-07932.00970601.gif">
      \nAssemble The No. 3 Bearing (Rear) Seal Ring Holder Assembly\nR Figure 1001a

      I want to capture the first line, the <IMG line, and the third line,
      the FIGURE line, and throw out the second line of text. Frankly, I
      have no idea what I'm dong at this point, I'm so lost.
      No, the code doesn't print anything for me. When I execute it, it brings me right back to the prompt.
      The temporary files are created and the permissions on the origional files are changed.

      I'm searching the file for the following lines of code:

      <IMG SRC="/CSS/tpgvoli/G-0/7/G-07932.00970601.gif">
      \nAssemble The No. 3 Bearing (Rear) Seal Ring Holder Assembly\n
      R Figure 1001a

      I want to capture the first line, the <IMG line, and the third line,
      the FIGURE line, and throw out the second line of text. The only part of the first line that is always the
      same is the <IMG SRC and the ending BR> There is not always a beginning R to start the third line and
      not always an alpha after the numeric. Frankly, I have no idea what I'm dong at this point, I'm so lost.