Well I've moved this along a little further, but it
looks like I'm stuck on one last thing.
I'm getting;
C:\Perl\scripts\dir\dir\dir
basename 0001
basename 0002
basename 0003
basename 0004
basename 0005
basename 0006
basename 0007
basename 0008
basename 0009
basename 0010
another_basename 0001
another_basename 0002
another_basename 0003
another_basename 0004
another_basename 0005
another_basename 0006
yet_another_name 0001
yet_another_name 0002
yet_another_name 0003
yet_another_name 0004
yet_another_name 0005
C:\Perl\scripts\dir\dir\dir\sub_directory
basename 0001
basename 0002
basename 0003
basename 0004
basename 0005
basename 0006
basename 0007
basename 0008
basename 0009
basename 0010
C:\Perl\scripts\dir\dir\dir\sub_directory\deeper_sub
basename 0001
basename 0002
basename 0003
basename 0004
The following is the re-worked script:
----------------snip---------------------
#!/usr/bin/perl -w
use strict;
my %HoA;
for ( `dir /b/s` ) {
push @{ $HoA{$1} }, $2 if
/(.+)\\(\w+)\.(\d+)\.(\w+)$/;
}
my %count;
for my $dir ( sort keys %HoA ) {
print "$dir\n";
my @basenames = @{ $HoA{$dir} };
for my $frames ( @basenames ) {
$count{$frames} += 1;
printf "%30s\t%04d\n", $frames,
$count{$frames};
}
}
----------------snip---------------------
I'm trying to get the following output:
C:\Perl\scripts\dir\dir\dir
basename 0010
another_basename 0006
yet_another_basename 0005
C:\Perl\scripts\dir\dir\dir\sub_directory
basename 0010
C:\Perl\scripts\dir\dir\dir\sub_directory\deeper_sub
basename 0004
I've gone through 'perldoc perlreftut', but can't see
the last step.
--- Gunnar Hjalmarsson <[EMAIL PROTECTED]> wrote:
> [ 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").
>
>
=== message truncated ===
_______________________________
Do you Yahoo!?
Express yourself with Y! Messenger! Free. Download now.
http://messenger.yahoo.com
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>