Can someone demystify tie, please? 
Author Message
 Can someone demystify tie, please?


Quote:

>SN!hostname!type!domain!user!manager!asset
>tag!hostid!mem!disk!OS!vendor!monitor!maint start date! maint end date.
>So if I open this file (inventory) to a hash table using this line,
>tie(%INV, NDBM_File, '/admin/inventory', 1 0)
>what are the keys and values in %INV?.  One of my scripts may want to use

One is tempted to write "Any string you like, up to a certain length".
That's the size of it: key and value are each strings.

Quote:
>%INV to match a hostname to a user and some other script may want to match
>a hostname to a domainname for ex. I guess the reason for my confusion,
>is that in all the examples that I have seen so far, the files had only
>two entries make it convenient for a hash table.

Yes, that's the picture.

Quote:
>What am I missing here?

Your problem is a lot more complicated, sorry; and "tie" doesn't help
very much.  Multiple indexes on the same data are possible, but you're
beginning to stray into Database technology here.  In general, if you
want to look something up, you have to index it, or search.

If the file's too big for memory, and you can't wait for a brute-force
search, you'll need several external indexes, or patience. If you have
several indexes, you have to work hard keeping them in sync.

If you can read it all in:

while(<FILE>) {


    foreach $fno (0..$#fields)
        { $field[$fno]{$fields[$fno]} = $line };

Quote:
}

Now $field[1]{'somehost'}[3] is the "domain" field of the record whose
hostname is 'somehost'. You can't tie this very well, since DB-style
programs can only key on strings, not references.

To understand this better, remember that the above is syntactic sugar
for this:
        $field[1]->{'somehost'}->[2];

There's probably a nicer solution using hashes rather than arrays, so
that you can say $field{'host'}{$somehost}{'type'}. This is left as an
exercise for the reader, as is working out what to do with duplicated
values in fields.

If memory's a problem, then don't index all the fields, hold the
array as a string and split it at access time.

I'd be inclined to do a brute-force search:

open F,'/my/file/name' or die $!;
while(<F>) {

    print "$fields[3]\n" if $fields[1] =~ /$ARGV[0]/o;

Quote:
}

Ian


Tue, 14 Apr 1998 03:00:00 GMT  
 Can someone demystify tie, please?
I would appreciate if someone could explain to me the use of tie and
untie. I have read the perldoc and the ref, but I am not sure how I
can use it the function. The doc says that I use tie to bind a package to
a varaible. What I want to do is use a existing ('!' delimited) inventory
file in one of my programs. The file has about 20 fields with a typical entry
like this:

SN!hostname!type!domain!user!manager!asset
tag!hostid!mem!disk!OS!vendor!monitor!maint start date! maint end date.

So if I open this file (inventory) to a hash table using this line,

tie(%INV, NDBM_File, '/admin/inventory', 1 0)

what are the keys and values in %INV?.  One of my scripts may want to use
%INV to match a hostname to a user and some other script may want to match
a hostname to a domainname for ex. I guess the reason for my confusion,
is that in all the examples that I have seen so far, the files had only
two entries make it convenient for a hash table.
What am I missing here?

Thanks for your time.

Vijay



Tue, 14 Apr 1998 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Please, can someone help me???????????

2. can someone explain these 8 lines of code please

3. closure sub routine (can someone please explain)

4. Can Someone Please Explain...

5. Someone Please Help Me - I Help You in Any Software Queries/Programming/Coding

6. Can Someone Explain This, Please?

7. Could someone please tell me......

8. Someone please post Oraperl calling Oracle Store procedure example(s)

9. pack result - can someone please explain ?

10. Can someone please help me?!?!

11. I need a script writen, can someone help me please

 

 
Powered by phpBB® Forum Software