Can you pl. take a look here?

C

clearguy02

Hi all,

Here is the problem description.

__DATA__
# user_id name email user_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
+++++++++++++++++++++++++++++++++++++++++++++++++++++

Here is my code:

while (<DATA>)
{
$line = $_;
@lineArray = split (/\s+/, $line);

if ($lineArray[3] == $lineArray[2])
{
s/$lineArray[3]/$lineArray[0]/g;
}
print "$lineArray[0]\t\t$lineArray[1]\t\t$lineArray[3]\n";
}

I am struggling with the part of replacing the the numerical
manager_uid with the manager_id.

Thanks in advance,
JC
 
A

A. Sinan Unur

(e-mail address removed) wrote in
Subject: Can you pl. take a look here?

Please do read the posting guidelines for this group and spend some time
composing a meaningful subject line.

I only looked at this post by accident (I had figured it for a Viagra ad
based on the subject line).
Here is the problem description.

Are you assigning homework now?
__DATA__
# user_id name email user_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).

So, if manager_uid is not in the user_uid's supplied, then assume user
answers to higher authority than the mortals in this set.

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
+++++++++++++++++++++++++++++++++++++++++++++++++++++

But now you are contradicting yourself. rcasey has a manager. The
manager's id is 00201009. rcasey answers to someone who is not him.
while (<DATA>)
{
$line = $_;
@lineArray = split (/\s+/, $line);

Don't forget to chomp.

No need for silly intermediate assignments. Are you being paid by the
line?

while ( <DATA> ) {
chomp;
my @lineArray = split /\s+/;
if ($lineArray[3] == $lineArray[2])

You are using numeric comparison when the input data are really strings
(i.e. things like ZIP codes, badge numbers etc -- even when they consist
entirely of digits -- are not numbers).
{
s/$lineArray[3]/$lineArray[0]/g;
}
print "$lineArray[0]\t\t$lineArray[1]\t\t$lineArray[3]\n";
}

I am struggling with the part of replacing the the numerical
manager_uid with the manager_id.

To my simple mind, it looks like your problem requires two passes over
data.

I assumed, based on your example, that the output had to be in the same
order as the input.

#!/usr/bin/perl

use strict;
use warnings;

my ( %users, @users );

while ( my $record = <DATA> ) {
next if $record =~/^#/;
chomp $record;
next if $record =~ /^\s*$/;

my @fields = split /\s+/, $record;
$users{ $fields[3] } = $fields[0];
push @users, \@fields;
}

for my $user ( @users ) {
my ($user_uid, $manager_uid) = @$user[3,4];

my $manager = exists $users{ $manager_uid }
? $users{ $manager_uid }
: $users{ $user_uid } ;
print join("\t", @$user[0 .. 2], $manager), "\n";
}

__DATA__
# user_id name email user_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
 
C

clearguy02

(e-mail address removed) wrote in
Subject: Can you pl. take a look here?

Please do read the posting guidelines for this group and spend some time
composing a meaningful subject line.

I only looked at this post by accident (I had figured it for a Viagra ad
based on the subject line).
Here is the problem description.

Are you assigning homework now?




__DATA__
# user_id name email user_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).

So, if manager_uid is not in the user_uid's supplied, then assume user
answers to higher authority than the mortals in this set.
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
+++++++++++++++++++++++++++++++++++++++++++++++++++++

But now you are contradicting yourself. rcasey has a manager. The
manager's id is 00201009. rcasey answers to someone who is not him.
while (<DATA>)
{
$line = $_;
@lineArray = split (/\s+/, $line);

Don't forget to chomp.

No need for silly intermediate assignments. Are you being paid by the
line?

while ( <DATA> ) {
chomp;
my @lineArray = split /\s+/;
if ($lineArray[3] == $lineArray[2])

You are using numeric comparison when the input data are really strings
(i.e. things like ZIP codes, badge numbers etc -- even when they consist
entirely of digits -- are not numbers).
{
s/$lineArray[3]/$lineArray[0]/g;
}
print "$lineArray[0]\t\t$lineArray[1]\t\t$lineArray[3]\n";
}
I am struggling with the part of replacing the the numerical
manager_uid with the manager_id.

To my simple mind, it looks like your problem requires two passes over
data.

I assumed, based on your example, that the output had to be in the same
order as the input.

#!/usr/bin/perl

use strict;
use warnings;

my ( %users, @users );

while ( my $record = <DATA> ) {
next if $record =~/^#/;
chomp $record;
next if $record =~ /^\s*$/;

my @fields = split /\s+/, $record;
$users{ $fields[3] } = $fields[0];
push @users, \@fields;

}

for my $user ( @users ) {
my ($user_uid, $manager_uid) = @$user[3,4];

my $manager = exists $users{ $manager_uid }
? $users{ $manager_uid }
: $users{ $user_uid } ;
print join("\t", @$user[0 .. 2], $manager), "\n";

}

__DATA__
# user_id name email user_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

--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)
clpmisc guidelines: <URL:http://www.augustmail.com/~tadmc/clpmisc.shtml>- Hide quoted text -

- Show quoted text -

Thanks a bunch.. Sinan.

JC
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top