Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Need more elegant solution

by dirtdog (Beadle)
on Feb 25, 2011 at 02:53 UTC ( #890110=perlquestion: print w/ replies, xml ) Need Help??
dirtdog has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I have a little piece of code that is going to be part of a larger program, but it seems a bit cumbersome. It does work, but I was hoping for a more elegant solution. I'm sure in perl it can be better. I have 3 reports (doc_request, doc_errors, and doc_types) and each report has 3 sections as you can see in the code. I need to access each section of each report in the order presented...so T_Doc_Request 1st, then N_Doc_Request, followed by X_Doc_Request, then T_Doc_Errors, followed by N_Doc_Errors, etc..

my @reports = qw /doc_request doc_errors doc_types /; my %rpt = ( doc_request => [ "T_Doc_Request, N_Doc_Request, X_Doc_Request" ], doc_errors => [ "T_Doc_Errors, N_Doc_Errors, X_Doc_Errors" ], doc_types => [ "T_Doc_Types, N_Doc_Errors, X_Doc_Errors" ], ); my @queries; foreach (@reports) { @queries = split /,/, "@{$rpt{$_}}"; foreach (@queries) { print "section is: $_\n"; } splice(@queries); }

Any help would be greatly appreciated. thanks

Comment on Need more elegant solution
Download Code
Re: Need more elegant solution
by Anonymous Monk on Feb 25, 2011 at 03:03 UTC
    for (@reports) { print "section is: $_\n" for split /,/, @{$rpt{$_}}; } for (@reports) { print map { "section is: $_\n" } split /,/, @$rpt{$_}; }
Re: Need more elegant solution
by AnomalousMonk (Abbot) on Feb 25, 2011 at 04:57 UTC

    Just a bit hard to tell what your ultimate intent is, but maybe something like:

    >perl -wMstrict -le "my @reports = qw /doc_request doc_errors doc_types /; my %rpt = ( doc_request => [ qw(T_Doc_Request N_Doc_Request X_Doc_Request) ], doc_errors => [ qw(T_Doc_Errors N_Doc_Errors X_Doc_Errors ) ], doc_types => [ qw(T_Doc_Types N_Doc_Errors X_Doc_Errors ) ], ); ;; foreach my $report (@reports) { foreach my $section (@{ $rpt{$report} }) { do_something_with($report, $section); } } ;; sub do_something_with { my ($report, $section) = @_; print qq{report '$report', section '$section'}; } " report 'doc_request', section 'T_Doc_Request' report 'doc_request', section 'N_Doc_Request' report 'doc_request', section 'X_Doc_Request' report 'doc_errors', section 'T_Doc_Errors' report 'doc_errors', section 'N_Doc_Errors' report 'doc_errors', section 'X_Doc_Errors' report 'doc_types', section 'T_Doc_Types' report 'doc_types', section 'N_Doc_Errors' report 'doc_types', section 'X_Doc_Errors'
Re: Need more elegant solution
by wind (Priest) on Feb 25, 2011 at 05:26 UTC
    If your data should be ordered, than you simply use an multidimensional array over a hash:
    my @reports = ( [doc_request => [qw(T_Doc_Request N_Doc_Request X_Doc_Request)]], [doc_errors => [qw(T_Doc_Errors N_Doc_Errors X_Doc_Errors )]], [doc_types => [qw(T_Doc_Types N_Doc_Errors X_Doc_Errors )]], ); for (@reports) { my $report = $_->[0]; for my $section (@{$_->[1]}) { print qq{report '$report', section '$section'\n}; } }

      Great suggestion, other than there only being a multi-dimensional array, no hash component.

      As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (10)
As of 2014-12-28 15:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (182 votes), past polls