2

When you dump your array with:

use Data::Dumper;
@arr=('a','b','c');
print Dumper @arr;

you get something like this:

$VAR1 = 'a';
$VAR2 = 'b';
$VAR3 = 'c';

Is possible to get something like this:

$VAR0 = 'a';
$VAR1 = 'b';
$VAR2 = 'c';

EDIT:

So far I have end up with this one-liner:

perl -lane 'if ($_=~/VAR([0-9]+) = (.*)/) {print "VAR" . ($1-1) . "=" . $2} else {print $_}'

It works as post processing script which decrement the number after VAR. But as you can see it wont produce correct output when you have element like this:

VAR7=$VAR2->[1];

Can I somehow extend this one-liner?

Wakan Tanka
  • 7,542
  • 16
  • 69
  • 122
  • 4
    Why would you want to start with `0`? You're not trying to parse the Dumper output are you? Because that would be a very bad idea indeed. When printing an array with `Dumper()` you should send it a reference, like so: `print Dumper \@arr`. – TLP Oct 14 '13 at 13:39
  • No, I do not want to parse Dumper output, all that I want is to set array indexing in output as it is in perl (first element of array start at zero) – Wakan Tanka Oct 14 '13 at 13:57
  • @WakanTanka The names of the variables are all but irrelevant, and indexing would be merely for cosmetic purposes. If that is why you want it: You like it when `$arr[0]` is `$VAR0` because it is easier to read (or something), then that's fine. If its for any other reason, you should not do this. Whatever you come up with, it will be fragile and redundant. – TLP Oct 14 '13 at 14:13
  • Why are you writing a Perl script to post-process the output of another Perl script? If all you're using `Data::Dumper` for is pretty-printing arrays, you could easily get the same functionality with a simple `for` loop and a *single* script. – ThisSuitIsBlackNot Oct 14 '13 at 14:17
  • 1
    `print "\$VAR$_ = '$arr[$_]';\n" for 0..$#arr` – mpapec Oct 14 '13 at 14:19

4 Answers4

4

The Dump method takes an optional second array ref where you can specify the desired variable names in the output:

my @arr   = ('a', 'b', [qw(d e f)]);
my @names = map "VAR$_", 0 .. $#arr;

print Data::Dumper->Dump(\@arr, \@names);

Output:

$VAR0 = 'a';
$VAR1 = 'b';
$VAR2 = [
  'd',
  'e',
  'f'
];

You might also take a look at Data::Printer. I've never used it, but it seems more oriented to the visual display of data structures.

FMc
  • 41,963
  • 13
  • 79
  • 132
  • Just one note, will this work also on nested arrays and on those cases (VAR7=$VAR2->[1];) that I have mentioned in question? – Wakan Tanka Oct 14 '13 at 15:24
  • 1
    @WakanTanka With nested arrays, the issue raised in your question (the top-level variable naming scheme) is sort of irrelevant. For the inner arrays, the output will be shown without names or index numbering. See edited answer for an example. – FMc Oct 14 '13 at 15:40
  • Would be possible to add numbering also for inner arrays? – Wakan Tanka Oct 14 '13 at 15:48
  • 1
    @WakanTanka Not that I know of. `Data::Dumper` is great, but it's not really a tool designed to support fine-grained customization in the presentation of its output. What is the underlying problem you're trying to solve? Maybe there's a better tool than `Data::Dumper` for that problem. – FMc Oct 14 '13 at 17:16
  • Originally this was a problem: how to address Data::Dumper output from starting at 0. Then I realized that indexing also inner elements might be useful in some cases, nothing more nothing less. – Wakan Tanka Oct 14 '13 at 20:50
  • 2
    @WakanTanka no, that isn't the underlying problem; there is something else you are trying to do by having the numbering starting at 1/having inner elements indexed; what is that other thing? – ysth Oct 14 '13 at 20:51
  • 1
    I just want the tool that can easy display various data structures for debugging purposes. Data::Dumper seems fine (it can display various nested arrays (trees), hashes etc.) but I can imagine that it will be possible to tweak and customize somehow it's output. Or maybe I am searching for fast and painless bug-hunting best practises. – Wakan Tanka Oct 14 '13 at 21:31
  • @WakanTanka Maybe take a look at Data::Printer (mentioned in the edited answer). – FMc Oct 14 '13 at 21:42
3

Whatever you are trying to do with $VARx, it isn't a good idea. How about just dumping \@arr instead of @arr?

use Data::Dumper;
@arr=('a','b','c');
print Dumper \@arr;

producing:

$VAR1 = [
          'a',
          'b',
          'c'
        ];
ysth
  • 96,171
  • 6
  • 121
  • 214
  • When you print reference you lose "indexes" and whole trying is useless. – Wakan Tanka Oct 14 '13 at 15:11
  • no, you don't; the indexes are there as `$VAR1->[0]`, etc. whatever you are trying to accomplish by having specific VARx numbers, there is a better way to do. – ysth Oct 14 '13 at 20:48
  • 1
    But I would like to have ability to visualize the structure (and its indexes) not just access them. Of course I can use `$VAR1->[0]` but how this helps me when I have the output from Data::Dumper in format that you presented? Imagine that you have complex structure like AoAoAoH... and you want to know where your code is going buggy, and you do not want to count indexes one by one to see what happens. What I want is just look at structure and be able to tell: the value at index in `$arr->[2]->[4]->[5]->{'key1'}` is not what I want, let's set some breakpoint where this variable changes – Wakan Tanka Oct 14 '13 at 21:42
0

You can play around with some of the options (see: http://perldoc.perl.org/Data/Dumper.html)

#!/usr/bin/perl
use warnings;
use strict; 
use Data::Dumper;
$Data::Dumper::Terse = 1; # This will print out just your output (in structure)
$Data::Dumper::Pad  = 'YOUR-PREFIX = '; # This prefixes your output with whatever you want

my @arr=('a','b','c');

print Dumper @arr;

Outputs:

YOUR-PREFIX = 'a'
YOUR-PREFIX = 'b'
YOUR-PREFIX = 'c'
fugu
  • 6,417
  • 5
  • 40
  • 75
  • Yes, I have found something similar here: http://stackoverflow.com/questions/908741/how-do-i-control-the-variable-names-in-perls-datadumper but it is not what I want. – Wakan Tanka Oct 14 '13 at 13:58
  • I'm not sure if you can/why you would want to do that. Maybe you could provide some context in your question to give people a better idea of what you're trying to do... – fugu Oct 14 '13 at 14:07
  • Because it seems to me more natural to have output which presents arrays as it is started at index 0 instead of index 1 – Wakan Tanka Oct 14 '13 at 14:11
  • If it's just a personal style thing then I can't see the point as you should send Dumper a reference anyway to output: `$VAR1 = [ 'a', 'b', 'c' ]; ` – fugu Oct 14 '13 at 14:13
  • 1
    @WakanTanka The natural way to represent an array with the Data::Dumper module is the way FlyingFrog did above: `$VAR1 = [ 'a', 'b', 'c' ]`. Which is what you get when you do `print Dumper \@arr`. The names of scalar variables are irrelevant, except for human readable purposes. – TLP Oct 14 '13 at 14:16
0

I had a similar need as in the question - I had incoming data with a fixed array, and wanted to know whether the correct indexes are referenced. From the original question, the part about the inner arrays does not seem to be answered, thus maybe this will help somebody.

For an inner array, I'm not aware of a way to do this in a tree, but for a single array just dereference it:

print Dumper(@{$hash->{stuff}->{fields}});
Richlv
  • 3,954
  • 1
  • 17
  • 21