Sponsored Content
Top Forums Shell Programming and Scripting Perl: Sorting a hash value that is a list. Post 302603110 by rangarasan on Wednesday 29th of February 2012 05:19:20 AM
Old 02-29-2012
perl

Hi,

Try this one,

Code:
#!/usr/bin/perl
use strict;
use Cwd;
my $cwd = cwd;
my $file = $cwd . '/usrgrps.txt';
my $gid;
my $grp;
my $host;
my $group;
my $userid;
my %table = ();
open(FILE, "<", $file) or die "Can't open $file:$!";
while(<FILE>)
{
chomp;
($host, $grp, $gid, $userid) = split(/\:/, $_);
$group = "$grp:$gid";
my @users=split(/\,/,$userid);
if ( scalar(@users) )
{
   foreach my $user ( @users )
   {
      push(@{$table{$group}}, $user) if ( ! grep{/$user/} @{$table{$group}} );
   }
}
else
{
   push @{$table{$group}}, $userid;
}
}
foreach $group (sort keys %table) {
print "$group:";
my @users = @{$table{$group}};
print join ',', sort @users;
print "\n";
}

Cheers,
RangaSmilie
This User Gave Thanks to rangarasan For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print Entire hash list (hash of hashes)

I have a script with dynamic hash of hashes , and I want to print the entire hash (with all other hashes). Itried to do it recursively by checking if the current key is a hash and if yes call the current function again with refference to the sub hash. Most of the printing seems to be OK but in... (1 Reply)
Discussion started by: Alalush
1 Replies

2. Shell Programming and Scripting

Perl Hash:Can not keep hash data in the same order that it was inserted

Can Someone explain me why even using Tie::IxHash I can not get the output data in the same order that it was inserted? See code below. #!/usr/bin/perl use warnings; use Tie::IxHash; use strict; tie (my %programs, "Tie::IxHash"); while (my $line = <DATA>) { chomp $line; my(... (1 Reply)
Discussion started by: jgfcoimbra
1 Replies

3. Shell Programming and Scripting

perl hash - using a range as a hash key.

Hi, In Perl, is it possible to use a range of numbers with '..' as a key in a hash? Something in like: %hash = ( '768..1536' => '1G', '1537..2560' => '2G' ); That is, the range operation is evaluated, and all members of the range are... (3 Replies)
Discussion started by: dsw
3 Replies

4. Shell Programming and Scripting

Sorting keys of a hash in perl

hi all, i have a small problem regarding sorting the keys in a hash. my %hash; for($i=0;$i<19;$i++) { $hash{$i}=$i; } foreach $c (sort keys %hash) { print "\n $hash{$c}"; } (1 Reply)
Discussion started by: niteesh_!7
1 Replies

5. Shell Programming and Scripting

Remove default data hash sorting in perl script?

Hi, I have a datahash with 'n' number of values in perl script. I am writing a xml file from the datahash. I am getting output with sorting(Field sorting). My question is that i don't want any default sorting.whatever i am inserting into datahash it should give same xml file. Any help? ... (0 Replies)
Discussion started by: solo123
0 Replies

6. Shell Programming and Scripting

Sorting the alpha numeric results of Hash

