Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: How can i debug compound map/grep statements just using print?

by LanX (Canon)
on Dec 01, 2012 at 18:22 UTC ( #1006623=note: print w/ replies, xml ) Need Help??


in reply to How can i debug compound map/grep statements just using print?

please, next time post clear questions with isolated code examples.

map and grep use code blocks which depend on the return value of the last statement.

that means you can use the same techniques used for debugging subroutines, e.g. you can use print anywhere within these blocks, as long as it doesn't change the returned value.

you can also split deeply nested maps and greps in smaller chunks filling temporary arrays and dump these results with Data::Dumper.

UPDATE:

and if you really need it very often, use something which assures the same list as in- and output

sub dumplist { use Data::Dumper; print Dumper \@_; return @_; } @result= map { ... } dumplist grep { ... } @input;

example:

@evenchars = map { $_->[0] } dumplist grep { $_->[1] % 2 } map { [ $_, ord($_) ] } a..l; #prints $VAR1 = [ [ 'a', 97 ], [ 'c', 99 ], [ 'e', 101 ], [ 'g', 103 ], [ 'i', 105 ], [ 'k', 107 ] ];

Cheers Rolf


Comment on Re: How can i debug compound map/grep statements just using print?
Select or Download Code
Re^2: How can i debug compound map/grep statements just using print?
by karlgoethebier (Curate) on Dec 01, 2012 at 19:29 UTC

    You answered: "please, next time post clear questions with isolated code examples.

    My question was: "How can i debug compound map/grep statements just using print?"...and in my code you can read this:

    map { ( $_->stat )[9] } # e.g i'm interested in this... grep { $_->name =~ /.+\.$suffix$/ } # ...and this

    How clear can we get ;-)

    You answered: "you can use print anywhere within these blocks as long as it doesn't change the returned value."

    This is what i was searching for! Honestly said: i didn't know this. And perhaps i've got yet another mental block. Thank you very much for this advice.

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

      Well you posted more than 10 times more code than necessary.

      I said "with isolated code examples." =)

      Take your new terse explanation as a standard for your coming posts. ;)

      Cheers Rolf

        Well, but i have been advised to provide complete "solutions". So please don't be so apodictic.

        «The Crux of the Biscuit is the Apostrophe»

      Your response did get a smile out of me, but the only part of the code that was necessary to show your problem is the find subroutine. All the POD and getoptions code you added are no doubt necessary for a complete program, but not to show the problem you are having

      A Monk aims to give answers to those who have none, and to learn from those who know more.

        Yes, thats right. Sorry. Regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1006623]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2014-10-02 04:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (48 votes), past polls