Tons of confusion over there, eh? :-)

First of all: you are opening your database and slurping in at once in memory. It's ok if it's not too big, but in case it is and you have enough disk space I suggest you to read line by line and write to another file. If you want, you could end your script by overwriting the old file with the newly created one; check File::Copy and it's function move for details.

Second: You use double quotes around $db in your opens: that would be needed if you specify, for example, "> $db" to mean overwrite the file. In your situation double quotes are useless.

Next: your foreach cycle doesn't print on DATA, which would be then left empty!

And finally: you used a character class \s where it had no meaning in the first substitution, and you swapped the matching string with the substitute in the second.

A slightly revised (but untested!) version of your code could be:

use strict ; # now and forever! my $db = 'aaa.txt'; open DATA, "< $db" or die "did not open for reading: $!\n"; my @dat = <DATA> ; # don't need parentheses, context forced by @dat close DATA; open DATA, "> $db" or die "did not open for writing: $!\n"; foreach my $line (@dat) { # Indent, it's useful! $line =~ s/main/index/g; $line =~ s/\t/ /g; #This is not working $line =~ s/^/\a/g; #This is not working print DATA $line; } close DATA ;

Ciao!
--bronto

# Another Perl edition of a song:
# The End, by The Beatles
END {
  $you->take($love) eq $you->made($love) ;
}


In reply to Re: Substituting data by bronto
in thread Substituting data by Anonymous Monk

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.