use strict;
use warnings;
use 5.010;
my $value;
my $key;
my %hash_keysort = (
servlet => 'true',
servlet_dude => 123,
hai => "null",
array => 123
);
I want to get same output.
I tried with this package use Sort::Naturally
.
Even though I am using natural sort, I am not able to get same output. Every time the key is changing.
Expected output:
servlet : true
servlet_dude : 123
hai : null
array : 123
I tried the nsort
function also. I am not able get as it is keys.
I tried all sort types. I am not able resolve the problem.
I need to get keys every time in same order .But the thing is keys potions are keep on changing..Is there any logic to get keys in same order. My intention is I need to get keys without changing their potions every time.
Update
#!/usr/bin/perl
use Sort::Naturally;
use strict;
use warnings;
my $value;
my $key;
my %hash_keysort = (
servlet => 'true',
servlet_dude => 123,
hai => "null",
array => [ 123, "null", "string" ]
);
for my $k ( sort keys %hash_keysort ) {
printf "%-12s : %s\n", $k, $hash_keysort{$k};
}
Actual output
array : ARRAY(0x8002afb8)
hai : null
servlet : true
servlet_dude : 123
Expected
servlet : true
servlet_dude : 123
hai : null
array : 123
Updated one: #!/usr/bin/perl use strict; use warnings;
#binmode STDOUT, ":utf8";
use utf8;
use Data::Dumper;
use JSON;
use Scalar::Util 'reftype';
my $json;
local $/; #Enable 'slurp' mode
open my $fh, "<", "my.json"; #opening a file
$json = <$fh>;
close $fh;
my $data = decode_json($json); #decodeing the json data
print "$data\n";
print Dumper($data); #prints HASH table
print "\n";
my $key;
my $value;
my $initialtag = ''; #declaring the intial tag of the
data
my $hash = {'HASH'=>9998,'ARRAY'=>9987,'SCALAR'=>9996}; # initializing the
Hash table for opcodes
my $first_key;
my $tag_len;
my $opcode;
my $hex;
my $finalkey_opcode;
my $firstvalue;
my $input;
my $initialtag_2;
my $firstvalue_2;
my $datavalue = check_refernces($data);
if($datavalue){
$datavalue = check_refernces($datavalue);
}
$datavalue = check_refernces($datavalue);
sub Hash_reference { my $input=shift; foreach $key (keys %$input) {
$firstvalue = $input->{$key}; #Acessing the value(Getting
value from hash)
print " the value of key $key $value\n";
$initialtag = $key;
}
print "Variable is HASH\n\n";
$opcode=$hash->{'HASH'}; #If it is hash Getting opcode
for object
print "the opcode is $opcode\n";
my $tag_len = length( $initialtag ); #Finding the length
print "$tag_len\n";
my $hex =sprintf('%04X',$tag_len); #Converting to hexadecimal
format
print "$hex\n";
my $first_key = $hex.$initialtag; #concatenating the Intail tag
and hexadecimal value(length of the data)
print "$first_key\n";
my $finalkey_opcode=$opcode.$first_key; #concatenating the opcode and
data
print "$finalkey_opcode\n";
return $firstvalue;
}
sub Scalar_reference
{
print "Variable is SCALAR refernce\n"; # Not a reference
{
my $input=shift;
$opcode=$hash->{'SCALAR'}; #If it is hash Getting opcode
for object
print "the opcode is $opcode\n";
my $tag_len = length( $initialtag_2 ); #Finding the length
print "$tag_len\n";
my $hex =sprintf('%04X',$tag_len); #Converting to hexadecimal
format
print "$hex\n";
my $first_key = $hex.$initialtag_2; #concatenating the Intail
tag and hexadecimal value(length of the data)
print "$first_key\n";
my $finalkey_opcode=$opcode.$first_key; #concatenating the opcode
and data
print "$finalkey_opcode\n";
return $firstvalue_2;
}
}
sub Array_refernce
{
print "Variable is ARRAY\n\n"; # Reference to a ARRAY
}
sub code_refernce
{
print "Variable is CODE\n\n"; # Reference to a CODE
}
sub check_refernces
{
my $input = shift;
my $returnvalue;
if (ref($input)eq 'SCALAR')
{
Scalar_reference();
}
elsif (ref($input)eq 'HASH')
{
$returnvalue = Hash_reference($input);
# Reference to a hash
}
elsif (ref($input)eq 'ARRAY')
{
Array_refernce();
}
elsif (ref($value)eq 'CODE')
{
code_refernce();
}
}
Data file is:
"web-app": {
"servlet":
{
"servlet-name": true,
"servlet-class": 123,
"hai":null,
"array":[123,null,"string"]
}}
Expected output: servlet : true servlet_dude : 123 hai : null array : ARRAY(0x8002afb8)
How to get the same output.