Re: editing a file using perl script



In article <43B98631.9040706@xxxxxxxx>, kelly <kelly@xxxxxxxx> wrote:

> Hi,
> I don't have a code to show you, what I need are references or
> algorithms so that I'm in a right track.
>
> By the way, thanks for introducing me the array or arrays. Now I can
> continue my script.

Whom are you thanking? Please include some context.

> Now I want to delete a line from a file. Line being the strings I
> got/saved to/from array of arrays.
>
> This website tells me I have to open my file and create a new file and
> put my changes to the new file.
> http://www.rocketaware.com/perl/perlfaq5/How_do_I_change_one_line_in_a_fi.htm

The first method on this web page is a typical way of modifying a file.
You should try using it on your file. You should write out your
modified file using a new name and don't rename it or overwrite the
existing file. Inspect the contents of the modified file manually until
you are satisfied that it is correct before coding your program to
overwrite the contents of your existing file.

>
> So here's what I have in mind, or questions on what should I do?
> -create a new blank file
> -open my existing file where it is full of strings with spaces, \n...etc.
> -where do I make my changes?

Make your changes in the strings that contain the lines from the old
file after you have read them in but before you write them out.

> -how will all the strings from existing file gets copied to new file?

You will write out the lines using the print statement.

> -will I need to save all the existing file strings to array of array or
> just array? what will be my separator?

If you read each line and then write it out, you needn't save it. You
would only save it if you needed to change more than one line at a time
or for some other reason.

> -then when do I do the copying to new file? Before of after deletion of
> strings from my array of arrays?

You don't need an array of arrays to modify a file. You only need one
scalar variable to hold each line, as demonstrated by the solution
given on your referenced web page.

> -when or where can I do the deletion?

You can delete a line by not writing it to the modified file.

>
> Sorry about all the questions? I just can't start coding this if I don't
> know what to do. Please feel free to tell me websites or which perldoc
> I would be needing to research on.

You are confusing two different methods for modifying a file. One is to
read the lines of the file into an array (not an array-of-arrays),
modify the array, then write out the array to a modified file. The
other method is to read one line at a time from the old file, modify
the line if necessary, and write the line out to the file if it should
be kept.

Pick one method and stick with it. The first method will have less
memory usage. The second method allows you to modify more than one line
at a time. Use whichever method suits your purpose.

The first method may be implemented using the Tie::File module, as
Jürgen has recommended. However, I recommend you implement your own
method until you understand programming in Perl better.

See the following for more info:

perldoc -f open
perldoc perlop (and search for "I/O Operators")

FYI: this newsgroup is defunct. Try comp.lang.perl.misc in the future.

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
.



Relevant Pages

  • Re: passing parameters by reference
    ... Try thinking of it this way: everything in Lisp is passed ... If you passed in an array, any changes you make to array elements ... and see that it does not necessarily modify its argument. ... A macro call looks syntactically ...
    (comp.lang.lisp)
  • Re: SSCANF
    ... This gives undefined behavior. ... You're trying to modify ... in this context will evalutate to pointers to their ... array of three char). ...
    (comp.lang.c)
  • Re: Problem in writing to a property node of a cluster
    ... I took the liberty to modify your VI for an alternative approach. ... You should keep your array of 32 clusters in a shift register and show only a single cluster as a front panel control. ... - Selecting a different transducer from the listbox on the left will load its settings into that control via a local variable. ...
    (comp.lang.labview)
  • Re: How to modify the proxyAdress property in AD with VBScript
    ... proxyAdress property array including an smtp prefix. ... How do i modify a property inside the array with vbscript. ... Dim strOldAddress, strNewAddress, blnFound, blnSkip ...
    (microsoft.public.windows.server.scripting)
  • Re: why do strcpy, strcat, & co return a pointer ?
    ... > does modify the p object, which is an array of 100 characters. ... meaning of my original claim should've been clear from the context, ... IG> a pointer to its first element. ...
    (comp.lang.c)