A little help on perl coding..
A little help on perl coding..
am 21.11.2007 21:14:39 von 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 jacarter@gmail.com 00206251
00207609
mstella mary mstella@yahoo.com 00207609 00220458
msmith martin msmith@gmail.com 00202227 00207609
bborders bob bborders@gmail.com 00220458 00202003
swatson sush swatson@yahoo.com 00224981 00207609
rcasey rick rcasey@gmail.com
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 jacarter@gmail.com mstella
mstella mary mstella@yahoo.com bborders
msmith martin msmith@gmail.com mstella
bborders bob bborders@gmail.com rcasey
swatson sush swatson@yahoo.com mstella
rcasey rick rcasey@gmail.com 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
Re: A little help on perl coding..
am 21.11.2007 22:54:53 von Jim Gibson
In article
<04ee98b1-2fb6-4ceb-a3e8-3f6168931eb3@w34g2000hsg.googlegroups.com>,
wrote:
> 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 jacarter@gmail.com 00206251
> 00207609
> mstella mary mstella@yahoo.com 00207609 00220458
> msmith martin msmith@gmail.com 00202227 00207609
> bborders bob bborders@gmail.com 00220458 00202003
> swatson sush swatson@yahoo.com 00224981 00207609
> rcasey rick rcasey@gmail.com
> 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 jacarter@gmail.com mstella
> mstella mary mstella@yahoo.com bborders
> msmith martin msmith@gmail.com mstella
> bborders bob bborders@gmail.com rcasey
> swatson sush swatson@yahoo.com mstella
> rcasey rick rcasey@gmail.com 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
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Re: A little help on perl coding..
am 22.11.2007 19:38:18 von clearguy02
On Nov 21, 1:54 pm, Jim Gibson wrote:
> In article
> <04ee98b1-2fb6-4ceb-a3e8-3f6168931...@w34g2000hsg.googlegroups.com>,
>
>
>
> wrote:
> > 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 jacar...@gmail.com 00206251
> > 00207609
> > mstella mary mste...@yahoo.com 00207609 00220458
> > msmith martin msm...@gmail.com 00202227 00207609
> > bborders bob bbord...@gmail.com 00220458 00202003
> > swatson sush swat...@yahoo.com 00224981 00207609
> > rcasey rick rca...@gmail.com
> > 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 jacar...@gmail.com mstella
> > mstella mary mste...@yahoo.com bborders
> > msmith martin msm...@gmail.com mstella
> > bborders bob bbord...@gmail.com rcasey
> > swatson sush swat...@yahoo.com mstella
> > rcasey rick rca...@gmail.com 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
>
> Posted Via Usenet.com Premium Usenet Newsgroup Services
> ----------------------------------------------------------
> ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
> ----------------------------------------------------------
> http://www.usenet.com
Hi Jim,
The following piece is not working.
%users;
while ()
{
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";
}
Re: A little help on perl coding..
am 23.11.2007 09:28:55 von Dave Weaver
On Thu, 22 Nov 2007 10:38:18 -0800 (PST),
clearguy02@yahoo.com wrote:
> 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.
> %users;
What do you think that line does?
ITYM:
my %users
>
> while ()
> {
> 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.