Ask a Question related to PERL Beginners, Design and Development.
-
Shaunn Johnson #1
breaking the loop
Howdy:
I'm trying to find the best solution for breaking
out of a loop when editing a list of files.
I have a script that:
* gets a list of files
* opens the files with a 'foreach $f(@list)'
* does a 'while <> { ... } close (FILE)'
The script works, but if I run it, it loops
continually and edits (then re-edits) the files.
I think I could use a lock file to do until I
get a particular file, but I'm not sure how
to go about it - at the same time, it may not be
the more productive way to go.
[--snip script--]
my @list = grep {/\.txt/ } readdir(DIR) or die "Can not read the dir\n";
# create a loop to search for instances of
# control characters and change it to something else
foreach $file(@list) {
open (FILE, $file) or die "can not open this file: $!";
local $^I=".bak"; # to keep a backup of the files
# set to "" if i don't want backups
local @ARGV = @list; # the files to work on
while (<>) {
s!$pattern!$new_ptrn!g ;
print;
} # end while loop
close (FILE);
} #end of for loop
close (DIR);
__END__
[/--snip script--]
Any suggestions? Thanks!
-X
Shaunn Johnson Guest
-
Loop option set, but flash doesn't loop
I'm loading some swf files on my website and they all use the same code. They include the loop=true command but some loop and some don't. Does... -
Can a film loop play once, then loop on the last frame(s)?
I need a film loop to play once, then loop playback on the last frame so I can keep the LOOP of the film loop set. This will allow the tell commands... -
Film loop rollovers working with tell sprite, but only if Loop is checked
on mouseWithin me cursor 280 tell sprite 40 --the sprite containing the film loop sprite(60).member = member("networkmapsbuttonroll") --swapping... -
Urgent: Repeat loop and Film loop clash!
Hi All, Scenario I have a script running in which the spelling which was typed in by the student is corrected. The alphabets are moved to... -
Help with loop inside loop and mysql queries
Hi List. I cannot see my error: I have relation tables setup. main id entity_name main_type etc etc date_in 1 test type1 x y 2003-06-02... -
Paul Kraus #2
RE: breaking the loop
Perldoc -f last
Perldoc -f next
-----Original Message-----
From: Johnson, Shaunn [mailto:SJohnson6@bcbsm.com]
Sent: Tuesday, September 16, 2003 4:22 PM
To: perl beginners
Subject: breaking the loop
Howdy:
I'm trying to find the best solution for breaking
out of a loop when editing a list of files.
I have a script that:
* gets a list of files
* opens the files with a 'foreach $f(@list)'
* does a 'while <> { ... } close (FILE)'
The script works, but if I run it, it loops
continually and edits (then re-edits) the files.
I think I could use a lock file to do until I
get a particular file, but I'm not sure how
to go about it - at the same time, it may not be
the more productive way to go.
[--snip script--]
my @list = grep {/\.txt/ } readdir(DIR) or die "Can not read the dir\n";
# create a loop to search for instances of
# control characters and change it to something else
foreach $file(@list) {
open (FILE, $file) or die "can not open this file: $!";
local $^I=".bak"; # to keep a backup of the files
# set to "" if i don't want backups
local @ARGV = @list; # the files to work on
while (<>) {
s!$pattern!$new_ptrn!g ;
print;
} # end while loop
close (FILE);
} #end of for loop
close (DIR);
__END__
[/--snip script--]
Any suggestions? Thanks!
-X
Paul Kraus Guest
-
Jeff 'Japhy' Pinyan #3
Re: breaking the loop
On Sep 16, Johnson, Shaunn said:
Your code is doing too much work.>* gets a list of files
>* opens the files with a 'foreach $f(@list)'
>* does a 'while <> { ... } close (FILE)'
>
>The script works, but if I run it, it loops
>continually and edits (then re-edits) the files.
>I think I could use a lock file to do until I
>get a particular file, but I'm not sure how
>to go about it - at the same time, it may not be
>the more productive way to go.
This creates the list of files.>my @list = grep {/\.txt/ } readdir(DIR) or die "Can not read the dir\n";
Perhaps you mean>foreach $file(@list) {
>open (FILE, $file) or die "can not open this file: $!";
>
>local $^I=".bak"; # to keep a backup of the files
> # set to "" if i don't want backups
>local @ARGV = @list; # the files to work on
local @ARGV = $file;
instead.
Better yet, remove the for loop. I'll show the code in a moment.> while (<>) {
> s!$pattern!$new_ptrn!g ;
> print;
> } # end while loop
> close (FILE);
>} #end of for loop
That should be closedir().>close (DIR);
You can do this without opendir(), and without grep() or the for loop.
{
local $^I = ".bak";
local @ARGV = glob "$directory/*.txt";
while (<>) {
s/foo/bar/g;
print;
}
}
Ta da!
--
Jeff "japhy" Pinyan [email]japhy@pobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
<stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
[ I'm looking for programming work. If you like my work, let me know. ]
Jeff 'Japhy' Pinyan Guest
-
John W. Krahn #4
Re: breaking the loop
Shaunn Johnson wrote:
Hello,>
> Howdy:
You are looping through all the files in @list using foreach above and> I'm trying to find the best solution for breaking
> out of a loop when editing a list of files.
>
> I have a script that:
>
> * gets a list of files
> * opens the files with a 'foreach $f(@list)'
> * does a 'while <> { ... } close (FILE)'
>
> The script works, but if I run it, it loops
> continually and edits (then re-edits) the files.
> I think I could use a lock file to do until I
> get a particular file, but I'm not sure how
> to go about it - at the same time, it may not be
> the more productive way to go.
>
> [--snip script--]
> my @list = grep {/\.txt/ } readdir(DIR) or die "Can not read the dir\n";
>
> # create a loop to search for instances of
> # control characters and change it to something else
>
> foreach $file(@list) {
> open (FILE, $file) or die "can not open this file: $!";
>
> local $^I=".bak"; # to keep a backup of the files
> # set to "" if i don't want backups
> local @ARGV = @list; # the files to work on
> while (<>) {
then you are looping through all the files in @list using while (<>).
This means that you are modifying each file the number of times for each
file in the directory. Are you sure this even works at all because you
are not prepending the directory name to the file name?
> s!$pattern!$new_ptrn!g ;
> print;
> } # end while loop
> close (FILE);
> } #end of for loop
>
> close (DIR);
>
> __END__
Something like this should work:
my $dir = '/home/someone';
local $^I = '.bak'; # to keep a backup of the files
# set to "" if i don't want backups
opendir DIR, $dir or die "Cannot open $dir: $!";
local @ARGV = map "$dir/$_", grep /\.txt$/, readdir DIR;
close DIR;
# create a loop to search for instances of
# control characters and change it to something else
while ( <> ) {
s!$pattern!$new_ptrn!g;
print;
} # end while loop
__END__
John
--
use Perl;
program
fulfillment
John W. Krahn Guest
-
R. Joseph Newton #5
Re: breaking the loop
Jeff 'japhy' Pinyan wrote:
Score one for Japhy!> You can do this without opendir(), and without grep() or the for loop.
>
> {
> local $^I = ".bak";
> local @ARGV = glob "$directory/*.txt";
> while (<>) {
> s/foo/bar/g;
> print;
> }
> }
>
> Ta da!
>
This is the first time I have seen the diamond or in-place editing operator
used well. Usually I blanch when I see them--the diamond conjures up the
image of some poor shmuck typing in lists of filenames at the command-line,
and the in-place suggests a delusory "economy" where there really is none in
the processing itself.. Here, with the encapsulation within a small block,
the immediacy of the assignment of @ARGV relative to its use in feeding the
diamond, and the overall brevity of the code, it makes good sense and makes
the process more clear by abstracting the [in this case] irrelevant details of
file access.
Thanks,
Joseph
R. Joseph Newton Guest
-
James Edward Gray II #6
Re: breaking the loop
On Saturday, September 27, 2003, at 01:57 PM, R. Joseph Newton wrote:
This seems a strange thing to say, to me. The diamond operator is> This is the first time I have seen the diamond or in-place editing
> operator
> used well. Usually I blanch when I see them--the diamond conjures up
> the
> image of some poor shmuck typing in lists of filenames at the
> command-line...
simple a shortcut to implementing the UNIX filter methodology, which I
believe has done quite well proving it's usefulness over the years.
James
James Edward Gray II Guest



Reply With Quote

