Hello Monks,

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!

#!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; } }
Here is some sample data (2 records) from the source sql table:
(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, '') +,
These records are from the linkdata source:
<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>

READMORE added by Arunbear


In reply to data management script by mkahn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.