Hi
whn said:
1) Where does that warning come from? How to get rid of it?
That is because :
[ul]
[li]You turned on warnings. ( By the way, unless you want to turn warnings on/off for certain blocks, the [tt]-w[/tt] switch and [tt]use warnings[/tt] pragma are equivalent. )[/li]
[li]Personally I prefer to enjoy Perl's flexibility and ignore warnings. Despite that I try to not force the edges too much when giving advices, there are catchy situations I miss.[/li]
[/ul]
In other words, at the end of input, the last value returned by the [tt]<>[/tt] operation is [tt]undef[/tt]. Using [tt]chomp()[/tt] on a variable with [tt]undef[/tt] value, gives that warning :
Code:
[blue]master #[/blue] perl -e 'chomp $x'
[blue]master #[/blue] perl -we 'chomp $x'
Name "main::x" used only once: possible typo at -e line 1.
Use of uninitialized value $x in scalar chomp at -e line 1.
[blue]master #[/blue] perl -e '$x=undef; chomp $x'
[blue]master #[/blue] perl -we '$x=undef; chomp $x'
Use of uninitialized value $x in scalar chomp at -e line 1.
To get rid of that warning just move the [tt]chomp()[/tt] call from the condition inside the loop :
Code:
[b]while[/b] [teal]([/teal][b]my[/b] [navy]$line[/navy] [teal]=[/teal] [green][i]<DATA>[/i][/green][teal])[/teal] [teal]{[/teal] [gray]# This is line 9[/gray]
[b]chomp[/b] [navy]$line[/navy][teal];[/teal]
whn said:
2) Why didn't my implementation get rid of --
i) the NULL string, which is what I expected and wanted;
In that DATA line the field indexes are
[pre]
[gray][highlight #fcc]0 [/highlight] [highlight]1 [/highlight] [highlight #cfc]2 [/highlight] [highlight #ccf]3 [/highlight] # from left[/gray]
[highlight #fcc]x11[/highlight] : : [highlight #cfc]x13[/highlight] : [highlight #ccf]value1[/highlight]
[gray][highlight #fcc]-4 [/highlight] [highlight]-3[/highlight] [highlight #cfc]-2 [/highlight] [highlight #ccf]-1 [/highlight] # from right[/gray]
[/pre]
Even more, you tested
after the loop, in that moment the key with the empty string was already created.
Better handle it on [tt]split()[/tt] by splitting not on each colon ( : ), but on one or more consecutive colons :
Code:
[b]my[/b] [navy]@field[/navy] [teal]=[/teal] [b]split[/b] [green][i]/:[highlight]+[/highlight]/[/i][/green][teal],[/teal] [navy]$line[/navy][teal];[/teal]
whn said:
ii) the '0', which is what I expected, but not what I wanted
Similarly, in that DATA line '0' was at index -4, not the tested one.
One minor thing. I find that instructions which condition the execution of the loop body are better to do [tt]next[/tt] ( or [tt]continue[/tt] in other languages ) then to control a huge block enclosing the entire loop block.
So finally I this is what I have now :
Perl:
[gray]#!/usr/bin/perl -w[/gray]
[b]use[/b] strict[teal];[/teal]
[b]use[/b] warnings[teal];[/teal]
[b]use[/b] Data[teal]::[/teal]Dumper[teal];[/teal]
[b]my[/b] [navy]%cache[/navy] [teal]=[/teal] [teal]();[/teal]
[b]while[/b] [teal]([/teal][b]my[/b] [navy]$line[/navy] [teal]=[/teal] [green][i]<DATA>[/i][/green][teal])[/teal] [teal]{[/teal] [gray]# This is line 9[/gray]
[b]chomp[/b] [navy]$line[/navy][teal];[/teal]
[highlight #ffc][b]next[/b] [b]if[/b] [navy]$line[/navy] [teal]=~[/teal] [green][i]/^#/[/i][/green][teal];[/teal][/highlight]
[b]my[/b] [navy]@field[/navy] [teal]=[/teal] [b]split[/b] [green][i]/:+/[/i][/green][teal],[/teal] [navy]$line[/navy][teal];[/teal]
[b]my[/b] [navy]$parent[/navy] [teal]=[/teal] [teal]\[/teal][navy]%cache[/navy][teal];[/teal]
[b]for[/b] [teal]([/teal][b]my[/b] [navy]$i[/navy] [teal]=[/teal] [purple]0[/purple][teal],[/teal] [b]my[/b] [navy]$l[/navy] [teal]=[/teal] scalar [navy]@field[/navy][teal];[/teal] [navy]$i[/navy] [teal]<[/teal] [navy]$l[/navy] [teal]-[/teal] [purple]2[/purple][teal];[/teal] [navy]$i[/navy][teal]++)[/teal] [teal]{[/teal]
[navy]$parent[/navy][teal]->[/teal][teal]{[/teal][navy]$field[/navy][teal][[/teal][navy]$i[/navy][teal]][/teal][teal]}[/teal] [teal]=[/teal] [teal]{}[/teal] [b]unless[/b] [b]defined[/b] [navy]$parent[/navy][teal]->[/teal][teal]{[/teal][navy]$field[/navy][teal][[/teal][navy]$i[/navy][teal]][/teal][teal]}[/teal][teal];[/teal]
[navy]$parent[/navy] [teal]=[/teal] [navy]$parent[/navy][teal]->[/teal][teal]{[/teal][navy]$field[/navy][teal][[/teal][navy]$i[/navy][teal]][/teal][teal]}[/teal][teal];[/teal]
[teal]}[/teal]
[navy]$parent[/navy][teal]->[/teal][teal]{[/teal][navy]$field[/navy][teal][-[/teal][purple]2[/purple][teal]][/teal][teal]}[/teal] [teal]=[/teal] [navy]$field[/navy][teal][-[/teal][purple]1[/purple][teal]];[/teal]
[teal]}[/teal]
[b]print[/b] Dumper [teal]\[/teal][navy]%cache[/navy][teal];[/teal]
__DATA__
# the 2nd element is NULL
x11::x13:value1
# the 2nd element is zero
x21:0:x23:x24:value2
x31:x32:value3
Feherke.
[link feherke.github.com/]
[/url]