On Friday 19 October 2007 07:41:40 Beginner wrote: > Hi, > > In the script below I am trying my hand at using a code ref within a > hash. I want the key 'name' to contain a scalar value. It does but I > am not sure how to dereference it. > > Here is the output from the script at the moment. > > /data/users/nat/finished/01.tif SCALAR(0x8db3cac) > /data/users/nat/finished/19.tif SCALAR(0x8db3cf4) > /data/users/nat/finished/06.tif SCALAR(0x8db3d3c) > > If i assign the return from the subroutine before, putting into a > scalar and using that value within the hash then it's fine but in > it's current state I get the SCALAR(0x8db3d3c) from $f->{'name'} > > Can anyone offer me some advise? > Thanx, > Dp. > > > =========================script=========== > #!/usr/bin/perl > > use lib qw(.); > use strict; > use warnings; > use Config; > use File::Find; > > my @list_of_found; > > find(\&wanted, $sroot); > > > foreach my $f (@list_of_found) { > print $f->{'basename'}," ",$f->{'name'},"\n"; > } > > > sub wanted { > > if ($File::Find::name =~ /users/ && $_ =~ /\.tif$/) { > > my %file = ( > 'tiffpath' => $File::Find::name, > 'basename' => $_, > 'name' => \&edit_from_path($File::Find::name), > ); > push(@list_of_found,\%file); > > } > > } > > sub edit_from_path { > (my $n) = ($_ =~ /users\/(\w+)\//); > return $n; > } > =======================================
Here's an illustrative example: #!/usr/bin/perl sub one { return "subroutine one\n"; } sub two { return "subroutine two\n"; } sub three { return "subroutine three\n"; } my %hash = ( 1 => \&one, 2 => \&two, 3 => \&three, ); print $hash{'1'} . " - " . &{$hash{'1'}}; print $hash{'2'} . " - " . &{$hash{'2'}}; print $hash{'3'} . " - " . &{$hash{'3'}}; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/