Hi,

I have a data file in which I need to delete a single entry value. The value will change, but its location in the file will always be the same.

Here is the section of the data file:

4.62399E+09 2.90871E+09 7.17338E+14
sb1 1.0 142r

I want to delete the 7.17338E+14 (or whatever is at that location. In my code I first open this file and find the previous line above sb1 ($oldine and $newline stuff)

#!usr/bin/perl use warnings; use strict; # my ($line)= "4.62399E+09 2.90871E+09 7.17338E+14"; my ($oldline) = "4.62399E+09 2.90871E+09 7.17338E+14"; my ($newline) = "4.62399E+09 2.90871E+09 "; # open (IN, "sdef"); while (<IN>) { if ($_ =~ /sb1 1.0 142r/ ) { $oldline = substr($line, 10, 38); $newline = substr($oldline, 0, 26); } else { $line = $_; } } close(IN); # chomp($oldline); chomp($newline); print "new = $newline\n"; print "old = $oldline\n"; # open (IN, "sdef"); open (OUT, ">sdef_steel") or die "Cannot open file for writing: + $+!" ; while (<IN>) { s/si1 L 1000 1000 /si1 L 1000 /; # works ok s/2150 2151 1035/2150 2151 /; # works ok s/$oldline/$newline/; # doesn't work s/sp1 D (\d+.\d+E[-+]\d+) /sp1 D /; # works ok print OUT $_; } close (IN); close (OUT);

Upon running, I get:

perl test.pl
new = 4.62399E+09 2.90871E+09
old = 4.62399E+09 2.90871E+09 7.17338E+14

Okay, so far so good. But if I grep the new file sdef_steel for 7.17338E+14, it still exists in the sdef_steel file.
It is not recognizing my $oldline string. I think the problem is how I am declaring it.
I have tried both my($oldline) = "txtxxt"; and my($oldline) = "1.234". Of course, the exponential is a combo.
I had thought declaring it as text (string) would be the best method.
So, what is the best way to handle subsitution of exponentials in a single data line?


In reply to how to read in a line containing exponentials by pattobw

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.