teun-arno has asked for the wisdom of the Perl Monks concerning the following question:
I have a question about the following :
use Data::Dumper;
# it seems that ', ,' in the third record is not producing a index in
+the array.
$arr= [
[1,2,3,4,5,6,7,8,9 ], #works as expected
[1,2,3,'',5,6,7,'',9 ], #works as expected
[1,2,3, ,5,6,7, ,9 ], #does not work as expected "empty fields "
+ are ignored
];
my $dd=Data::Dumper->new([$arr],[ qw(arr) ] )->Indent(1)->Quotekeys(0)
+->Dump;
print $dd;
$str_arr=q`$arr= [
[1,2,3,4,5,6,7,8,9 ],
[1,2,3,'',5,6,7,'',9 ],
[1,2,3, ,5,6,7, ,9 ],
];` ;
print "\n\n";
print $str_arr ;
$str_arr=~ s/,\s*,/,'',/g; # filling in the empty fields
print $str_arr ;
$arr = undef;
$arr = eval $str_arr;
my $dd=Data::Dumper->new([$arr],[ qw(arr) ] )->Indent(1)->Quotekeys(0)
+->Dump;
print $dd;
# this seems to work, next question is : The original $arr is send to
+a subroutine.
# How to stringify that in the subroutine without hardcoding this. ( a
+s I have done in the above ) !
#
Thanks for your time dear perl monks.
Re: Perl reference array
by AnomalousMonk (Archbishop) on Mar 28, 2015 at 23:12 UTC
|
Perl's list constructors flatten empty lists and arrays when building new lists. As LanX has written, there is no such thing as an "empty" element. You may define a placeholder value (e.g., undef or the empty string) for the use of your own application along with a custom parser (or maybe YAML/JSON/Text::CSV as LanX suggested), but that's as close as you can get.
c:\@Work\Perl>perl -wMstrict -le
"use Data::Dump;
;;
my @ar = (99, 88);
my @empty = ();
;;
my $ar = [ 1, @ar, 2, @empty, 3, (), ,,,,, (), (),(), 4 ];
dd $ar;
"
[1, 99, 88, 2, 3, 4]
Give a man a fish: <%-(-(-(-<
| [reply] [d/l] [select] |
Re: Perl reference array
by LanX (Saint) on Mar 28, 2015 at 21:06 UTC
|
Hello teun-arno
Welcome to the monastery! :)
Please try to word out real questions instead of mumbling stuff in comments.
I'm not sure what your problem is, "empty field" in Perl is undef not an empty string and it has to be explicitly inserted.
A regex solution on pseudo-code is doomed to fail and will byte you in form of an ugly, hard to track bug.
| [reply] |
Re: Perl reference array
by CountZero (Bishop) on Mar 29, 2015 at 12:05 UTC
|
You have to go one step further back. Your data must be coming from somewhere.Perhaps it is a CSV file (or similar) and in that case Text::CSV can help you to deal with the empty fields. Or perhaps the data are generated by another program and piped to your program or your program picks the data up through its STDIN. In those cases you will have to channel the data through a filter that will replace empty or missing fields with undef, before you hand over the data for further processing. It will all depend on where the data is coming from and where it is going to. .
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James My blog: Imperial Deltronics
| [reply] [d/l] [select] |
Re: Perl reference array
by teun-arno (Acolyte) on Mar 28, 2015 at 21:27 UTC
|
The problem is that the items between two ,, are not taken into the index of the created array :
$arr= [
[1,2,3,4,5,6,7,8,9 ], #works as expected
[1,2,3,'',5,6,7,'',9 ], #works as expected
[1,2,3, ,5,6,7, ,9 ], #does not work as expected "empty fields "
+ are ignored
];
my $dd=Data::Dumper->new([$arr],[ qw(arr) ] )->Indent(1)->Quotekeys(0)
+->Dump;
print $dd;
$arr = [
[
1,
2,
3,
4,
5,
6,
7,
8,
9
],
[
1,
2,
3,
'',
5,
6,
7,
'',
9
],
[
1,
2,
3,
5,
6,
7,
9
]
];
as you can see from the dump in the last record there are less fields then I mentioned in the definition of $arr. When I quote the "emty fields" everything is OK. however when I just use a sequence of , , or ,, the index of the array is not updated. Hence the less fields in the last record. Should expect that all records have the same number of indexes, which is not the case.
Is there a way that the "empty fields" , as i call them can be initialized to undef. In my opinion this should be the case.
Thanks in advance
Greeting ,
| [reply] [d/l] |
|
[1,2,3, undef, 5,6,7, undef ,9 ]
the only way to facilitate things is to define your own input string syntax:
Like a sub row() which returns the above from a "simplified" list row(qw(1 2 3 . 5 6 7 . 9))
Or alternatively to parse() a multiline-string representing a matrix, like
$AoA = parse <<'__matrix__';
1 2 3 4 5 6 7 8 9
1 2 3 . 5 6 7 . 9
1 2 3 . 5 6 7 . 9
__matrix__
YMMV according to your real problem.
> In my opinion this should be the case.
It's a feature of Perl, other stuff would break in this "case", sorry!
| [reply] [d/l] [select] |
|
| [reply] |
|
|
|