Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Perl script does not work on other directories?

by james28909 (Deacon)
on Nov 24, 2014 at 02:20 UTC ( [id://1108216]=note: print w/replies, xml ) Need Help??


in reply to Perl script does not work on other directories?

heres another way, tho it still does not use chdir, but it does work from the scripts location and all you have to feed it is the absolute path or from script path as arg.
use strict; use warnings; use diagnostics; use File::Slurp; #use Cwd; my @files; get_args(); sub get_args { my $dir; my $string; print "enter path\n"; chomp( $dir = <STDIN> ); print "enter match string\n"; chomp( $string = <STDIN> ); print "\n"; @files = read_dir($dir); print $_ for @files ; traverse( $dir, $string ); } sub traverse { foreach my $element (@files) { open $file, "<", $dir.'/'.$element; while (<$file>) { if ( $_ =~ m/$string/i ) { print "found $string in $element\n"; } } close $file; } get_args(); }
to me its more manageable if it is broken up in subroutines. that way you can have one function to get the input, then the other to actually collect results.

EDIT: here is the newest code, which i have not really looked any further into this tbh, but you should be able to add chdir and list dir contents pretty easily. Could maybe even add switches for searching cwd for the string you needed.
use strict; use warnings; use diagnostics; use File::Slurp; #use Cwd; my @files; get_args(); sub get_args { print "enter path: "; chomp( my $dir = <STDIN> ); print "enter match string: "; chomp( my $string = <STDIN> ); @files = read_dir($dir); print "\n", $_ for @files, "\n" ; traverse( $dir, $string ); } sub traverse { my ( $dir, $string ) = @_; for my $element (@files) { open (my $file, '<', "$dir/$element") || next; while (<$file>) { if ( $_ =~ m/$string/i ) { print "found $string in $element\n"; } } close $file; } print "\n"; get_args(); }

Replies are listed 'Best First'.
Re^2: Perl script does not work on other directories?
by Anonymous Monk on Nov 24, 2014 at 10:38 UTC
    Why write  traverse( $dir, $string ); if you're using fake globals?
      the script complained when i tried to declare them only when i needed them. but yes you are correct and i completely missed that. i changed the script on my machine and declared the variables inside their respective subroutine and its working fine. thanks for pointing that out :)

        ... i changed the script on my machine and declared the variables inside their respective subroutine and its working fine....

        Hmm, I don't think I see that reflected in the code you posted, will you do an update?

Re^2: Perl script does not work on other directories?
by james28909 (Deacon) on Nov 26, 2014 at 03:30 UTC
    Here is my last attempt. This will print directories then files and you can traverse directories easily. typing ".." at "enter path" will get you to parent dir
    use strict; use warnings; use diagnostics; use File::Slurp; use Cwd; for(;;){ my @files; my $dir; print "enter path: "; chomp( $dir = <STDIN> ); chdir($dir); $dir = getcwd($dir); print $dir; # print "enter match string: "; # chomp( my $string = <STDIN> ); @files = read_dir($dir); foreach my $element (@files) { next if -f $element; print "$element\n"; } foreach my $element (@files) { next if -d $element; print "$element\n"; } print "\ncurrent dir is: $dir\n"; print "\n"; system("pause"); system("cls"); }
      i was trying to figure out how to sort the -d and -f without having to use two loops. but i am unsure how to use sort to do such a thing.

        You want to compare one thing, and then if it's the same, compare something else, and continue until you're sure they're both identical.

        @results = sort { MostImportantFeature($a) <=> MostImportantFeature($b) or NextFeature($a) <=> NextFeature($b) or LeastImportantFeature($a) <=> LeastImportantFeature($b) } @source;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1108216]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-24 18:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found