in reply to Re: warnings and grep problem
in thread warnings and grep problem

You're probably right (I'm still not very good with grep and map)
Anyway I tried the following
my $hash = map { /\Q$dir\E(\w+?)\.pm$/ and defined $1, $1 } keys %$pac +klist ;
Looks a lot better,but printing it like
foreach ( keys %hash) print $_ ; }
gives a new warning
Odd number of elements in hash assignment at /usr/lib/p....
Any suggestions ?

LuCa

Replies are listed 'Best First'.
Re^3: warnings and grep problem
by Fletch (Bishop) on Jun 20, 2006 at 16:10 UTC

    You're attempting to use the number of keys which matched to initialize a hash. Trying to use a single scalar to initialize a hash isn't going to produce anything useful.

    my @desired_keys = grep { /\Q$dir\E.*?\.pm$/ } keys %$packlist; my %hash; @hash{ map { /\Q$dir\E(\w+?)\.pm$/; $1 } @desired_keys } = @desired_ke +ys;

    Update: Gah. Me no write well. Reworded to (hopefully) clarify.

      Ok, but what about this
      my @dirs = map { s/\Q$dir\E(\w+?)\.pm$/$1/, $1 } keys %$packlist; for (@dirs) { print "|$_|\n" if ( $_ and length($_) > 2 ); }
      This seems to work, but without the 'length($_) > 2' I get
      |1| |A_Module| |1| |An_Other_Module|
      Is there a better way to get rid of the 1s!
Re^3: warnings and grep problem
by eric256 (Parson) on Jun 20, 2006 at 16:11 UTC

    You assigned the output of the map to a scalar $has so you'll just get the number of elements map returned, not the actual elements. Then you did keys %hash which is a whole new variable. The following might do what you want, but if you are messing with path/file names then i would highly recommend use a CPAN module.

    my @dirs = map { s/\Q$dir\E(\w+?)\.pm$/$1/ } keys %$packlist; for (@dirs) { print $_, "\n"; }

    ___________
    Eric Hodges