editing a file using per script

editing a file using per script

am 02.01.2006 20:58:45 von kelly

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.

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

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?
-how will all the strings from existing file gets copied to new file?
-will I need to save all the existing file strings to array of array or
just array? what will be my separator?
-then when do I do the copying to new file? Before of after deletion of
strings from my array of arrays?
-when or where can I do the deletion?

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.

Thanks.

Re: editing a file using per script

am 02.01.2006 21:45:27 von Paul Lalli

kelly wrote:
> 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.
>
> 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

That FAQ page is very out of date. You should probably use the
"official" FAQ, which these days has a completely different answer:
use Tie::File;
http://perldoc.perl.org/perlfaq5.html#How-do-I-change-one-li ne-in-a-file/delete-a-line-in-a-file/insert-a-line-in-the-mi ddle-of-a-file/append-to-the-beginning-of-a-file%3F

#!/usr/bin/perl
use strict;
use warnings;
use Tie::File;

tie my @lines, 'Tie::File', 'file.txt';

@lines = grep { ! want_to_delete($_) } @lines;

untie @lines;
__END__

The definition of &want_to_delete is up to you.

However, if you insist on doing things the hard way, I'll attempt to
answer your below questions...

> So here's what I have in mind, or questions on what should I do?
> -create a new blank file

Yes, open a new file for writing.

> -open my existing file where it is full of strings with spaces, \n...etc.

yes, open the file for reading.

> -where do I make my changes?

You loop through each line of the file as you're reading it. If you're
just looking to delete lines, test the line to see if it should be
included or not. If you're looking to modify lines, modify the line
however you choose.

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

After you've either modified the line or determined that you still want
it in the new file, print that line to the opened new file.

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

No and no. Do not save them at all. Just loop through them.
while (my $line = ) {
# . . .
}

> what will be my separator?

Whatever the line ending on your machine is. Perl takes care of this
for you.

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

You do not store anything in an array, and certainly not in an array of
arrays. Simply print each line (or not) as to the new file as you read
it from the original file.

> -when or where can I do the deletion?

As you read each line

> 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.

If you really feel the need to do this the hard way (and I can't for
the life of me understand why you'd want to), here is a framework:

#!/usr/bin/perl
use strict;
use warnings;

open my $orig_fh, '<', 'original.txt' or die "Could not open original:
$!";
open my $new_fh '>', 'new.txt' or die "Could not open new: $!";
while (my $line = <$orig_fh>){
if (!want_to_delete($line)){
print $new_fh $line;
}
}
close $orig_fh or die "Could not close original: $!";
close $new_fh or die "Could not close new: $!";
__END__

But again, use File::Tie. It's easier, it's shorter, and it's a
standard module.

Paul Lalli

Re: editing a file using per script

am 02.01.2006 21:48:39 von Paul Lalli

Paul Lalli wrote:
> But again, use File::Tie.

Tie::File, I meant of course.

Paul Lalli