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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create a hash

Status
Not open for further replies.

dUbbsNIX

MIS
Jul 10, 2003
70
GB
Hi, Observe this output I get from a command ran from my perl script, which I OPEN as a filehande:
eg. open (FILEH, "unix_command_to_generate_output |")

1016 000b 0.2 10000000C9311154 magellan.c1
1016 000b 0.2 10000000C931106D magellan.c2
1017 000c 0.2 10000000C9310EDC columbus.c1
1017 000c 0.2 10000000C9310F95 columbus.c2
1017 000c 0.2 10000000C9311154 magellan.c1
1017 000c 0.2 10000000C931106D magellan.c2
1018 0000 20.1 10000000C9381CE7 tiger.c1
1018 0000 20.1 10000000C9381E92 tiger.c2
1019 0001 20.1 10000000C9381CE7 tiger.c1
1019 0001 20.1 10000000C9381E92 tiger.c2
101a 0002 20.1 10000000C9381CE7 tiger.c1

The elements are split via a newline, so the next thing I do is:

while (<FILEH>)
{
(my $Volume,my $LUN,my $Size,my $Initiator,my $FullHost,my $Rest) = split('/\s+/');

}

Now I really want to lose the duplicates eg. all the .c2's then store the entire lot in a hash indexed by $Volume.

how do I do this?

cheers,

Dave.
 
First things first, you need to explain what is a duplicate and what is not.
For example, with your sample data (looking at the tigers only) which records are duplicates and why? Do we have only one resultant record (just the first c1) or two (the first c1 and c2) or 3 (all the c1 records only)?

Looks to me like you want all the c1's but none of the c2's.
Assuming that:
Code:
%result = ();
while (<FILEH>)
{
  my ($Volume,$LUN,$Size,$Initiator,$FullHost,$Rest) = split('/\s+/');
  next if($FullHost =~ /\.c2$/);
  $result{$Volume} = [$Volume,$LUN,$Size,$Initiator,$FullHost,$Rest];
}
I've stored each record as an array of fields but you could just as easily store as a subhash of fields.
If you need that then reply and one of us will add that feature.



Trojan.
 

many many thanks for your help, basically I've now done this:

while (<FILEH>)
{
my
($Volume,$LUN,$Size,$Initiator,$FullHost,$Rest) = split('/\s+/');
next (my $Host,my $Rest) = split('.',$FullHost);
}
Because I want to lose the .c2 and .c1 suffixs. With this done it is now clearer to see what a duplicate looks like, basically anthing matching the first field which also matches thge last field. ($Host).

What I need now is to add this into a hash somehow probabley indexed to $Volumes, cos I need to report the total size being used by $Host.

Can I do something like this?

my %array = (
'Volume' => my $Volume,
'LUN' => my $LUN,
'Size' => my $Size,
'Initiator' => my $Initiator,
'Host' => my $Host,);



 
this looks wrong to me:

split('/\s+/');

should be:

split(/\s+/);

maybe it will still work with the single-quotes around the forward slashes but it looks like perl will try and split on /\s+/ and not \s+ if it's written with the single-quotes which will be treated as the delimiters instead of the forward slashes.

this also looks wrong:

next (my $Host,my $Rest) = split('.',$FullHost);

maybe you mean:

my ($Host,$Rest) = split(/./,$FullHost);

your use of single quotes as delimiters to the split regexp should be discontinued, I think youi are confusing them with quotes used to define a literal string instead of delimiters for a regexp.


Can I do something like this?

you only need "my" before the hash name:

Code:
my %hash = (
 key => value
);
 
Kevin,
You're absolutely right about the split.
I should have spotted that and didn't. Just cut and pasted the error.
Using my code from above you can do the hash like this:
Code:
$result{$Volume} = {
                  'Volume' => $Volume,
                  'LUN' => $LUN,
                  'Size' => $Size,
                  'Initiator' => $Initiator,
                  'Host' => $Host,
                   };


Trojan.
 

hi thanks again for your help,

my understanding now is that it is not necessary for me to use your %result = (); code to remove the duplicates as hash creation can do this for me, make sense?

SO with that in mind, can you suggest how I create a hash for each host, then use that to provide the sum total of $size for each host?

cheers,

D.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top