Hi All I've got a perl script that I'm having a problem with when it prints the output of a hash. Some background. I'm trying to merge two file with a similar structure but with different data. Here is a portion of the script I'm using. while (<INPUT>) { my... (0 Replies)
Discussion started by: kingpin2502
0 Replies

7. Shell Programming and Scripting

Perl hash help

Hi , i have the below code its working fine when i execute in unix , but its not working in windows could you pls explain me where i am going wrong. This is the program $data = { '1' => 'one' , '2' => 'two' , 3 => 'three' }; print "hello : $data->{'1'}... (2 Replies)
Discussion started by: ragilla
2 Replies

8. Shell Programming and Scripting

Sorting values of hash in ascending order using Perl

I want to sort values of a hash in ascending order. my %records; for my $value (sort values %records){print $value,"\n";} When I use the above code I get values in this order: 1,10,11,2,3,4,5,6,7,8,9. But, I need values in my output in this order: 1,2,3,4,5,6,7,8,9,10,11. Can Someone... (1 Reply)
Discussion started by: koneru_18
1 Replies

9. Shell Programming and Scripting

Compare values of hashes of hash for n number of hash in perl without sorting.

Hi, I have an hashes of hash, where hash is dynamic, it can be n number of hash. i need to compare data_count values of all . my %result ( $abc => { 'data_count' => '10', 'ID' => 'ABC122', } $def => { 'data_count' => '20', 'ID' => 'defASe', ... (1 Reply)
Discussion started by: asak
1 Replies

10. Programming

Perl: restrict perl from automaticaly creating a hash branches on check

My issue is that the perl script (as I have done it so far) created empty branches when I try to check some branches on existence. I am using multydimentional hashes: found it as the best way for information that I need to handle. Saing multidimentional I means hash of hashes ... So, I have ... (2 Replies)
Discussion started by: alex_5161
2 Replies
Text::vFile::asData(3pm)				User Contributed Perl Documentation				  Text::vFile::asData(3pm)

NAME
Text::vFile::asData - parse vFile formatted files into data structures SYNOPSIS
use Text::vFile::asData; open my $fh, "foo.ics" or die "couldn't open ics: $!"; my $data = Text::vFile::asData->new->parse( $fh ); DESCRIPTION
Text::vFile::asData reads vFile format files, such as vCard (RFC 2426) and vCalendar (RFC 2445). DATA STRUCTURE
A vFile contains one or more objects, delimited by BEGIN and END tags. BEGIN:VCARD ... END:VCARD Objects may contain sub-objects; BEGIN:VCALENDAR ... BEGIN:VEVENT ... END:VEVENT ... ENV:VCALENDAR Each object consists of one or more properties. Each property consists of a name, zero or more optional parameters, and then a value. This fragment: DTSTART;VALUE=DATE:19970317 identifies a property with the name, "DSTART", the parameter "VALUE", which has the value "DATE", and the property's value is 19970317. Those of you with an XML bent might find this more recognisable as: <dtstart value="date">19970317</dtstart> The return value from the "parse()" method is a hash ref. The top level key, "objects", refers to an array ref. Each entry in the array ref is a hash ref with two or three keys. The value of the first key, "type", is a string corresponding to the type of the object. E.g., "VCARD", "VEVENT", and so on. The value of the second key, "properties", is a hash ref, with property names as keys, and an array ref of those property values. It's an array ref, because some properties may appear within an object multiple times with different values. For example; BEGIN:VEVENT ATTENDEE;CN="Nik Clayton":mailto:nik@FreeBSD.org ATTENDEE;CN="Richard Clamp":mailto:richardc@unixbeard.net ... END:VEVENT Each entry in the array ref is a hash ref with one or two keys. The first key, "value", corresponds to the property's value. The second key, "param", contains a hash ref of the property's parameters. Keys in this hash ref are the parameter's name, the value is the parameter's value. (If you enable the "preserve_params" option there is an additional key populated, called "params". It is an array ref of hash refs, each hash ref is the parameter's name and the parameter's value - these are collected in the order they are encountered to prevent hash collisions as seen in some vCard files) line.) The third key in the top level "objects" hash ref is "objects". If it exists, it indicates that sub-objects were found. The value of this key is an array ref of sub-objects, with identical keys and behaviour to that of the top level "objects" key. This recursive structure continues, nesting as deeply as there were sub-objects in the input file. The "bin/v2yaml" script that comes with this distribution displays the format of a vFile as YAML. "t/03usage.t" has examples of picking out the relevant information from the data structure. AUTHORS
Richard Clamp <richardc@unixbeard.net> and Nik Clayton <nik@FreeBSD.org> COPYRIGHT
Copyright 2004, Richard Clamp and Nik Clayton. All Rights Reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. CAVEATS
We don't do any decoding of property values, including descaping ",", we're still undecided as to whether this is a bug. BUGS
Aside from the TODO list items, none known. SEE ALSO
Text::vFile - parses to objects, doesn't handle nested items RFC 2426 - vCard specification RFC 2445 - vCalendar specification perl v5.10.1 2010-08-25 Text::vFile::asData(3pm)
All times are GMT -4. The time now is 12:44 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy