Hi Trina.

Trina Espinoza wrote:
> I have a file that I want to run multiple actions on. First, the file is
> read in as one long string. I want to
> substitute some words with others so I run a substitution. That seems to
> change the data in the first
> while, HOWEVER, when I run the second while loop on the same data to parse
> my newly reformated file, I get the original file, instead of the newly
> reformated file. . .
>
> Something like the code I'm using:
> -----------------------------
> open(SOURCE, $file);
> ###Run a substitution that breaks up my string into lines
> while (<SOURCE>) {
>      s/something/\nsomething/g;
>
> }

This doesn't read in the file as one long string: it reads individual
records into $_ and changes them without saving the changes.

> #What I want it to parse a file, which at this point, shoud consist of
> multiple lines.
> #HOWEVER problem is I amI running this command on the same file
>
> while (<SOURCE>) {
>    parse SOURCE;
> }

Well you certainly don't want that, as SOURCE is a filehandle and,
since you've just read to the end of that file you won't get any more
data out of it! Also you would use either

  while (<SOURCE>) { parse $_ }

to parse it line by line, or

  parse SOURCE

if the 'parse' subroutine expected a filehandle.

> ----------------------------------------------
> Is the best thing to  save the outcome of the substitution into a variable
> and read that variable in on the
> second while or is there an easier way?  Thanks in advance for the help :)

It depends a lot on what 'parse' does. Can it process the file in lines
or does it need the whole thing at once? If it will take the input line
by line, then why not just write:

  while (<SOURCE>) {
    s/something/\nsomething/g;
    parse($_);
  }

On the other hand, are your 'something's line terminators which
replace the usual "\n" in which case you can read the file
in a different way to avoid the substitution.

If your file is guaranteed to be only a few Mb in size you can
read it into a single string with:

  my $file_data = do {
    local $/;
    <SOURCE>;
  };

  $file_data =~ s/something/\nsomething/g;
  parse($file_data);

It would help to know a little about your application.

HTH,

Rob



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to