mkahn has asked for the wisdom of the Perl Monks concerning the following question:
I have 2 data files: One is a sql table dump, which I would like to search and replace a couple of the fields from the second file; which has a corresponding title in one field, and an updated directory path in the other.
I am reading in $title and $url, then reading in each record from the data file, testing to see if the fields match, then dumping the fields back together and writing to an output file.
The purpose here is to update a menuing system with database generated urls from the old values.
So that part is actually working. If there is a match, it replaces it and writes the file. However the menu also contains items that dont match, but still need to be rewritten back to the output file. There are menu items which do not represent a database query, but are simply static links.
This would probably be easier if I could just substitute directly in the source file (though not for development purposes..) instead of writing a new file. But thats the approach I took. What I am trying to do here is create an array of matches, then cycle through the file again, skipping any records that previously matched, write the record from any that dont, then push that index field to the matched array.
And now I must apologise for what I'm sure is blasphemous coding technique, but if you've read this far, perhaps you'll take a look at what I've got.
I'm sure there are several other things that will come up before this does what I want it to do. But it will theoretically be a bridge between 2 of joomlas components, so the shop links can reside in a customized menu.
What I should really do is integrate this with the script thats generating the menu in the first place. but for now, this is my coding lesson.
Thank you!
Here is some sample data (2 records) from the source sql table:#!usr/bin/perl open LINKFILE, "linksource.dat" || die "no such file here"; while (<LINKFILE>) { #memorize title, then url; my ($title) = /title=(".*?")/; ($title) =~ s/"/'/g; ($title) =~ + s/^\s+//g;($title) =~ s/\s+$//g; my ($url) = /(component.*")/; ($url) = '"/' . $url; ($url) =~ s/"/' +/; #print $title , "\n" , $url, ,"\n"; my $matched = 0; open SQL, "mark.sql"; while (<SQL>) { @fields = split /,/ , $_ ; foreach $i(0 .. $#fields) { s/^\s+//, s/\s+$// for $fields[$i]; #print "\$fields[$i]:$fields[$i] \n"; } print "\$fields[2]:|$fields[2]| \$title:|$title| \n" if ($title eq +"'Alternative Reds'"); next unless ($fields[2] eq $title) or ($i == $#fields); push @used, $fields[0]; ($fields[3] = $url) if ($fields[2] eq $title); my $sqlout = join "," , @fields; open NEWFILE , ">>newsql.sql"; print NEWFILE "$sqlout \n"; #print NEWFILE "\$fields[2]:$fields[2], \$title:$title \n" +; close NEWFILE; last; } } close SQL; close LINKFILE; open SQL, "mark.sql"; while (<SQL>){ @fields = split /,/ , $_ ; foreach $i(0 .. $#fields) { s/^\s+//, s/\s+$// for $fields[$i];} foreach $j(0..$#used) { $match++ && (print "$_:$fields[0]") if ($fields[0] eq $used[$j]) && ne +xt; print "\$used:|$used[$j]| \$fields[0]:|$fields[0]| \n"; my $sqlout = join "," , @fields; open NEWFILE , ">>newsql.sql"; print NEWFILE "$sqlout \n" && (push @used, $fields[0]) unless $match +; $match = 0; close NEWFILE; last; } }
These records are from the linkdata source:(6, 'usermenu', 'Upload', 'index.php?option=com_content&task=view&id=5 +', 'content_item_link', 1, 0, 5, 0, 2, 62, '2006-06-24 11:42:36', 0, +0, 2, 0, 'menu_image=-1'), (7, 'mainmenu', 'Alternative Red', 'index.php?option=com_lxmenu', 'com +ponents', -2, 2, 19, 0, 1, 62, '2006-04-23 22:47:37', 0, 0, 0, 0, '') +,
<a title="Wine Accessories" style="display:block;" class="mainlevel" h +ref="/component/page,shop.browse/category_id,1/option,com_virtuemart/ +Itemid,16/" >Wine Accessories</a> <a title="Gewurztraminer" style="display:block;" class="mainlevel" hre +f="/component/page,shop.browse/category_id,2/option,com_virtuemart/It +emid,16/" >Gewurztraminer</a>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: data management script
by chargrill (Parson) on Jul 22, 2006 at 15:17 UTC | |
by mkahn (Beadle) on Jul 22, 2006 at 16:42 UTC | |
by planetscape (Chancellor) on Jul 23, 2006 at 05:51 UTC | |
|
Re: data management script
by johngg (Canon) on Jul 22, 2006 at 23:33 UTC |