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

Perl Hash of arrays 3

Status
Not open for further replies.

Hemulll

Technical User
Nov 10, 2008
25
RU
Hello All.
How can create from this List Hash with Uniq Key "MSS1" and list values ATS1,ATS2 ...

MSS1;ATS1
MSS1;ATS2
MSS1;ATS3
MSS1;ATS4

MSS2;ATS5
MSS2;ATS6

In my Script i create Hash, but in the Key use only one element of first line, cause this non Uniq.


Please Help ...
 
Hi

Like this ?
Code:
[u]  DB<1> [/u]%whatever = ( 'MSS1' => [ 'ATS1', 'ATS2', 'ATS3', 'ATS4' ], 'MSS2' => [ 'ATS5', 'ATS6' ] );

[u]  DB<2> [/u]print "keys : ", join ", ", keys %whatever 
keys : MSS2, MSS1

[u]  DB<3> [/u]print "values of key MSS1 : ", join ", ", @{$whatever{'MSS1'}}
values of key MSS1 : ATS1, ATS2, ATS3, ATS4

[u]  DB<4> [/u]print "value 2 of key MSS1 : ", ${$whatever{'MSS1'}}[2]
value 2 of key MSS1 : ATS3

Feherke.
 
Hi Feherke.

Exactly so, bu i need to create this Hash from Data.

For example i Read this file in my script, and create Hash from .

I have a trouble with create Uniq hash .

open(FILE, "data.csv") || die("Could not open file!");
my %x = ();
while (<FILE>) {
next if $_ =~ m/^(\s)*$/; # skip blank lines
next if $_ =~ m/^(,)*$/;
@arr = split(/;/, $_);

$Temp{@arr[0]}{@arr[2]} = @arr[1];
}
 
Hi

Code:
open(FILE, "data.csv") || die("Could not open file!");
my %x = ();
while (<FILE>) {
    [red]chomp;[/red]  [gray]# assuming you not want to keep the ending newline[/gray]
    next if $_ =~ m/^\s*$/;  [gray]# skip blank lines[/gray] [red](*)[/red]
    next if $_ =~ m/^,*$/;  [gray]#[/gray] [red](*)[/red]
    [red]my[/red] @arr = split(/;/, $_);
    
    [red]push @{$x{$arr[0]}}, $arr[1];[/red]
}
[red](*)[/red] - Capture groups in your regular expression only when necessary. They slow down the matching.

Feherke.
 
Hello feherke!

Thank you for prompt response!!!

This is my final code.
Code:
while (<FILE>) {
    chomp;  # assuming you not want to keep the ending newline
    next if $_ =~ m/^\s*$/;  # skip blank lines (*)
    next if $_ =~ m/^,*$/;  # (*)
    my @arr = split(/;/, $_);
    
    push @{$x{$arr[1]}}, $arr[2];
}

      
# -- create list with groups 
foreach $key (sort keys %x) {
	print "HierarchicalGroup $key members";
	foreach (@{$x{$key}}) {
		print "$_\n";
    }

}
 
You can shorten it a little:

Code:
while (<FILE>) {
    chomp;
    if (my ($k,$v) = split(/;/)) {
        push @{$x{$k}}, $v;
    }
}

Certainly no major improvement over feherkes well expalined example above. If its a big file it will be a bit faster, if its not a big file then there will be little difference. More of an example to show how some conditions might not be necessary when reading/parsing a file.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hello All.

I have aditional question about:

push @{$x{$arr[0]}}, $arr[1];

This array may be non Uniq, how can I sort this array to Uniq array ?

Can i use Map function for this ?

 
Hello All,

I solve this problem with

Code:
foreach $key (sort keys %MSS) {
	
	my %uniq = map { $_ => 1 } (@{$MSS{$key}});
	my @uniqArr = sort keys %uniq;
	
	print "\nHierarchicalGroup $key children ";
	print WRITE "\nHierarchicalGroup $key children ";
	
	foreach (@uniqArr) {
		#print "$_ ";
		print " $_  ";
		print  WRITE "$_ "; 
    }
    
}
 
You can also use a hash of hashes directly:
Code:
while (<FILE>) {
  chomp;
  if(my($k,$v)=split/;/){
    $MSS{$k}{$v}=1;
  }
}
...
for$key(sort keys%MSS){
  print "HierarchicalGroup $key children  ";
  print WRITE "HierarchicalGroup $key children ";
  for(sort keys%{$MSS{$key}}) {
    print "$_   ";
    print  WRITE "$_ "; 
  }  
    #OR as a faster alternative to the for loop
  my@chi=sort keys%{$MSS{$key}};
  print join('   ',@chi),"\n";
  print WRITE "@chi\n";
}

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Thank you Franco for your prompt response!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top