Help- storing and printing array contents
--20cf305b1318b5626a049eb40938
Content-Type: text/plain; charset=ISO-8859-1
I am writing a perl script to scan all *.html and *.jsp files underneath a
directory recursively and print strings (urls) matching a pattern.
I can do that fine. Problem is when I try to store what it outputs to
console into an array, not able to make it to print the contents.
The reason I am trying to even store it inside an array is to enable me to
sort it next. Any help/pointers greatly appreciated.
Thanks
Script below:
===========
#!/usr/bin/perl -w
#use strict
use File::Find;
my $dir_to_process = "C:/MyDirectory";
#opendir DH, $dir_to_process or die "Cannot open $dir_to_process:$1";
print "Files in $dir_to_process are:\n";
$i = 0;
[at] url_array = (1..2000); # initialized to have enough space in the array
find (\&wanted, $dir_to_process);
print "Printing url_array\n:";
for ($j=0; $j <= $#url_array; $j++)
{
print ($url_array[$j], "\n"); # This DOES NOT print url strings as
expected!!!!!
}
sub wanted {
return unless -f; #skip directories
if ( -f and /.html?/ ) {
$file = $_;
open FILE, $file;
[at] lines = <FILE>;
close FILE;
for $line ( [at] lines) {
if ($line =~ /somepattern/) {
#print"$line";
$url_line = $';
$url_line =~ /\"/;
$url = $`;
print "$i ";
print "$url";
print "\n"; # This prints FINE the string as expected!
$url_array[$i] = $url;
$i++;
}
}
}
if ( -f and /.jsp?/ ) {
$file = $_;
open FILE, $file;
[at] lines = <FILE>;
close FILE;
for $line ( [at] lines) {
if ($line =~ /somepattern) {
$url_line = $';
#print $';
$url_line =~ /\"/;
$url = $`;
print "$i ";
print "$url";
print "\n"; # This prints FINE the string as expected!
$url_array[$i] = $url;
$i++;
}
}
}
}
--20cf305b1318b5626a049eb40938--
Re: Help- storing and printing array contents
--000e0cd4ca46e50c7a049eb47fff
Content-Type: text/plain; charset=ISO-8859-1
I could solve it. Able to store in array, sort it, remove duplicates and
print it. Sure there may be better ways to do it, but it seems to
work. Amazing power of perl in so few lines.
Thanks
Fixed Script below:
============
#!/usr/bin/perl -w
#use strict
use File::Find;
my $dir_to_process = "C:/Mydir";
#opendir DH, $dir_to_process or die "Cannot open $dir_to_process:$1";
print "Files in $dir_to_process are:\n";
$i = 0;
my [at] url_array ;
find (\&wanted, $dir_to_process);
my %hash = map { $_ => 1} [at] url_array;
my [at] unique_url_array = keys %hash;
my [at] sorted_url_array = sort( [at] unique_url_array);
print "Printing sorted_url_array:\n";
for ($j=0; $j <= $#sorted_url_array; $j++)
{
print ($sorted_url_array[$j], "\n"); # This PRINTS sorted,
non-duplicate array
}
print "END Printing sorted_url_array\n";
sub wanted {
return unless -f; #skip directories
if ( -f and /.html?/ ) {
$file = $_;
open FILE, $file;
[at] lines = <FILE>;
close FILE;
for $line ( [at] lines) {
if ($line =~ /somepattern/) {
$url_line = $';
$url_line =~ /\"/;
$url = $`;
$url_array[$i] = $url;
$i++;
}
}
}
if ( -f and /.jsp?/ ) {
$file = $_;
open FILE, $file;
[at] lines = <FILE>;
close FILE;
for $line ( [at] lines) {
if ($line =~ /somepattern/) {
$url_line = $';
$url_line =~ /\"/;
$url = $`;
$url_array[$i] = $url;
$i++;
}
}
}
}
--000e0cd4ca46e50c7a049eb47fff--
Re: Help- storing and printing array contents
>>>>> "SV" == Saral Viral <thesaral.perl [at] 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> [at] 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 [at] 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 ( [at] 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> [at] lines = <FILE>;
SV> close FILE;
alternatively:
use File::Slurp ;
my [at] lines = read_file( $file ) ;
SV> for $line ( [at] 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( [at] 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> [at] lines = <FILE>;
SV> close FILE;
SV> for $line ( [at] 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 ------ uri [at] 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-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Help- storing and printing array contents
>>>>> "SV" == Saral Viral <thesaral.perl [at] gmail.com> writes:
SV> I could solve it. Able to store in array, sort it, remove duplicates and
SV> print it. Sure there may be better ways to do it, but it seems to
SV> work. Amazing power of perl in so few lines.
see my posted comments. you could reduce this by about half easily. and
also make it much better in many many ways. don't settle for something
just working. learn how to make it better which will help all your
future perl programs as well.
also you don't say what the fix was which isn't helpful to others.
SV> Fixed Script below:
SV> ============
SV> #!/usr/bin/perl -w
SV> #use strict
SV> use File::Find;
SV> my $dir_to_process = "C:/Mydir";
SV> #opendir DH, $dir_to_process or die "Cannot open $dir_to_process:$1";
SV> print "Files in $dir_to_process are:\n";
SV> $i = 0;
SV> my [at] url_array ;
SV> find (\&wanted, $dir_to_process);
SV> my %hash = map { $_ => 1} [at] url_array;
hash is a very bad name for a hash. it says nothing about the contents
of the hash.
SV> my [at] unique_url_array = keys %hash;
SV> my [at] sorted_url_array = sort( [at] unique_url_array);
no need for the temp array:
my [at] sorted_url_array = sort keys %hash;
SV> print "Printing sorted_url_array:\n";
SV> for ($j=0; $j <= $#sorted_url_array; $j++)
no need for even the sorted temp array:
foreach my $url ( sort keys %hash ) {
learn to use foreach loops vs index loop. they are better in every way.
uri
--
Uri Guttman ------ uri [at] 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-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/