Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing to a File 2

Status
Not open for further replies.

jriggs420

Programmer
Sep 30, 2005
116
US
Hello again, I know that it's possible to create a file, and then write too it fairly easily in perl. But what about writing to a file that already exists, I have I notion that this must be possible. I'd like to take it a step further; Is it possible to write to a specified spot in a file. If so, is the write location determined by linenum or by regexs, or (hopefully) either/both.

I know that you can overwrite any/all text w/ perl by using the '>file' (or '>>file' to append) open method, but what about just overwrtting certain specified lines? Any generic code or links would be must appreciated, TIA,

Average Joe user.

A clever person solves a problem.
A wise person avoids it.

-- Einstein
 
That sounds like a good idea but munging and spurting sounds kind of messy! :)
 
I think we're discovering more than we needed to know about the way rharsh's mind works :)

Mike

You cannot really appreciate Dilbert unless you've read it in the
original Klingon.

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
As I may or may not have mentioned, I am a novice, so the following may seem totally illogical, or downright stoopid - but it's how I solved my problem w/perl. I'm using the Text::Wrap module to format the first (i.e. these are some cool people -- in the actual data it could be upto 10 times longer tho) string. This part works perfectly.
For the following (non-long) strings I need to run 3 separate, unique s/ // type regexes (First line--Ones in the Middle--the Last line). The only way I know for perl to distinguish first middle and last strings is to put them into an array as a group. I have no way of knowing how many short strings there are before hand. Well that seems clear as mud to me.
And then I need to repeat the process up to 10 times, each time using new data refreshing if you will. Suggestions?

A clever person solves a problem.
A wise person avoids it.

-- Einstein
 
nope - read that three times and still don't get it. Can you post some sample data?

f

["]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.["]
--Maur
 
___INDATA_____
1. Here is the first long string my program will come across when reading input. It needs to be wrapped and formatted, so I use the Text::Wrap Module to handle this string.
- Here is a subsequent string
- These don't need to be wrapped
- They each need unique foramtting
- Based on whether they are
- 1st, middle, or last
.....repeat the process, same format: 1 long followed by several short

___OUTDATA______
paragraph 1 ++Here is the first long string my+
+ program will come across when reading+
+ input. It needs to be wrapped and+
+ formatted....etc..................+
++Here is a subseqent string+(2 plusses at beginning)
+These don't need to be wrapped+
+They each need unique formatting+
+....etc.......+
+1st, middle, or last++(two plusse at end on last string)

The actual formatting isn't where my problem is. I can't figure out how to get the last ++ on the short string, and go on to the next long string. I can't use eof because it may or may not be there, this loop can repeated several times. I can post code if you'd like, but really I am looking for an alternate approach, since seems like it may be close, but no cigar. Thanks for your continued help f
-Joe

A clever person solves a problem.
A wise person avoids it.

-- Einstein
 
I'm starting to understand. How do you differentiate the long lines in the input data? Is it simply by length or do they have some other defining characteristic?

f

["]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.["]
--Maur
 
Only the long strings will begin with a number. I have found some code else where which does exactly what I was tring to do (with some modifications). If you are curious to see it I can post it.
-Joe

A clever person solves a problem.
A wise person avoids it.

-- Einstein
 
How about something like
Code:
my @dataset;
while(<>) {
  if ( /^\d/ ) {         # found start of new dataset
    if ( @dataset ) {    # so this one's complete
      process_buf( @dataset );
      @dataset = ( );    # start a fresh one
    }
  push @dataset , $_;
}
process_buf( @dataset ); # a bit ugly?

sub process_buf {
  # do exciting things with
  my $long = shift;      # the first one
  my $last = pop;        # the last one
  my @middle = @_;       # the rest
}

I'm not ecstatic about it but it would work.

Yours,

f

[&quot;]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.[&quot;]
--Maur
 
surprisingly close to what I found:
Code:
my $heading = '';
my @lines;

while (my $line  = <DATA>)
  {
  chomp $line;
  if ($line =~ /^[1-9]/)
    {#New heading line
    ProcessEntry ($heading, @lines) if length $heading;
    $heading = $line;
    @lines = ();
    }
  else
    {push @lines, $line;}
  }

ProcessEntry ($heading, @lines) if length $heading;
  
sub ProcessEntry
{
my $heading = shift;
my @lines = @_;

print "Heading is: $heading\n";
print "Lines are:\n   " . join "\n   ", @lines;
print "\n\n";
}

A clever person solves a problem.
A wise person avoids it.

-- Einstein
 
Extraordinary!

I can't see the immediate use of $heading and it doesn't treat the last line of each group differently (as you wished) but, other than that, it's pretty much the same code.

However, mine's prettier.

Lots prettier. [medal]

f

[&quot;]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.[&quot;]
--Maur
 
Yup you are right about $heading, unfortunately I didn't realize until I'd wasted a bunch of time trying to figure out why my data is fubar'd. Seems like I'm spending more time trying to tweak the code than actual programming. It's gotten pretty bulky, which is probably a key indication that I am going about this problem the wrong way. I am catching up on arrays since they seem more promising on this scenario, i.e. feeding all input into an array and then trying to thrash out the parts that need formatting. I've already got a new thread on arrays going, which as probably where I'll be posting as the above method seems inefficient at best. But thanks for the continued advice nonetheless.

A clever person solves a problem.
A wise person avoids it.

-- Einstein
 
OK, fish, I have been working with the code you have posted above. I was wondering if you would mind answering a couple questions about it. 1) It seems to be missing a curly bracket somewhere, not 100% where the best place to put it would be. 2) What exactly is @dataset equal to? The entire input file, or a small part of it over and over again. 3) I find shift and pop particularly confusing, but if I can get the script going at least I can tinker with them and figure that much out...
I'm not criticizing the code, but I just wanted to make sure you understood what I am trying to accomplish here. In simplest terms. Put data from an input file into an array if it meets a condition (pattern match), continue to do so until a 2nd condition is met (pattern match). Do some stuff, print to outfile, empty array. Find the next incidence in the input file, and repeat the process until infile is exhausted. Not slurping, not globbing, what is this called? Thanks again for your help-

Joe

A clever person solves a problem.
A wise person avoids it.

-- Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top