A little help on perl coding..

C

clearguy02

Hi folks,


I am playing around on the below issue. I have a file, C:\test.txt
with the following content:

--------------------------------------------------------------------------------------------------------------
user id name email employee
uid manager uid

jcarter john (e-mail address removed) 00206251
00207609
mstella mary (e-mail address removed) 00207609 00220458
msmith martin (e-mail address removed) 00202227 00207609
bborders bob (e-mail address removed) 00220458 00202003
swatson sush (e-mail address removed) 00224981 00207609
rcasey rick (e-mail address removed)
00202003 00201009
----------------------------------------------------------------------------------------------------------------


mstella is the boss of jcarter, msmith and swatson;
bborders is the boss of mstella;
rcasey is the boss of bborders;

Now I need to replace the manager uid's with the boss id's; for the
top-most manager's id, you can replace his manager uid with his user
id itself (in this case rcasey is the top-most guy).

i.e the output file should be as follows:
---------------------------------------------------------------------------------
user id name email manager id

jcarter john (e-mail address removed) mstella
mstella mary (e-mail address removed) bborders
msmith martin (e-mail address removed) mstella
bborders bob (e-mail address removed) rcasey
swatson sush (e-mail address removed) mstella
rcasey rick (e-mail address removed) rcasey
--------------------------------------------------------------------------------

I am struggling to figure it out with hashes and arrays, but not
getting a desired result.. can some one help me out here?

Thanks,
JC
 
C

clearguy02

Hi folks,
I am playing around on the below issue. I have a file, C:\test.txt
with the following content:
jcarter john (e-mail address removed) 00206251
00207609
mstella mary (e-mail address removed) 00207609 00220458
msmith martin (e-mail address removed) 00202227 00207609
bborders bob (e-mail address removed) 00220458 00202003
swatson sush (e-mail address removed) 00224981 00207609
rcasey rick (e-mail address removed)
00202003 00201009
mstella is the boss of jcarter, msmith and swatson;
bborders is the boss of mstella;
rcasey is the boss of bborders;
Now I need to replace the manager uid's with the boss id's; for the
top-most manager's id, you can replace his manager uid with his user
id itself (in this case rcasey is the top-most guy).
i.e the output file should be as follows:
jcarter john (e-mail address removed) mstella
mstella mary (e-mail address removed) bborders
msmith martin (e-mail address removed) mstella
bborders bob (e-mail address removed) rcasey
swatson sush (e-mail address removed) mstella
rcasey rick (e-mail address removed) rcasey
I am struggling to figure it out with hashes and arrays, but not
getting a desired result.. can some one help me out here?

I would use a hash-of-hashes. The key for the primary hash would be the
employee uid, which should always be unique. Each employee will have a
primary hash entry, with the uid as key and a reference to a secondary
hash as value. Each secondary hash will have entries with keys
'user_id', 'manager_uid', etc. Note that you can include the
'employee_uid' as a secondary hash entry or not, because the value is
already the primary hash key.

Your problem them becomes iterating through the primary hash, fetching
the manager uid value from the secondary hash, looking up the 'user id'
field from the manager's hash found by using the manager uid as key,
and adding the value of the manager's user id field to each employee's
secondary hash. Note that I would use a new field for manager id and
not overwrite the existing mananger uid field.

Something like the following, assuming that all of the employee data
has been stored in the primary hash %employees (untested):

for my $euid ( keys %employees ) {
my $manager_uid = $employees{$euid}->{manager_uid};
my $manager_id = $employees{$manager_uid}->{user_id};
$employees{$euid}->{manager_id} = $manager_id;
}

Then you can print out the fields for each employee as you wish.

Good luck!

--
Jim Gibson


----------------------------------------------------------

----------------------------------------------------------
color]

Hi Jim,

The following piece is not working.


%users;

while (<DATA>)
{
chop;
($nt_id, $name, $uid, $mid) = split(/\s+/);

$users{$nt_id} = [$name, $uid, $mid];
}

close(DATA);


for $uid (keys %users)
{
$mid = $users{$uid}->{mid};
$manager_id = $users{$mid}->{$nt_id};
$users{$uid}->{mid} = $manager_id;

print "$nt_id\t\t$manager_id\n";
}
 
D

Dave Weaver

On Thu, 22 Nov 2007 10:38:18 -0800 (PST),
The following piece is not working.

What does "not working" mean? It is the most useless problem description
ever. What does it not do that you would like it to do? What does it do
that you don't want it to do? Where is the sample input data? What do
you expect the output to be?

Please read the posting guidelines for this group. They are posted
here regularly.


What do you think that line does?
ITYM:
my %users
while (<DATA>)
{
chop;

Don't use chop(), use chomp() which will correctly remove the input
record separator no matter how many characters it is.
($nt_id, $name, $uid, $mid) = split(/\s+/);

$users{$nt_id} = [$name, $uid, $mid];
}

close(DATA);


for $uid (keys %users)

for my $uid ( keys %users )
{
$mid = $users{$uid}->{mid};

That won't work, because $users{$uid} is a reference to an array, not
a hash.
my $uid = $users{$uid}[2];
$manager_id = $users{$mid}->{$nt_id};

I've no idea what you're trying to do here, but you're not storing the
nt_id anywhere in the values of the users hash...
$users{$uid}->{mid} = $manager_id;

print "$nt_id\t\t$manager_id\n";
}

Post again, giving a *short* but *complete* program (including sample data in a
__DATA__ section) that we can all run, that exhibits the problem you have, and
explain what the problem is.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top