CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Alter contents between tags

 



fds
Novice

Aug 17, 2011, 12:41 AM

Post #1 of 8 (1891 views)
Alter contents between tags Can't Post

Hi guys, I'm not getting anywhere on this. Spent hours looking but not found anything except how to strip tags etc.

It's something I'm only going to do once and that's it.

I have several hundred files which have one or more tags like this in each one:

Code
<date>27/09/2005</date>


Obviously dates are all different. What I want to do is change them all to this format:

Code
<date>27.09.2005</date>


I can get to the files, but beyond that I don't know.

Code
use strict; 
use warnings;
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
print "Content-type: text/html\n\n";

$ENV{'SCRIPT_FILENAME'} =~ m|(.*)/(.*)$|; my $thisdir = $1;
my $dir = "$thisdir/files";

my @files = <$dir/*>;
foreach my $file (@files) {
$file =~ s#.*/##;
print $file . "<br>\n";

my @fto = "$file";
foreach my $fop (@fto) {
open (DFIL,"$dir/$fop") || die("Cannot open $fop");
while (<DFIL>) {

# Can open and read them (so I am seeing them),
# that's as far as I've got.
print $_, "<br>";

}
close (DFIL);
}
}


This will be done locally on my PC under Indigo Perl (Apache/Perl).
Hope someone can help
----------------------------------------------
fdsaadsfdsaf


rovf
Veteran

Aug 17, 2011, 3:27 AM

Post #2 of 8 (1887 views)
Re: [Ted] Alter contents between tags [In reply to] Can't Post

I don't understand why you add '<BR>' when outputting the changed file, if all what you want is to change the one line containing the date, however...

There are two things to consider:

(1) You need to modify an existing file.

The usual approach is that you read the original file, write to a new (modified) file, and (after closing) move the new file over the old one.

Your case is however special, in that you alternatively also could edit the file in-place (since only one character is replaced by a different character). If you want to try this approach, open the file in binmode, read the whole file at once in a variable, do the substitution, go back to the beginning of the file (see the function 'seek') and write back the file.

(2) You need to find the <date> line.

The usual recommendation here is to use a HTML parser. However, if you know for sure that your files have a pretty regular format, which makes it easy to identify the <data> entry, you could use either

- the index function to search for the entry (and use substr to replace the date separators), or

- Use a regular expression together with the s/// operator.

So just use whichever approach looks more familiar to you.


davidbj
Novice

Aug 17, 2011, 3:43 AM

Post #3 of 8 (1886 views)
Re: [Ted] Alter contents between tags [In reply to] Can't Post

Please, explain what you want to do with more details.
This is a sample.

Code
#!/usr/bin/perl  

use Modern::Perl;
use autodie;

open FILE, q[<], 'file.txt';
open FILE2, q[>>], 'fileout.txt';
while(my $line=<FILE>){
chomp($line);
$_ =~ s/\//\./g;
print FILE2 "$_\n";
}
close FILE;
close FILE2;



(This post was edited by davidbj on Aug 17, 2011, 4:16 AM)


fds
Novice

Aug 17, 2011, 5:36 AM

Post #4 of 8 (1879 views)
Re: [rovf] Alter contents between tags [In reply to] Can't Post


Quote
I don't understand why you add '<BR>'

that was just so I knew I could see the files. Didn't mean to confuse - sorry.

Not sure what "binmode" is.

I'll check out davidbj's method first (if I can find the modules to install on my local testbed server - it's on windows machine) and get back to you both.
----------------------------------------------
fdsaadsfdsaf

(This post was edited by Ted on Aug 17, 2011, 5:36 AM)


rovf
Veteran

Aug 17, 2011, 6:00 AM

Post #5 of 8 (1875 views)
Re: [Ted] Alter contents between tags [In reply to] Can't Post


Quote
Not sure what "binmode" is.


Only relevant if you are on Windows.

Usually, line endings are translated when reading and writing files, because Windows uses a two-byte-sequence as line terminator (0x0d0a). If you binmode a file handle immediately after opening the file, this translation is suppressed.

If you want to position in a file at random, it is in general a good idea to put the file handle into binmode.

There *are* cases where this is not strictly necessary, and as far I can see, a line ending translation would not hurt in your particular case. But since I don't know in what other way you will modify your files in the future, I thought it is a good idea to advise you about binmode.


fds
Novice

Aug 17, 2011, 10:09 AM

Post #6 of 8 (1866 views)
Re: [davidbj] Alter contents between tags [In reply to] Can't Post

David, those modules are not available for me to install under Indigo Perl.

I'll try something else, thanks anyways - Ted
----------------------------------------------
fdsaadsfdsaf


davidbj
Novice

Aug 17, 2011, 12:45 PM

Post #7 of 8 (1861 views)
Re: [Ted] Alter contents between tags [In reply to] Can't Post

Ted, use the strict pragma only,
you don't need Modern::Perl or autodie, and these modules are available only in linux.

Use Modern::Perl is like write:


Code
use strict; 
use warnings;
use feature qw(say state switch);


Autodie functionality is that if the script fails at any point, the program automatically dies.


fds
Novice

Aug 17, 2011, 1:58 PM

Post #8 of 8 (1856 views)
Re: [davidbj] Alter contents between tags [In reply to] Can't Post

No worries.

Would you believe I just had an email from the guy I was doing it for and he's decided to keep stuff the way it is!

Sorry to have bothered anyone. If he doesn't want it I'm not going to spend any more time on it.

Thanks anyways both of you.
----------------------------------------------
fdsaadsfdsaf

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives