http://www.perlmonks.org?node_id=877715

iphone has asked for the wisdom of the Perl Monks concerning the following question:

I have a hash like below,values are in an array,some values of array are comments,now I want to add a check not to print the comments.I treid the below but not successful $VAR1 = { 'file.txt' => [ 'pages/value/data.h ', '@label ', '/* CAT/APP/file.c */ ', };

for my $key (keys %Hash) { my $value = $Hash{$key}; if(@$value !=~ /^\/\*/) #if the value is a comment donot print + it. { if (scalar @$value) { # check that the arrayref isn't empty print "\n ", join(", ", @$value), "\n\n"; } } }

Replies are listed 'Best First'.
Re: How do I stop printing a hash value
by ikegami (Patriarch) on Dec 18, 2010 at 01:02 UTC

    No, your problem has nothing to do with hashes. You want to stop printing some elements of an array.

    You can use grep to filter out the elements you are passing to join.

      ya..right,to be be exact my query is how do I grep for a comment?

        Q2. A2.   RTFM: perldoc -f grep

        Q1. A1.   perldoc perlretut

        or, work out what's happening here:

        ... for my $var(@var) { # as ikegami said, you're not working +with a hash if ( $var !~ /\/* .*? \/*/ ) { print "\$var, \'$var\', is (something for iphone to fill in) \ +n"; } else { print "\$var, \'$var\', is (the reverse) \n"; } } ...
        I mean I tried the below but doesnt seem to work for me
        if ((scalar @$value) && not grep (/\/\*(.*)\*\//,@$value)) { }
Re: How do I stop printing a hash value
by PeterPeiGuo (Hermit) on Dec 18, 2010 at 01:45 UTC

    First of all, you need to change !=~ to !~, your code as it is won't even compile. Secondly, you don't want to apply matching against the array, it is not going to do what you wanted.

    Peter (Guo) Pei

      I am using the below
      if ((scalar @$value) && not grep (/\/\*(.*)\*\//,@$value)) { }

        Read the document for "grep" more carefully. The following might be a starting point for you, and you can continue from here:

        print join(",", grep(!/\/\*(.*)\*\//, @$value));

        Peter (Guo) Pei