Hi,
I have created the hash.
%hash;
@arr1 = qw(Dealnum AdminStatus adminReason effFrom effTo);
@arr2 = qw(121212121 YES 1992-06-19T05:14:27 );
@hash{@arr1}=@arr2;
foreach(sort keys %hash){
print "$_ ---- $hash{$_}\n";
}
The output i got like this:
C:\strawberry\perl\bin>perl sort.pl
AdminStatus ----> YES
Dealnum ----> 121212121
adminReason ----> 1992-06-19T05:14:27
effFrom ---->
effTo ---->
I want to retain the same order as in the arr1. The output should be:
Dealnum ----> 121212121
AdminStatus ----> YES
adminReason ----> 1992-06-19T05:14:27
effFrom ----> NULL
effTo ---> NULL
How can i get the elements printed as the same order of @arr1 in Hash?
Regards
Vanitha
you can use this
%hash;
@arr1 = qw(Dealnum AdminStatus adminReason effFrom effTo);
@arr2 = qw(121212121 YES 1992-06-19T05:14:27 );
@hash{@arr1}=@arr2;
foreach(@arr1){
print "$_ ---- $hash{$_}\n";
}
o/p
Dealnum ---- 121212121
AdminStatus ---- YES
adminReason ---- 1992-06-19T05:14:27
effFrom ----
effTo ----
you have to do a Null string checking for 'NULL' print
---------- Post updated at 11:30 AM ---------- Previous update was at 11:14 AM ----------
Use this with NULL print
%hash;
@arr1 = qw(Dealnum AdminStatus adminReason effFrom effTo);
@arr2 = qw(121212121 YES 1992-06-19T05:14:27 );
@hash{@arr1}=@arr2;
foreach(@arr1){
if ( !defined $hash{$_} || $hash{$_} eq "" )
{
print "$_ ---- NULL\n";
}
else
{
print "$_ ---- $hash{$_}\n";
}
}
o/p
Dealnum ---- 121212121
AdminStatus ---- YES
adminReason ---- 1992-06-19T05:14:27
effFrom ---- NULL
effTo ---- NULL