file parsing..

C

clearguy02

Hi experts,

I have below script and I need to translate a full name to a login ID.

===========================================
while (<DATA>)
{
($last, $first) = split ("\s+", $_);
$login = $first[0] . $last;
print "$login\n";
}

__DATA__
Smith Bob
Carter John
Fisher Ann
Lewis Maggy
=======================================

Output should be:
===============
bsmith
jcarter
afisher
mlewis
=============

How can I modify the aboe script to get the desired output?

Thanks,
Rider.
 
J

jl_post

I have below script and I need to translate a
full name to a login ID.

===========================================
while (<DATA>)
{
($last, $first) = split ("\s+", $_);
$login = $first[0] . $last;
print "$login\n";
}

__DATA__
Smith Bob
Carter John
Fisher Ann
Lewis Maggy
=======================================

Output should be:
===============
bsmith
jcarter
afisher
mlewis
=============

How can I modify the aboe script to get the desired output?


Dear Rider,

First, know that just using split() with no parameters is almost the
same as:

split(/\s+/, $_)

Second, saying $first[0] does NOT give you the first letter of the
$first variable. Instead it gives you the first ELEMENT of the @first
ARRAY. To get the first letter of the $first variable, use substr()
instead, like this:

substr($first, 0, 1)

Therefore, I think this code will do exactly what you want:

while (<DATA>)
{
($last, $first) = split;
$login = substr($first, 0, 1) . $last;
print "$login\n";
}
__DATA__
Smith Bob
Carter John
Fisher Ann
Lewis Maggy
=======================================


I hope this helps, Rider.

-- Jean-Luc
 
J

jl_post

I forgot to mention:

You probably want to add the line:

$login = lc($login);

before the print() statement so that all your usernames come out in
lower-case letters.
 
K

Keith Keller

I have below script and I need to translate a full name to a login ID.

Try your script with

use strict;
use warnings;

At least two of the reported errors are relevant to why you're not
getting the output you desire.
===========================================
while (<DATA>)
{
($last, $first) = split ("\s+", $_);
$login = $first[0] . $last;
print "$login\n";
}

__DATA__
Smith Bob
Carter John
Fisher Ann
Lewis Maggy
=======================================

--keith
 
J

John W. Krahn

I have below script and I need to translate a full name to a login ID.

===========================================
while (<DATA>)
{
($last, $first) = split ("\s+", $_);
$login = $first[0] . $last;
print "$login\n";
}

__DATA__
Smith Bob
Carter John
Fisher Ann
Lewis Maggy
=======================================

Output should be:
===============
bsmith
jcarter
afisher
mlewis
=============

How can I modify the aboe script to get the desired output?

while ( <DATA> ) {
my ( $last, $login ) = map lc, split;
substr( $login, 1 ) = $last;
print "$login\n";
}

__DATA__
Smith Bob
Carter John
Fisher Ann
Lewis Maggy



John
 
J

Josef Moellers

I have below script and I need to translate a
full name to a login ID.

===========================================
while (<DATA>)
{
($last, $first) = split ("\s+", $_);
$login = $first[0] . $last;
print "$login\n";
}

__DATA__
Smith Bob
Carter John
Fisher Ann
Lewis Maggy
=======================================

Output should be:
===============
bsmith
jcarter
afisher
mlewis
=============

How can I modify the aboe script to get the desired output?



Dear Rider,

First, know that just using split() with no parameters is almost the
same as:

split(/\s+/, $_)

Second, saying $first[0] does NOT give you the first letter of the
$first variable. Instead it gives you the first ELEMENT of the @first
ARRAY. To get the first letter of the $first variable, use substr()
instead, like this:

substr($first, 0, 1)

Therefore, I think this code will do exactly what you want:

while (<DATA>)
{
($last, $first) = split;
$login = substr($first, 0, 1) . $last;
print "$login\n";
}
__DATA__
Smith Bob
Carter John
Fisher Ann
Lewis Maggy

Dear Jean-Luc,

Please (pretty please, with sugar on top, as my cousing from down under
used to say) TRY YOUR SOLUTIONS BEFORE POSTING (this is your second
reply in sequence which falls short of this simple requirement)!
Your solution DOES NOT "do exactly what you want", as a simple test will
reveal, because the output will be

BSmith
JCarter
AFisher
MLewis

Note the difference? (Hint: look at the case of the letters)

Rider will also have to lc the strings or the entire result:

while (<DATA>)
{
($last, $first) = split;
$login = lc substr($first, 0, 1) . $last;
print "$login\n";
}
__DATA__
Smith Bob
Carter John
Fisher Ann
Lewis Maggy

Josef
 
P

Peter Wyzl

Hi experts,

I have below script and I need to translate a full name to a login ID.

===========================================
while (<DATA>)
{
($last, $first) = split ("\s+", $_);
$login = $first[0] . $last;
print "$login\n";
}

__DATA__
Smith Bob
Carter John
Fisher Ann
Lewis Maggy
=======================================

Output should be:
===============
bsmith
jcarter
afisher
mlewis
=============

How can I modify the aboe script to get the desired output?

My offering to the gods...

use strict;
use warnings;

while (<DATA>){
my $login = lc("$2$1") if m/(\S+)\s+(\w)/;
print "$login\n";
}
<STDIN>;
__END__
Smith Bob
Carter John
Fisher Ann
Lewis Maggy



P
 

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

Forum statistics

Threads
474,169
Messages
2,570,918
Members
47,458
Latest member
Chris#

Latest Threads

Top