this pretty much outlines the structure of the program....so writing it is a fairly simple process of following the above structure.
personally, i would make the backup copy of the files and change the permissions as a separate task (just on the general principle that a tool like this should only do one thing so that it can be re-used easily - and also so that you can run it while testing it WITHOUT making any changes to the files/directories on disk), but you can do it within the perl script if you want.
once you have the IMG SRC urls in a hash, you can do whatever you want with them, including printing them out as an <A HREF="..."> HTML link.
note: that point about read-only testing is an important one. it's one of the many reasons why it can be a good idea to write tools like this as a filter (i.e. input on stdin, output on stdout). if the program doesn't actually change the input files in any way then development can be an iterative process of hack and fix. also, without hard-coded directory/file names, you can run your program on a backup copy of the data while developing it. keeping your original data safe allows you to take risks with the backup that you can't afford to take with the original - if you mess it up, just take another copy and try again.#! /usr/bin/perl -w use strict; use File::Copy; # pass directory to scan as arg1 (default to current dir) my $dir = shift || "./" ; # get list of non-hidden files in directory opendir(DIR, $dir) || die "can't opendir $dir $!"; my @files = grep { /^[^.]/ && -f "$dir/$_" } readdir(DIR); closedir DIR; my %images = (); # process each file foreach my $file (@files) { next if ($file =~ /\.pl/) ; # skip perl program files copy($file, "$file.bak"); chmod 0600, $file; my $img = ''; my $fig = ''; open(FH,"<$file") || die "couldn't open $file for read: $!\n"; while (<FH>) { chomp ; s/^\s*|\s*$//g; # strip leading and trailing spaces if (/<IMG SRC/) { $img = $_ ; } elsif (/Figure\s+\d+/) { $fig = $_ ; } ; } ; close(FH); # if we found an IMG SRC line *AND* a Figure line, then # add it to the images hash. if ($img && $fig) { $images{$fig} = $img } ; }; foreach (sort keys %images) { print "$_ : $images{$_}\n" ; };
In reply to Re: Regular Expression Pattern Search Problem
by cas2006
in thread Regular Expression Pattern Search Problem
by xdbd063
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |