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

Perl read file into hash 3

Status
Not open for further replies.

Hemulll

Technical User
Nov 10, 2008
25
RU
Hello Perl guru!

I wan't to create hash, from next data structure:

fragment from cvs file.

SportChannels
Euro Sport news, 239.255.2.20
EuroSport1, 239.255.2.48
EuroSport2, 239.255.2.49
ESPN classic Sport, 239.255.2.85
Viasat Sport, 239.255.2.110
Extreme Sports, 239.255.2.51
Music
VH classic, 239.255.2.78
MTV Hits, 239.255.2.79
VH1 RUS, 239.255.2.80

a question is:
How to create one hash with all values, something like this.....
Hash->GroupName->Channel->Address

A lot of thanks
 
Hi

Code:
%Hash={};
while (chomp($s=<>)) {
  @p=split /, */,$s;
  if (scalar @p==1) {
    $Hash{$g=$s}={};
  } else {
    $Hash{$g}{$p[0]}=$p[1];
  }
}

print $Hash{"Music"}{"VH classic"}; [gray]# prints '239.255.2.78'[/gray]

Feherke.
 
Hello feherke.

Thank you for response, i have additional question:

if i print this Hash, i have next structure

use Data::Dumperl;
print Dumper($Hash);

Output:
$VAR27 = 'Music;';
$VAR28 = {
'MTV Hits' => '239.255.2.79',
'Music Box RU' => '239.255.2.37',
'BridgeTV' => '239.255.2.112',
'A-One' => '239.255.2.66',
'Trace TV' => '239.255.2.81',
'VH classic' => '239.255.2.78',
'MCM' => '239.255.2.33'
};
$VAR29 = 'HDTV;';
$VAR30 = {
'' => undef,
'MelodyZen' => '239.255.0.122',
'VOOM HD' => '239.255.0.120',
'LUXE TV HD' => '239.255.0.121'
My question is:

After file read i nedd to output this hash as next structure, for example;
HierarchicalGroup members HDTV 239.255.0.122 239.255.0.120 239.255.0.121

May be i need to build Hash like "Hash{HDTV}->{MelodyZen}->{239.255.0.122}
OR
"Hash{HDTV}->(Array with ip addresses)

How can i build this hashes?

A lot of thanks.




 
If i understand right, so now is Hash of hash, and will be hash of hash of hash - 3 hashes, Right ?
 
Sorry, this is my code:
my $file = "channelCvs.csv";
open(DAT, $file) || die("Could not open file!");

%Hash={};
#foreach $s (<DAT>) {
while (chomp($s=<DAT>)) {
#s/#.*//; # ignore comments by erasing them
next if $s =~ m/^(\s)*$/; # skip blank lines
next if $s =~ m/^(,)*$/;
next if $s =~ m/Mcast address*$/;

chomp($s); # remove the newline from $line.
#@p = split(';',$s);
@p=split /, */,$s;
if (scalar @p==1) {
#print $p[0],"\n";
$Hash{$g=$s}={};
} else {
$Hash{$g}{$p[0]}=$p[1];
}
}
and hash is:

$VAR114 = {};
$VAR115 = '2*2;239.255.2.115';
$VAR116 = {};
$VAR117 = 'Total Hits (Germany);239.255.0.178';
$VAR118 = {};
$VAR119 = 'Country Stars;239.255.0.166';
$VAR120 = {};
$VAR121 = 'Strictly 60\'s;239.255.0.155';
$VAR122 = {};
$VAR123 = 'WorldMadeChannel;239.255.2.52';
 
Looks like you need a hash of hashes of arrays

$hash->{groupname}->[channels]

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

Thank you for response!

From this hash:$VAR27 = 'Music;';
$VAR28 = {
'RU-TV' => '239.255.2.111',
'Mezzo' => '239.255.2.34',
'Music Box TV' => '239.255.2.60',

};

How can i print in this format?:
Group Music members 239.255.2.111 239.255.2.34 239.255.2.60

 
This Hash looks incorrect, beacause $var 27 and 28 needs to be together right ?
Needs to be
$Hash{Music}{Mezzo}{239.255.2.34}
 
Hi

Sorry Hemulll, but I am lost.

To print the IP addresses of all channels in a given group ( after my previous code ) :
Code:
$g="Music";
print "Group $g members : ",join " ",values %{$Hash{$g}};
Anyway, I do not understand what data manipulations you want to perform. It could be better to change to arrays as proposed by Kevin.

Feherke.
 
Hello feherke !

Thank you for response!!!!

My issue is:

I have cvs file with channels:

Music;
MCM;239.255.2.33
Mezzo;239.255.2.34
Trace TV;239.255.2.81
?2 tv;239.255.2.46
MTV 2;239.255.2.76
MTV Dance;239.255.2.77
VH classic;239.255.2.78
MusicChoice (audio channels);
Total Hits;239.255.0.150
The Biggest Rock Anthems Ever;239.255.0.151
The Perfect Dinner Party;239.255.0.152
The Alternative;239.255.0.153

So, Music this is Group name
MCM - this is Chanel that member of group Music and 239.255.2.33 - this is MCM ip address.
So, i need to create next format:
HierarchicalGroup Music members 239.255.2.33 239.255.2.34 239.255.2.46 ......

How can i realize this ?
 
I think that i need to create next Hash.
$hash{Music}->(Array List with ip addresses).

How can i realize this
 
To create and add elements to an array of hashes and to parse it:
Code:
push @{$Hash{$key}},$element;
#....
for my$key(keys%Hash){
  print"$key\n";
  for(@{$Hash{$key}}){
    print"\t$_\n";
  }
}

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Hemull,

is this school work of some kind?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
This Hash looks incorrect, beacause $var 27 and 28 needs to be together right ?
Needs to be
$Hash{Music}{Mezzo}{239.255.2.34}

Thats only because you are not using Data::Dumper correctly. You should pass a reference to the Dumper function:

print Dumper \%Hash;

This line in the code you posted:

%Hash={};

Should really be:

%Hash=();

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

Part and Inventory Search

Sponsor

Back
Top