Entering array output
Hi,
#!/usr/bin/perl
#!/usr/bin/perl -w
my [at] result=`find ../src -name '*Msgs.msg'`;
#print "MSG direictories are: $result[0]";
foreach my $res ( [at] result) {
print "$res"; #prints the array value of result
}
I have the array value as:
.../src/libfile/src/FileRealMsgs.msg
.../src/libl/src/RRLRealMsgs.msg
.../src/libg/src/SSRealMsgs.msg
.../src/libt/src/TTRealMsgs.msg
.../src/libl/src/UURealMsgs.msg
.../src/libpl/src/VVMsgs.msg
Question:
From avobe output for each line I need to "cd to that directory and do
some task".
Example: from above output, I need to "cd ../src/libfile/src"; "do
some task"; again enter to next direcotry and so on.
Any help is appreciated.
Thanks!
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Entering array output
On Fri, Jul 16, 2010 at 17:43, perl_learner <rahman77 [at] gmail.com> wrote:
> Hi,
>
> #!/usr/bin/perl
> #!/usr/bin/perl -w
>
> my [at] result=`find ../src -name '*Msgs.msg'`;
> #print "MSG direictories are: $result[0]";
>
> foreach my $res ( [at] result) {
> print "$res"; #prints the array value of result
> }
>
> I have the array value as:
>
> ../src/libfile/src/FileRealMsgs.msg
> ../src/libl/src/RRLRealMsgs.msg
> ../src/libg/src/SSRealMsgs.msg
> ../src/libt/src/TTRealMsgs.msg
> ../src/libl/src/UURealMsgs.msg
> ../src/libpl/src/VVMsgs.msg
>
> Question:
>
> From avobe output for each line I need to "cd to that directory and do
> some task".
>
> Example: from above output, I need to "cd ../src/libfile/src"; "do
> some task"; again enter to next direcotry and so on.
snip
The [chdir][0] function will change the directory for the Perl script,
but you may be better served by using the [File::Find][1] module as it
rolls the find command and the chdir into one function:
#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
find sub {
return unless /Msgs[.]msg$/;
print "found $File::Find::name\n";
#you are now in the directory where the file was found
#do whatever you want
}, "../src";
[0]: http://perldoc.perl.org/functions/chdir.html
[1]: http://perldoc.perl.org/File/Find.html
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Entering array output
perl_learner wrote:
> Hi,
Hello,
> #!/usr/bin/perl
> #!/usr/bin/perl -w
>
> my [at] result=`find ../src -name '*Msgs.msg'`;
> #print "MSG direictories are: $result[0]";
>
> foreach my $res ( [at] result) {
> print "$res"; #prints the array value of result
> }
>
> I have the array value as:
>
> ../src/libfile/src/FileRealMsgs.msg
> ../src/libl/src/RRLRealMsgs.msg
> ../src/libg/src/SSRealMsgs.msg
> ../src/libt/src/TTRealMsgs.msg
> ../src/libl/src/UURealMsgs.msg
> ../src/libpl/src/VVMsgs.msg
>
> Question:
>
>> From avobe output for each line I need to "cd to that directory and do
> some task".
>
> Example: from above output, I need to "cd ../src/libfile/src"; "do
> some task"; again enter to next direcotry and so on.
>
> Any help is appreciated.
Something like this may work but it is UNTESTED:
#!/usr/bin/perl
use warnings;
use strict;
use Cwd;
use File::Find;
my $current_dir = getcwd;
my $search_dir = '../src';
my %working_dirs;
find sub {
$working_dirs{ $File::Find::dir }++ if /Msgs\.msg\z/;
}, $search_dir;
foreach my $dir ( keys %working_dirs ) {
chdir $dir or die "Cannot chdir to '$dir' $!";
do_some_task();
chdir $current_dir or die "Cannot chdir to '$current_dir' $!";
}
__END__
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Entering array output
Hi John,
Thanks for your reply.
On Jul 18, 6:48=A0pm, jwkr... [at] shaw.ca ("John W. Krahn") wrote:
> perl_learner wrote:
> > Hi,
>
> Hello,
>
>
>
>
>
> > #!/usr/bin/perl
> > #!/usr/bin/perl -w
>
> > my [at] result=3D`find ../src -name '*Msgs.msg'`;
> > #print "MSG direictories are: $result[0]";
>
> > foreach my $res ( [at] result) {
> > print "$res"; #prints the array value of result
> > }
>
> > I have the array value as:
>
> > ../src/libfile/src/FileRealMsgs.msg
> > ../src/libl/src/RRLRealMsgs.msg
> > ../src/libg/src/SSRealMsgs.msg
> > ../src/libt/src/TTRealMsgs.msg
> > ../src/libl/src/UURealMsgs.msg
> > ../src/libpl/src/VVMsgs.msg
>
> > Question:
>
> >> From avobe output for each line I need to "cd to that directory and do
> > some task".
>
> > Example: from above output, I need to "cd ../src/libfile/src"; "do
> > some task"; again enter to next direcotry and so on.
>
> > Any help is appreciated.
>
> Something like this may work but it is UNTESTED:
>
> #!/usr/bin/perl
> use warnings;
> use strict;
>
> use Cwd;
> use File::Find;
>
> my $current_dir =3D getcwd;
> my $search_dir =A0=3D '../src';
>
> my %working_dirs;
> find sub {
> =A0 =A0 =A0$working_dirs{ $File::Find::dir }++ if /Msgs\.msg\z/;
> =A0 =A0 =A0}, $search_dir;
>
> foreach my $dir ( keys %working_dirs ) {
> =A0 =A0 =A0chdir $dir or die "Cannot chdir to '$dir' $!";
> =A0 =A0 =A0do_some_task();
> =A0 =A0 =A0chdir $current_dir or die "Cannot chdir to '$current_dir' $!";
> =A0 =A0 =A0}
>
> __END__
>
> John
> --
> Any intelligent fool can make things bigger and
> more complex... It takes a touch of genius -
> and a lot of courage to move in the opposite
> direction. =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 -- Albert Einstein- Hide q=
uoted text -
>
> - Show quoted text -
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/