Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Hash Manipulation

by Fletch (Bishop)
on Jun 07, 2021 at 18:41 UTC ( [id://11133635]=note: print w/replies, xml ) Need Help??


in reply to Hash Manipulation

This may sound a bit like pushing you out into deeper water, but you probably should look into the Template module (see also template-toolkit.org). Rather than trying to whip up your formatted data in your hash, concentrate on extracting just the info you want to appear in your output and pass that to a template which has placeholders in your TeX output. That lets you separate manipulating your data from the presentation (and the template lets you more easily get the output in the right "shape" and you just have to pass the right things to populate the placeholders).

use 5.010; use Template; my $t = Template->new; my $document_template = <<'EOT'; [% FOR item IN rows.keys %] %% Item [% loop.count %] of [% loop.size %] \multirow{1}{0.85in}{\mytabhead{[% item %]}} & \mytabhead{[% rows.$ite +m.Description %]} & [% rows.$item.Type %] \hline [% END %] EOT my $output_text = q{}; my $context = {}; $context->{rows} = \%hash; # from your original data $t->process( \$document_template, $context, \$output_text ) or die qq{Template error: }, $t->error, qq{\n}; say $output_text;

Edit: Two things came to mind after seeing haukex' reply below:

I didn't look closely at your manipulation code (because what you're trying to do made my eyes water . . .) but he's completely right about you overwriting things reassigning new hashrefs. Similarly using integers as the keys of a hash is a sign you prossibly want a different data structure using an arrayref instead. Using an array(ref) and push would maybe make some of your problems with tracking the indexen and positions easier. Checkout the data structures cookbook for more details (perldsc; look for an AoH, Array Of Hashes).

Also another TT feature that may be of use is the loop special variable that gives you access to an iterator object inside the template FOR loop (added a comment using it to sample above). You could use that and arithmetic to alter the output based on which element you're on to get your pagination / special breaks. Alternately you could use something like List::MoreUtils and its natatime to partition your list into whatever size chunks.

The cake is a lie.
The cake is a lie.
The cake is a lie.

Replies are listed 'Best First'.
Re^2: Hash Manipulation
by JusaEngineer (Novice) on Jun 08, 2021 at 14:31 UTC

    Thank you for your answers, It's just going to take me a little while before I can decipher them lol. You are far more advanced than I am at understanding perl. My code is clunky because I don't know better ways to accomplish what I need. So I try to take a bunch of baby stems then see if I can put those baby steps together for less steps. I have about 500 buttons over about 20 screens and I can only have 14 per page. I know how to pull the data from the XML file because I use this file to make other documents.

    I'm able to get most of what I need formatted. I'll make a new post with the simplified hash so it's easier to read. Again thanks for your help!

Log In?
Username:
Password:

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

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

    No recent polls found