Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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,, 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 :)

In reply to .csv file in to hash of hashes for a Berkley DB (for a hash newbie) by Doozer

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (5)
    As of 2018-02-20 20:04 GMT
    Find Nodes?
      Voting Booth?
      When it is dark outside I am happiest to see ...

      Results (274 votes). Check out past polls.