Hi,
I have been working with perl for a little while and have now entered in to the world of hashes. I will outline what I want to achieve and then show examples of my data and scripts below.
I currently have a .csv file which holds information for 324 pieces of equipment (Set Top Boxes). The first column holds our unique equipment locator and the following columns hold further information such as serial number, date installed, asset numbers, etc.
Each piece of equipment has its own row which is comma delimited (example below):
Zone1 - Box1,01/06/2011,Amstrad,4F3107,0362940644,540 442 969,R007.061.38.00U,R007.061.38.00U - 03/06/2013,, - R007.061.38.00U,,172.16.121.4 D,00:19:fb:2c:56:2c,03/06/2013,0,03/06/2013
You can see from the example that the first field is our equipment locator ID which I would like to use as the main hash keys. I would then like to have the remaining details as hash values for that individual Set Top Box. This is kind of what I would want:
our %h;
%h = (
'Zone1 - Box1' => {
'Date Active' => 'Value',
'Manufacturer' => 'Value',
'HW Version' => 'Value',
'Serial' => 'Value',
'Card No' => 'Value',
'Current Software' => 'Value',
'OS Version' => 'Value',
'EPG Version' => 'Value',
'Box Type' => 'Value',
'Designation' => 'Value',
'IP Address' => 'Value',
'MAC Address' => 'Value',
'Code Date' => 'Value',
},
'Zone1 - Box2' => {
'Date Active' => 'Value',
'Manufacturer' => 'Value',
'HW Version' => 'Value',
'Serial' => 'Value',
'Card No' => 'Value',
'Current Software' => 'Value',
'OS Version' => 'Value',
'EPG Version' => 'Value',
'Box Type' => 'Value',
'Designation' => 'Value',
'IP Address' => 'Value',
'MAC Address' => 'Value',
'Code Date' => 'Value',
},
)
The aim is to be able to store all of the information for each Set Top Box but also make it searchable so I can select a Set Top Box by its unique ID (Zone1 - Box1 for example) and it will show me all of the details.My code is below.
#!/usr/bin/perl
use strict;
use warnings;
use DB_File ;
our (%hash) ;
unlink "STB_Box_DUT_Details" ;
tie %hash, "DB_File", "STB_Box_DUT_Details", O_RDWR|O_CREAT, 0666, $DB
+_HASH || die "Cannot open file 'STB_Box_DUT_Details': $!\n";
my $stbdetails = '/path/to/my/file.csv';
my $search = $ARGV[0];
if (defined $search) {
chomp $search;
}
open DUT, "<$stbdetails" or die $!;
my @boxes = <DUT>;
close DUT;
my $count = '0';
foreach my $line (@boxes) {
$count++;
my @box = split (',', $line);
my $location = $box[0];
my $date_active = $box[1];
my $manufacturer = $box[2];
my $hwver = $box[3];
my $serial = $box[4];
my $cardno = $box[5];
my $currentsw = $box[6];
my $os_ver = $box[7];
my $epg_ver = $box[8];
my $boxtype = $box[9];
my $designation = $box[10];
my $boxip = $box[11];
my $boxmac = $box[12];
my $codedate = $box[13];
$hash{$location} = {
'Date Active' => $date_active,
'Manufacturer' => $manufacturer,
'HW Version' => $hwver,
'Serial' => $serial,
'Card No' => $cardno,
'Current Software' => $currentsw,
'OS Version' => $os_ver,
'EPG Version' => $epg_ver,
'Box Type' => $boxtype,
'Designation' => $designation,
'IP Address' => $boxip,
'MAC Address' => $boxmac,
'Code Date' => $codedate,
};
}
print "STBs added to the hash - $count\n";
foreach my $STB (keys %hash) {
if ($STB =~ /$search/) {
while ( (my $key1, my $value1) = each %{ $hash{$STB}}) {
print "$key1 is $value1\n";
}
}
}
If I execute this script it reports that all 324 boxes were added to the hash. If I set a box reference for $ARGV[0] ($search) I keep on getting "Can't use string ("HASH(0x7ff9aa090b58)") as a HASH ref while "strict refs" in use" error. I don't think I am adding the new details for each box to the hash correctly. Any help would be very much appreciated :)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.