wynnmc has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to set up a data structure to hold the information in /usr/share/misc/pci.ids. After going backwards and forwards in Programming Perl Ch 9 I tried reading the file in and creating the structure, didn't work. I tried running the sub's with fixed data from a record in the file: didn't work. Finally I created a structure manually but I can't print it out. I think there must be a hole where knowledge of references should be. The structure is
my %pci_ids = { "9710", devices => { "NetMos Technology", devices => [ { devid => "7780", devname => "USB IRDA-port" }, { devid => "9805", devname => "PCI 1 port parallel adapter" }, { devid => "9815", devname => "PCI 9815 Multi-I/O Controller", subinfo => [ { subv => "1000", subdev => "0020", subsys =>"2P0S (2 port paral +lel adaptor)" } ] }, { devid => "9835", devname => "PCI 9835 Multi-I/O Controller", subinfo => [ { subv => "1000", subdev =>"0002", subsys =>"2S (16C550 UART)" +}, { subv => "1000", subdev => "0012", subsys => "1P2S" } ] }, { devid => "9845", devname => "PCI 9845 Multi-I/O Controller", subinfo => [ { subv => "1000", subdev => "0004", subsys => "0P4S (4 port 165 +50A serial card)" }, { subv => "1000", subdev => "0006", subsys =>"0P6S (6 port 1655 +0a serial card)" } ] }, { devid => "9855", devname => "PCI 9855 Multi-I/O Controller", subinfo => [ { subv => "1000", subdev => "0014", subsys => "1P4S" } ] } ] } };
To print it I have
my $href; for $href (keys %pci_ids) { print "Vendor Id: $href:\n"; print("pci_ids{href}: ", ${$href}{devices}, "\n"); }
I think I'm in out of my depth.

Replies are listed 'Best First'.
Re: pci.ids to a complex data structure
by davidrw (Prior) on Aug 20, 2006 at 20:15 UTC
    Your my %pci_ids declaration isn't right .. first, should be (), not {} since it's a hash, not a hashref. second, ( "9710", devices => { ... } ) is an odd number of elements that will be complained about (you're using use strict; use warnings; right?)

    In your print attempt, i think (not sure since the %pci_ids isn't right) that you want the values, not the keys ..

    check out perldata and perldsc for more info, too
Re: pci.ids to a complex data structure
by rminner (Chaplain) on Aug 20, 2006 at 20:30 UTC
    I recommend that you consider using "use strict;" and "use warnings;". Your hash %pci_ids isn't even valid.
    "9710", devices => { "NetMos Technology",
    if you had turned on warnings, you would have gotten:
    perl pci_ids.pl Odd number of elements in anonymous hash at pci_ids.pl line 4. Odd number of elements in anonymous hash at pci_ids.pl line 4. Reference found where even-sized list expected at pci_ids.pl line 4. $VAR1 = { 'HASH(0x1a62388)' => undef };
    Also you are using curly braces where you should be using round braces - it should be:
    %pci_ids = ( #or $pci_ids = {
    In general it is advisable to use
    use strict; use warnings;
    and if you don't understand the error messages
    use diagnostics;
    which makes the error message a bit more verbose.
Re: pci.ids to a complex data structure
by graff (Chancellor) on Aug 21, 2006 at 01:31 UTC
    I am trying to set up a data structure to hold the information in /usr/share/misc/pci.ids.

    I gather this is a linux-on-intel kind of thing (on a laptop with pci slots?) -- alas, those of us who don't have this sort of setup are clueless about what the "pci.ids" file contains or how it's organized. (My macosx has a /usr/share/misc directory, but no pci.ids file.) Could you post a sample of the data, and/or some reference info about the file format?

    And can you show us your code that attempted to read the file into a hash structure? I'm sure you would rather read from the file, instead of having to hard-code the file contents into a perl script...

      From /usr/share/misc/pci.ids on my (Debian) Linux box:
      # Vendors, devices and subsystems. Please keep sorted. # Syntax: # vendor vendor_name # device device_name <-- single tab # subvendor subdevice subsystem_name <-- two tabs
      And some sample data:
      # Formerly NCR 1000 LSI Logic / Symbios Logic 0001 53c810 1000 1000 LSI53C810AE PCI to SCSI I/O Processor 0002 53c820 0003 53c825 1000 1000 LSI53C825AE PCI to SCSI I/O Processor (Ultr +a Wide) 0004 53c815 0005 53c810AP 0006 53c860 1000 1000 LSI53C860E PCI to Ultra SCSI I/O Processor 000a 53c1510 1000 1000 LSI53C1510 PCI to Dual Channel Wide Ultra2 +SCSI Controller (Nonintelligent mode) 000b 53C896/897 1000 1000 LSI53C896/7 PCI to Dual Channel Ultra2 SCSI + Multifunction Controller 1000 1010 LSI22910 PCI to Dual Channel Ultra2 SCSI ho +st adapter 1000 1020 LSI21002 PCI to Dual Channel Ultra2 SCSI ho +st adapter # multifunction PCI card: Dual U2W SCSI, dual 10/100TX, graphics 13e9 1000 6221L-4U 000c 53c895 1000 1010 LSI8951U PCI to Ultra2 SCSI host adapter 1000 1020 LSI8952U PCI to Ultra2 SCSI host adapter 1de1 3906 DC-390U2B SCSI adapter 1de1 3907 DC-390U2W 000d 53c885 000f 53c875 0e11 7004 Embedded Ultra Wide SCSI Controller 1000 1000 LSI53C876/E PCI to Dual Channel SCSI Contro +ller 1000 1010 LSI22801 PCI to Dual Channel Ultra SCSI hos +t adapter 1000 1020 LSI22802 PCI to Dual Channel Ultra SCSI hos +t adapter 1092 8760 FirePort 40 Dual SCSI Controller 1de1 3904 DC390F/U Ultra Wide SCSI Adapter 4c53 1000 CC7/CR7/CP7/VC7/VP7/VR7 mainboard 4c53 1050 CT7 mainboard

      I started to have a play around with this earlier this morning, but I'm afraid that I'm a bit busy at work now. If I get time to have another go at it later I'll update.

        The following might not be exactly the structure that you really want, but it does store all the pci.id file content without loss of information or structure. It differs from the OP layout (which maybe wasn't what you really wanted either) in that here, the various ID field values from the file are used as hash keys.

        It's a bit clunky, because the "descriptive" fields are held as values of "special" hash keys ("vendor_name" and "device_name"), and these keys are siblings to the hash keys that happen to be ID fields. But the structure is both adequate and fairly simple.

        (I had to make sure the data sample contained tab characters as described.)

Re: pci.ids to a complex data structure
by wynnmc (Initiate) on Aug 21, 2006 at 10:17 UTC
    Thank you all for your interest and advice.

    The comments that %pci_ids should have used parentheses and not braces just shows my ignorance.

    I do have #!/usr/bin/perl -w and use strict, the data structure was so long that I thought I should remove everything but (what I thought were) the essentials.

    Many thanks to McDarren for trying to help me understand what I was doing :) and graff for the script which I will have to take away and chew on.

    Thanks again.