>>>>> "SV" == Saral Viral <thesaral.p...@gmail.com> writes:
SV> I am writing a perl script to scan all *.html and *.jsp files underneath a SV> directory recursively and print strings (urls) matching a pattern. SV> I can do that fine. Problem is when I try to store what it outputs to SV> console into an array, not able to make it to print the contents. SV> The reason I am trying to even store it inside an array is to enable me to SV> sort it next. Any help/pointers greatly appreciated. SV> Thanks SV> Script below: SV> =========== SV> #!/usr/bin/perl -w use warnings is better. SV> #use strict don't disable that. always let perl help you find mistakes. SV> use File::Find; SV> my $dir_to_process = "C:/MyDirectory"; SV> $i = 0; SV> @url_array = (1..2000); # initialized to have enough space in the array no need to preallocate arrays. and no need for $i. see below. just declare that array as my @urls (no need to have 'array' as a suffix for arrays). SV> find (\&wanted, $dir_to_process); SV> print "Printing url_array\n:"; SV> for ($j=0; $j <= $#url_array; $j++) don't index loop over arrays. just loop over them directly. for my $url ( @urls ) { SV> { SV> print ($url_array[$j], "\n"); # This DOES NOT print url strings as SV> expected!!!!! SV> } try to see what is in the array with Data::Dumper. the loop and code look ok but i can't tell without running it. SV> sub wanted { SV> return unless -f; #skip directories SV> if ( -f and /.html?/ ) { SV> $file = $_; SV> open FILE, $file; always check for the result of open. SV> @lines = <FILE>; SV> close FILE; alternatively: use File::Slurp ; my @lines = read_file( $file ) ; SV> for $line (@lines) { SV> if ($line =~ /somepattern/) { SV> #print"$line"; SV> $url_line = $'; SV> $url_line =~ /\"/; SV> $url = $`; don't use $' and $` (long explanation why). instead make your pattern grab the parts you want and use $1, etc to get them. the whole style you have of matching something and grabbing around it is confusing. you can easily write a single regex to match and grab at the same time. it will be faster and much easier to read and understand. SV> print "$i "; SV> print "$url"; no need to quote that. in fact you could print it all in one string: print "$i: $url\n" ; that is if you have $i, i wouldn't use it. SV> print "\n"; # This prints FINE the string as expected! that prints a newline. put the comment where it belongs. :) SV> $url_array[$i] = $url; SV> $i++; push( @urls, $url ) ; SV> } clean up your indenting. it will make reading and debugging your code easier. SV> } SV> } SV> if ( -f and /.jsp?/ ) { SV> $file = $_; SV> open FILE, $file; SV> @lines = <FILE>; SV> close FILE; SV> for $line (@lines) { SV> if ($line =~ /somepattern) { SV> $url_line = $'; SV> #print $'; SV> $url_line =~ /\"/; SV> $url = $`; SV> print "$i "; SV> print "$url"; SV> print "\n"; # This prints FINE the string as expected! SV> $url_array[$i] = $url; SV> $i++; that code looks to be the same as the other part except for the html vs jsp part. why not check for either suffix and have one common code section? uri -- Uri Guttman ------ u...@stemsystems.com -------- http://www.sysarch.com -- ----- Perl Code Review , Architecture, Development, Training, Support ------ --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com --------- -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/