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

Hash table inside class::struct

Status
Not open for further replies.

phidelt1354

Technical User
Nov 28, 2007
5
US
How do you add a hash table to this structure?

struct header =>
{

customer => '$',
type => '$',
shares => '$'

};

I need to add a hash table to the header struct to hold a map of friend_type->friend_name. The friend_type string will represent the different friends in the flat.dat file that it will be reading from.

PAVANSYMBOL
LICURSISYMBOL
BASSISYMBOL


So for the example line below:

CUSTOMER|TYPE|SHARES|PAVANSYMBOL|LICUR...

PAVAN|F|100|COOL|MAN|GUY

The string "PAVANSYMBOL" will map to COOL, "LICURSISYMBOL" will map to MAN, etc.



I need help with the syntax of the structure and setting it up. The data will be read from a flat file.


Thanks!
 
I've not used Class::Struct, but it's manual page offers
Code:
struct header => {
    customer => '$',
    type => '$',
    shares => '$',
    hashtable => '%'
  };

Does this not work for you?

Yours

fish


["]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life was going to be spent in finding mistakes in my own programs.["]
--Maurice Wilkes, 1949
 
That does work thank you! I am fairly new to PERL so I was having trouble declaring my hash table in the struct, the syntax to populate it, and how to print it out.

I appreciate your answer and feel free to give me any instruction on how the syntax for populating/printing fields from my hash table within the struct.

Thanks!
 
Code:
my $h1 = new header; # construction

$h1->hashtable( colour => 'red' ); # assignments...
$h1->hashtable( size => 6 );
$h1->hashtable( type => 'rubber' );

print $h1->hashtable( 'type' ), "\n"; # print a value
seems to be the Class::Struct way of doing things.

I confess, I've not felt a need for this library or for formal structures since I started using perl. The language is rich enough to do all of this on the fly. That said, I don't know your application or requirements and there's no harm in the extra safety and error-checking that it offers.

Yours,

fish



["]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life was going to be spent in finding mistakes in my own programs.["]
--Maurice Wilkes, 1949
 
FISH! I need your help again please! How would you initialize a hash later in the code (ie not at declaration). I am getting an error that says "Use of uninitialized value in length at....."

I am calling the length function on a field in my hash that has no value so I want to initialize this. Do you know the solution to this or can anyone help?

Thanks!
 
why are you checking the length if you know it is empty? Maybe you need to use "exists" or something first?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
I'm declaring my hash table that exists from within a struct. Then later in the code, the program goes through and loads a flat file to read from and it reads each line and populates each line accordingly. I am checking to see if the value exists. The problem is when I check to see if it exists by checking the length and the value DOESN'T exist, PERL gives a warning when it's running saying the value was never initialized.

SO... I want to intialize the table (not at declaration) so that it at least will have the NULL character when it comes to the 'length' check.

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top