Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've spent some time looking at your code and I think I have it figured out.</p> <p>The reason this was hard to answer is that you've unintentionally planted a red herring--the data dumper output.</p> <p>Notice how it shows <code>$VAR1 = 'server1';</code> and then <code>$VAR2 = { blah };</code>.</p> <p>You called Dumper like so: <code>print Dumper %MyItems;</code></p> <p>The problem is that Dumper wants a list of values to dump, since Perl flattens lists, complex structures must be passed by reference. So, you need to call Dumper like so:</p> <pre><code>print Dumper \%MyItems; </code></pre> <p>This shows the whole structure.</p> <p>When you called dumper earlier, you inadvertently stripped off one layer of your data structure. The proposed solutions, and your own code are operating on this stripped structure.</p> <p>Here I've bolted on some code to handle additional layer of nesting (and made it Perl 5.8 compatible):</p> <pre><code>for my $server_items ( values %MyItems ) { for my $record ( values %$server_items ) { print join ';', map { # Replace non-existant values with 'undef' my $val = exists $record-&gt;{$_} ? $record-&gt;{$_} : 'undef'; "'$_'=&gt;$val" # &lt;-- this is what we print for each field } qw( MyHost MyLogdate MyDataset backup-time backup-status ); print "\n"; } } </code></pre> <p>It looks like you have a lot of questions and need some help getting your head around a number of concepts. I suggest that you post a request on Perlmonks in Seekers of Perl Wisdom for help improving your code. SO is great for focussed question, but PM is more amenable to code rework.</p> <p>** Original answer: **</p> <p>To get around any parsing issues that I can't replicate, I just set <code>%MyItems</code> to the output of Dumper you provided.</p> <p>Your warnings you mention above have to do with all the complicated quoting and repetitive coding you have in your print statement. I have replaced your print statement with a <code>map</code> to simplify the code.</p> <p>Holy crap, a big join map blah is not simpler, you might be thinking. But really, it is simpler because each individual unit of expression is smaller. What is easier to understand and get right? What is easier to alter and maintain in a correct and consistent manor?</p> <pre><code>print "'foo'=&gt;$_-&gt;{foo};'bar'=&gt;$_-&gt;{bar};boo'=&gt;$_-&gt;{boo};'far'=&gt;$_-&gt;{far}\n"; </code></pre> <p>or</p> <pre><code>say join ';', map { "'$_'=&gt;$item-&gt;{$_}" } qw( foo bar boo far ); </code></pre> <p>Here, you can add, remove or rearrange your output merely by changing the list of arguments passed to <code>map</code>. With the other style, you've got a bunch of copy/paste to do.</p> <p>The map I use below is a bit more complex, in that it checks to see if a given key is defined before printing a value, and assign a default value if none is present.</p> <pre><code>#!perl use strict; use warnings; use feature 'say'; my %MyItems = ( 'abc1.mil.mad' =&gt; { 'ERROR' =&gt; ' If you are sure is not running, please remove the file and restart ', 'MyLogdate' =&gt; 'Fri Aug 06 2010', 'MyHost' =&gt; 'server1', 'MyDataset' =&gt; 'abc1.mil.mad' }, 'abc2.cfl.mil.mad' =&gt; { 'backup-size' =&gt; '187.24 GB', 'MyLogdate' =&gt; 'Fri Aug 06 2010', 'MyHost' =&gt; 'server1', 'backup-status' =&gt; 'Backup succeeded', 'backup-time' =&gt; '01:54:27', 'MyDataset' =&gt; 'abc2.cfl.mil.mad' }, 'abc3.mil.mad' =&gt; { 'backup-size' =&gt; '46.07 GB', 'MyLogdate' =&gt; 'Fri Aug 06 2010', 'MyHost' =&gt; 'server1', 'backup-status' =&gt; 'Backup succeeded', 'backup-time' =&gt; '00:41:06', 'MyDataset' =&gt; 'abc3.mil.mad' }, 'abc4.mad_lvm' =&gt; { 'backup-size' =&gt; '422.99 GB', 'MyLogdate' =&gt; 'Fri Aug 06 2010', 'MyHost' =&gt; 'server1', 'backup-status' =&gt; 'Backup succeeded', 'backup-time' =&gt; '04:48:50', 'MyDataset' =&gt; 'abc4.mad_lvm' } ); for my $record ( values %MyItems ) { say join ';', map { my $val = $record-&gt;{$_} // 'undef'; # defined-or requires perl 5.10 or newer. "'$_'=&gt;$val" # &lt;-- this is what we print for each field } qw( MyHost MyLogdate MyDataset backup-time backup-status ); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload