Yet another data structure doubt. I will get straight to it.
This is what I have
use strict;
use warnings;
use Data::Dumper;
my $head= undef;
my $tail=\$head;
open FILE, "<datastored.txt" or die $!;
while (<FILE>){
my $node = {
"data" => $_ ,
"next" => undef
};
$$tail=$node;
$tail = \${$node->{"next"}};
};
# #1# Print full list #
print Dumper $head;
# #2# Delete the first node and display data of next node #
$head = $head->{next};
my $value = $head->{data};
and this is the output I get
$VAR1 = {
'next' => \{
'next' => \{
'next' => \{
'next' => \undef,
'data' => 'line 4'
},
'data' => 'line 3
'
},
'data' => 'line 2
'
},
'data' => 'line 1
'
};
Not a HASH reference at linkedlist.pl line 32, <FILE> line 4. **<<<< My Problem and hence my question?**
Note – the content of the file datastored.txt is simply
line 1
line 2
line 3
line 4
If I look at the print result of $head initially, it clearly is a hash within a hash and so on, so why is that after deleting the first node is the integrity of the data structure hampered? (See Error on last line of output)
Perl references are tricky. The dumper output notation
\{is showing that the{next}fields contain references to references to hashes.The offending line is the update to
tail. Try