Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Substituting data

by Anonymous Monk
on Jun 28, 2002 at 17:39 UTC ( [id://178080]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I want an output of my file where I need to substitute all tabs with spaces and put \a at the beginning of each line. Please advise what I am doing wrong with the last two substitute attempts:
$db = 'aaa.txt'; open(DATA, "$db") or die "did not open: $!\n"; @dat = (<DATA>); close(DATA); open(DATA, "$db") or die "did not open part 2: $!\n"; foreach $line (@dat) { $line =~ s/main/index/g; $line =~ s/\t/\s/g; #This is not working $line =~ s/\a/^/g; #This is not working print $line; } close(DATA);

Replies are listed 'Best First'.
Re: Substituting data
by kvale (Monsignor) on Jun 28, 2002 at 17:48 UTC
    The problem is that you are mixing up regex elements with straight text. Try:
    $line =~ s/\t/ /g; $line = "\a" . $line; # simplest # $line =~ s/^/\a/; # alternative using s///
    -Mark
Re: Substituting data
by bronto (Priest) on Jun 28, 2002 at 21:17 UTC

    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) ;
    }

Re: Substituting data
by mikeirw (Pilgrim) on Jun 28, 2002 at 17:46 UTC
    Try this:
    $line =~ s/\t/ /g; $line =~ s/^/\a/;
      Thanks for the answers to my script problem.
Re: Substituting data
by fruiture (Curate) on Jun 28, 2002 at 18:35 UTC
    you seem to have mixed up the order of s/PATTERN/RELACEMENT/ but that was already told you. I just cannot stop myself from trying to give good advice ...
    Have a look at perlfaq4 'What's wrong with always quoting "$vars"'.
    I also wonder why you open the same file twice and why you do not iterate via while(<HANDLE>) which would save the memory consumed by the array @dat .
Re: Substituting data
by robobunny (Friar) on Jun 28, 2002 at 17:53 UTC
    as kvale said, unless you actually want a literal \a at the beginning, then do:
    $line =~ s/^/\\a/;
    i don't see why you'd want either one of them there, so i don't know which one you want.
Re: Substituting data
by flounder99 (Friar) on Jun 28, 2002 at 19:09 UTC
    Aside from your regexes you will want to add a > to your second open or you will not be able to write to the file. For example:
    open (FILE, ">blah.txt") or die; print (FILE "1 blah\n2 blah\n3 blah\n") or warn "could not print"; close FILE; open (FILE, "blah.txt") or die; # NOTE: no > print (FILE "new data\n") or warn "could not print"; close FILE; open (FILE, "<blah.txt") or die; print <FILE>, "\n"; close FILE; __OUTPUT__ could not print at temp.pl line 5. 1 blah 2 blah 3 blah

    --

    flounder

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-19 23:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found