Re: Rename File Using Strring Found in File?



He Who Greets With Fire <Entwadumayla@xxxxxxxxxxxxxxx> wrote:
On Tue, 4 Mar 2008 16:15:20 +0000, Ben Morrow <ben@xxxxxxxxxxxx>
wrote:
Quoth He Who Greets With Fire <Entwadumayla@xxxxxxxxxxxxxxx>:

OK, thanks, but the script does not seem to rename the files.
^^^^^^^
^^^^^^^
Eh?

I added some troubleshooting code, most of which I commented out. I
also moved a copy of the personalinjury folder and all its files
inside the C:\Perl directory so it can access it directly.

Don't do that. You can set the working directory from within your Perl
script using the chdir function. In any case, the working directory may
not be what you expect under Win32.


the directory/folder location is not a problem.


Exactly so. That is why you should not do that.

Your "current working directory" and the directory that your perl
executable are in are not the same thing.

The directory where your perl binary lives has no connection
whatsoever to accessing files so moving files under there
will not solve file accessing problems.

Your cwd is what matters with regard to filesystem access.


#!/bin/perl

Perl is *never* installed as /bin/perl.

but it already works in that regard--the script executes


The fact that your script executes does not prove that perl
is installed as /bin/perl.

(Windows programs use some other mechanizm for associating files).

You should either use a place where perl is usually installed, eg:

#!/usr/bin/perl

or simply

#!perl

if you want to use command line switches, or even

(nothing)

leave that line out completely.


#sleep 2;


Why do you think that calling sleep() will help with debugging?


print "here I am! \n";

Diagnostics like this are better given with warn, which will .a. print
them to STDERR, where they ought to be and .b. tell you where you are in
the script.

Well, I'm not actually a programmer,


You will need to become a bit of a programmer if you hope to
write a bit of a program.


just someone trying to do some
organization of my files.


If you need some programming done, and you want to do it yourself,
then you are going to have to learn some programming.


So, that issue is not a concern right now.


so the issue of how to debug programs should be of ultimate concern
right now, since now is when you have a program that you need to debug!

Error and warning messages should go on STDERR, not STDOUT.


foreach my $file ( glob 'personalinjury/*.htm' ) {

# print "here I am A \n";


The text of the debugging message should tell you where it is in
the program rather than requiring you to search in the program
to find where it is. It also give you a chance to examine the
data that you are operating on:

warn "processing '$file' inside the foreach loop\n";


open my $PI, '<', $file or die "could not open '$file' $!";

# print "here I am! B \n";


warn "succeeded in opening $file\n";


while ( <$PI> ) {
# print "\n inside whileloop";


warn "processing '$_' inside the while loop\n";


I AM getting to this point here.

next unless /Citation: [\d-]+.*([\d.]+)/;

but I never get to this point here--apparently the regex never sees a
match for the "Citation:" etc string.


Then you should modify the regex so that is sees a match for
the "Citation:" etc string.

To do that, you need to know *exactly* what the data looks like,
and you probably need to read some of the standard documentation
that covers regexes.


Here is a screen shot of the typical file, with a red arrow pointing
to the string in this particular file that I want to match.
I do not know why the regex does not see a match, because it looks
like it matches it???

See here:
http://img225.imageshack.us/img225/91/citationue2.jpg

*DON'T* do that.

Don't do what?


Don't post a screenshot. Your program is processing text, not graphics.

Don't post a URL and expect people to go follow it to find out what
you are talking about.


Had you done the right thing, and copy-pasted a small
section of the relevant file into your message,


Do post a copy-pasted section of the data into your message.


you would have found
that the file doesn't in fact contain the string 'Citation: whatever' at
all. It's an HTML file, so there is markup in there as well, and the
string may well be spread across several lines. Get into the habit of
looking at files in a text editor before you try parsing them with Perl.

That is a good point.


Well duh.

When crafting a regular expression, it is *essential* to know *exactly*
what the data you are trying to match looks like.


Here are two excerpts from the source html for a typical file in that
folder:


<td class="toolbar" align=right valign=top width="1%"
nowrap>Citation:&nbsp;&nbsp;</td>
<td class="toolbar" valign=top width="99%"><b>21-340 Dorsaneo, Texas
Litigation Guide § 340.02</b></td>


If the data you want is in an HTML table, then you should use
a module that will process an HTML table for you, such
as HTML::TableExtract.


Are the angle brackets special characters in perl so that they have to
be backslashed inside the regex?


Yes, angle brackets are special characters in Perl, they mean
"less than" and "greater than" and whatnot.

No, angle brackets are not special characters in a regular
expression, so they do not need to be backslashed.

The Perl Language and the Regular Expression Language are different
languages, so the funny characters mean different things depending
on which language you are in.


I wonder if this regex would work?


The way to answer that is to write a teeny tiny program
and *see* for yourself it it works or not.


my $newfile = $1;
rename $file, "$newfile.htm" or die "could not mv '$file' $!";
print "\n renamed a file";
sleep 1;
last;
}#end while

If you had used proper indentation, you would be able to see that
comments like this are completely useless.

Not sure what you mean?


Me either.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
.



Relevant Pages

  • Re: How do i convert text string to hex values as string?
    ... addition is faster than concatenation" is a valid justification for the use ... because QuickBASIC had by far the best string handling ... middle-level language, such as C. ... More in point, however, VBS is a utility script designed for relatively ...
    (microsoft.public.scripting.vbscript)
  • my script crashes when I try to rename the file!
    ... OK, thanks, but the script does not seem to rename the files. ... You can set the working directory from within your Perl ... # sleep 1; ... to the string in this particular file that I want to match. ...
    (perl.beginners)
  • Re: Perl script to extract data from webpage? (knucklehead newbie).
    ... Your $& is a special perl variable that represents the string matched by ... the last successful pattern match...which in the case of your example ... I have this running now using a script I found elsewhere, ...
    (comp.lang.perl)
  • Re: How to pass string in command line argument.
    ... Perl Pra wrote: ... i have perl script that searches given string (the string should be passed ... The string should be sent to the script with double quotes attached to it ...
    (perl.beginners)
  • Re: dictionary/hash and 1 versus 1
    ... In order to keep things simple, Perl's string operators ... programming language you have to use its idioms or you end up fighting ... IMO, since Python is strongly but dynamically typed, a) is the 'worst' ... Perl and Python. ...
    (comp.lang.python)