Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^3: array of hashes help

by rjt (Curate)
on Apr 23, 2014 at 22:23 UTC ( [id://1083477]=note: print w/replies, xml ) Need Help??


in reply to Re^2: array of hashes help
in thread array of hashes help

Ok, that helps. If your files are indeed line-based (i.e., your patterns do not span multiple lines), then I suggest processing them line-by-line:

use File::Slurp; my @myHashArray = ( { toFind => qr/(?i)foo/, toReplace => 'bar' } ); my $counter = 0; my $name = 'data_file'; # $File::Find::name for my $line (read_file('data_file', chomp => 1)) { $counter++; my $orig_line = $line; $line =~ s/$_->{toFind}/$_->{toReplace}/g for @myHashArray; next if $line eq $orig_line; print "LOG: $counter $name\n"; print "LOG: \t Original line: $orig_line\n"; print "LOG: \t Replaced line: $line\n"; }

Here I've replaced LOGFILE with a LOG: prefix for ease of testing, and eliminated the reliance on File::Find for the same reason.

With the following data_file:

Foolius Caesar Footholomew Cubbins No changes here Just another barrage of text

I get the following output:

LOG: 1 data_file LOG: Original line: Foolius Caesar LOG: Replaced line: barlius Caesar LOG: 2 data_file LOG: Original line: Footholomew Cubbins LOG: Replaced line: bartholomew Cubbins

Does that help?

use strict; use warnings; omitted for brevity.

Replies are listed 'Best First'.
Re^4: array of hashes help
by rv799cv (Initiate) on Apr 23, 2014 at 23:04 UTC

    thank you so much for your help and do very much appreciated

    The problem is that my input files are XML and I have to search for multiple lines

    Here is my entire code, I have removed many of values from @myHashArray and just left one item

    #!/usr/bin/perl ############### ## Libraries ## ############### use File::Copy; use File::Find; use File::Path; use File::Basename; use File::stat; use strict; use warnings; use Time::localtime; ################## ## User-defined ## ################## #--check for rptCustomAssembly word my $find = "CustomAssembly"; #--the root location of files to start reading (this can be our ShareP +oint site) my $rootLocationOfFiles = 'C:\rptTest'; #--the name and location of log file my $logFile = '>C:\rptDest\logRptFindAndReplace.txt'; #--get the start timestamp my $currentTime = time; #--Open a logfile to write the file names open (LOGFILE, $logFile); #--get current timestamp my $startTimeStamp = '[' . timestamp() . ']'; #--write to our logfile print LOGFILE "------------------------------------------------------- +------------\n"; print LOGFILE "Start time: $startTimeStamp\n"; print LOGFILE "Please Note:\n"; print LOGFILE "List of reports that are modified by script.\n"; print LOGFILE "------------------------------------------------------- +------------\n\n"; #----------------------------------------------------- #--Find and replace $source with $target if CustomAssembly is not refe +renced in report file my $source = '<Language>en-US</Language>'; my $target = '<Language>en-US</Language> <CodeModules> <CodeModule>CustomAssembly, Version=1.0.0.1, Culture=neutral, Publ +icKeyToken=null</CodeModule> </CodeModules>'; use File::Find qw(finddepth); my @files; my $counter = 0; my @myHashArray = ( {toFind => '<Image Name="Logo"> <Source>Embedded</Source>', toReplace => '<Image Name="Logo"> <Source>External</Source>' } ); #--END @myHashArray finddepth(sub { return if($_ eq '.' || $_ eq '..'); my ($dir, $name, $ext) = fileparse($File::Find::name,'.rdl'); my $fileName = $_; chop($name); my $fullFileName = "$name\\$fileName"; my $Dest = "$rootLocationOfFiles\\$fileName"; #--read only .rdl files if($ext eq '.rdl') { my $data = read_file($File::Find::name); for ($data) { if ($_ !~ /$find/) { $data =~ s/$source/$target/g; $data =~ s/$_->{toFind}/$_->{toReplace}/g foreach (@myHash +Array); write_file($File::Find::name, $data); #--write to the fi +le print LOGFILE "$counter $File::Find::name\n"; print LOGFILE "\t Original line: HERE WOULD BE MY ORIGINAL + LINE\n"; print LOGFILE "\t Replaced Line: HERE WOULD BE THE NEW LIN +E\n"; print "$counter $File::Find::name\n"; $counter = $counter + 1; #system("copy \"$fullFileName\" \"$Dest\""); #--copy fil +es } else { $counter = $counter + 1; $data =~ s/$_->{toFind}/$_->{toReplace}/g foreach (@myHash +Array); write_file($File::Find::name, $data); print LOGFILE "$counter $File::Find::name\n"; print LOGFILE "\t Original line: HERE WOULD BE MY ORIGINAL + LINE\n"; print LOGFILE "\t Replaced Line: HERE WOULD BE THE NEW LIN +E\n"; print "$counter $File::Find::name\n"; #print "$_->{toFind} : $_->{toReplace}\n" foreach (@myHash +Array); } } } }, $rootLocationOfFiles); my $endTimeStamp = '[' . timestamp() . ']'; my $totalSeconds = time - $currentTime; my $totalMins = ($totalSeconds/60)%60; my $totalSec = $totalSeconds%60; print LOGFILE "------------------------------------------------------- +------------\n"; print LOGFILE "\n"; print LOGFILE "Program ended at: $endTimeStamp\n"; print LOGFILE "It took $totalMins minute(s) and $totalSec seconds\n"; print "DONE, it took $totalMins minute(s) and $totalSec seconds\n"; close (LOGFILE); #----------------------------------------------------- #--Read the file sub read_file { my ($filename) = @_; open my $in, '<:encoding(UTF-8)', $filename or die "Could not open + '$filename' for reading $!"; local $/ = undef; my $all = <$in>; close $in; return $all; } #----------------------------------------------------- #--Write the file back sub write_file { my ($filename, $content) = @_; open my $out, '>:encoding(UTF-8)', $filename or die "Could not ope +n '$filename' for writing $!";; print $out $content; close $out; return; } #----------------------------------------------------- #--get timestamp sub timestamp { my $t = localtime; return sprintf( "%04d-%02d-%02d at %02d:%02d:%02d", $t->year + 1900, $t->mon + 1, $t->mday, $t->hour, $t->min, $t->sec ); } #-----------------------------------------------------

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1083477]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2024-04-19 15:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found