Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

How do I stop printing a hash value

by iphone (Beadle)
on Dec 18, 2010 at 00:46 UTC ( [id://877715]=perlquestion: print w/replies, xml ) Need Help??

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://877715]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2024-04-19 01:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found