[ replying to the list since that's where the discussion belongs ]
Ron Smith wrote (to me privately):
Thank you *very* much for furthering my 'Perl' knowlege. I've never see a variable like '@{ $HoA{$dir} }' before.
Well, it's not a special variable type. $HoA{$dir} is a reference to an anonymous array, which you dereference with the @{ $HoA{$dir} } construct.
I'm just at the 'Llama' level. I think I understand what's going on though. Your solution:...
my %HoA; for ( `dir /b/s` ) { push @{ $HoA{$1} }, $2 if /(.+)\\(\w+)\.\d+\.\w+$/; }
for my $dir (sort keys %HoA ) { print "$dir\n", join( "\n", @{ $HoA{$dir} } ), "\n\n"; }
...worked out fine. This one took some thought for me to wrap my head around. Thank you so *very* much for showing me something new and very useful.
I now realize that the small piece of code above combines three components of Perl that make it a really powerful programming language: Hashes, references and regular expressions.
I'm attempting to play around with this new tool to get it to do different things, but I'm running into another problem. I can't seem to pull the elements back out from the arrays properly. I'm tring to count the basenames now. I get what looks like memory addresses instead. I think these are the references to the actual arrays that you were eluding to.
Sounds plausible. :)
I was using parts of the script you helped me out on before to do the counting of the basenames:
#!/usr/bin/perl -w
use strict;
my @paths = `dir /b`; my @basenames = &extract_names(@paths);
sub extract_names { my ($name, @names); for (@_) { if (/(\w+)\.\d+\.\w+$/) { $name = $1; $name =~ s/$/\n/;
Hmm.. It's usually practical to not add "\n" like that, but take care of linebreaks in connection with printing the variable. Without adding "\n", instead of saying
print @basenames;
you can say e.g.
print join("\n", @basenames), "\n";
push @names, $name; } } @names; }
my (%count, $frames); for $frames (@basenames) { chomp ($frames); $count{$frames} += 1; } for $frames (sort keys %count) { # print "$frames\t1-$count{$frames}\n"; printf "%20s\t%04d\n", $frames, $count{$frames}; }
One of the things I attempted was the following:
#!/usr/bin/perl -w
use strict; my %HoA; for ( `dir /b/s` ) { push @{ $HoA{$1} }, $2 if /(.+)\\(\w+)\.\d+\.\w+$/; }
for my $dir (sort keys %HoA ) { print "$dir\n"; for my $frames (@{ $HoA{$dir}}) { my $count{frames} += 1; print "$count{frames}\n"; } }
But, I get the following error messages:
syntax error at solution line 13, near "$count{frames" syntax error at solution line 13, near "+=" syntax error at solution line 16, near "}" Execution of solution aborted due to compilation errors.
There's some concept I'm missing,
No, it's rather just because you didn't declare the %count hash properly (you also had 'frames' instead of '$frames' a couple of times). Instead of
for my $frames (@{ $HoA{$dir}}) { my $count{frames} += 1; print "$count{frames}\n"; }
you want
my %count; for my $frames ( @{ $HoA{$dir} } ) { $count{$frames} += 1; print "$count{$frames}\n"; }
and I was hoping you'd help me out one more time. I've spent a lot of time on this trying to solve the problem myself, but I keep hitting a brick wall no matter what I try. I'm trying to get:
C:\dir_name\dir_name\dir_name (Path) followed by basename 0001-0005 (file counts) another_basename 0001-0010
These files have the same basename, but they're numbered like: basename.0001.rgb, basename.0002.rgb ...etc.
Now when I've helped you with the syntax error, I believe you can figure out how to print the counts similar to what you did with the previous code version. If not, please feel free to post (to the list) again.
Also, If you can, could you point me in the dirrection of a book or a site that explains the more advanced stuff like:
@{$HoA{$dir}}
I would really appreciate it.
References and data structures are indeed tricky in the beginning - I sure thought they were - but they are well documented at the same time. These are some applicable parts of the Perl docs:
perldoc perlreftut perldoc perlref
perldoc perldsc perldoc perllol
As regards books, others are better suited than me to give recommendations (the only Perl book I have is the "Camel").
Or, should I try to see if I can get a tutor?
Only you can tell.
-- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>