Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Working with %hash

Status
Not open for further replies.

biobrain

MIS
Joined
Jun 21, 2007
Messages
90
Location
GB
Here is a simple %list i have written


Code:
@array="z,b,c,d,e,f";
@array2="10,2,3,4,5,6";

%list=(

@array => @array2);

@Allkeys =sort(keys(%list));
foreach $Thekey (@Allkeys)
{print "$Thekey\n"}


@Allval =sort(values(%list));
{print "@Allval \n"};

The output is
z,b,c,d,e,f
10,2,3,4,5,6

But I want to print it like this i.e vertically
z,10
b,2
c,3
d,4
e,5
f,6
Please guide me how to do this.

Regards
 
Your code is wrong in many ways. These are not really arrays in the sense that I think you think they are:

Code:
@array="z,b,c,d,e,f";
@array2="10,2,3,4,5,6";

if you print the first element of @array you will see what I mean:

Code:
@array="z,b,c,d,e,f";
print $array[0],"\n";

the output is: "z,b,c,d,e,f", instead of "z", because your code is the same as:

Code:
$array[0]="z,b,c,d,e,f";

which assigns the string "z,b,c,d,e,f" to the first index of the array: [0]

The arrays should be using () to populate them correctly:

Code:
@array = ('z', 'b', 'c', 'd', 'e', 'f');
print $array[0],"\n";

output is: "z"

You can notice in the code you posted the sort() function had no affect on sorting the data because there is nothing to sort. All you have are two strings.

The correct way to construct a hash from the two arrays is like so:

Code:
my @array = ('z', 'b', 'c', 'd', 'e', 'f');
my @array2 = (10, 2, 3, 4, 5, 6);
my %list;
@list{@array}=@array2;

now you can loop through hash %list and sort the keys or the values and print them out like you want. Give it a try.





------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Dear Friend

Thanks for your detailed reply.

Well here is another question

If I have two %hash like %hash1


A =>Name1
B =>Name2
C =>Name3

and %hash2

A =>Address1
B =>Address2
C =>Address3

Now you see A, B, and C are common and equal in both hash.

Now I want to print it like that

The name of A is Name 1 and address is Address 1
The name of B is Name 2 and address is Address 2
The name of C is Name 3 and address is Address 3

i.e I want to match the keys in the two hash and then to combine their value.
 
if both hashes use the same key:

Code:
foreach my $keys (sort keys %hash)
   print "The name of A is $hash{$keys} and address is $hash1{$keys}\n";
}

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
should be:

Code:
foreach my $keys (sort keys %hash)
   print "The name of [red]$keys[/red] is $hash{$keys} and address is $hash1{$keys}\n";
}



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top