For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > February 2005 > file parsing..









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author file parsing..
clearguy02@yahoo.com

2005-02-23, 8:57 pm

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.

jl_post@hotmail.com

2005-02-23, 8:57 pm

cleargu...@yahoo.com wrote:
> 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

jl_post@hotmail.com

2005-02-23, 8:57 pm

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.

Keith Keller

2005-02-23, 8:57 pm

On 2005-02-23, clearguy02@yahoo.com <clearguy02@yahoo.com> wrote:
>
> 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.
[color=darkred]
> ========================================
===
> while (<DATA> )
> {
> ($last, $first) = split ("\s+", $_);
> $login = $first[0] . $last;
> print "$login\n";
> }
>
> __DATA__
> Smith Bob
> Carter John
> Fisher Ann
> Lewis Maggy
> =======================================[
/color]

--keith

--
kkeller-usenet@wombat.san-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://wombat.san-francisco.ca.us/cgi-bin/fom
see X- headers for PGP signature information

John W. Krahn

2005-02-23, 8:57 pm

clearguy02@yahoo.com wrote:
>
> 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
--
use Perl;
program
fulfillment
Josef Moellers

2005-02-24, 8:57 am

jl_post@hotmail.com wrote:
> cleargu...@yahoo.com wrote:
>=20
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
3D=3D=3D=3D=3D=3D[color=darkred]
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
3D=3D[color=darkred]
>=20
>=20
>=20
> Dear Rider,
>=20
> First, know that just using split() with no parameters is almost the=


> same as:
>=20
> split(/\s+/, $_)
>=20
> 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:
>=20
> substr($first, 0, 1)
>=20
> Therefore, I think this code will do exactly what you want:
>=20
> while (<DATA> )
> {
> ($last, $first) =3D split;
> $login =3D 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=20
used to say) TRY YOUR SOLUTIONS BEFORE POSTING (this is your second=20
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) =3D split;
$login =3D lc substr($first, 0, 1) . $last;
print "$login\n";
}
__DATA__
Smith Bob
Carter John
Fisher Ann
Lewis Maggy

Josef
--=20
Josef M=F6llers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

Peter Wyzl

2005-02-24, 8:57 am

<clearguy02@yahoo.com> wrote in message
news:1109188215.923039.18540@g14g2000cwa.googlegroups.com...
> 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

--

Nothing to see here...


Peter Wyzl

2005-02-24, 8:57 am

"Peter Wyzl" <wyzelli@yahoo.com> wrote in message
news:8jgTd.173522$K7.50527@news-server.bigpond.net.au...
> <clearguy02@yahoo.com> wrote in message
> news:1109188215.923039.18540@g14g2000cwa.googlegroups.com...


<snip>

> while (<DATA> ){
> my $login = lc("$2$1") if m/(\S+)\s+(\w)/;
> print "$login\n";
> }
> <STDIN>;


The <STDIN> is just for my own testing (in Windows XP....)

P
--
Still nothing to see here...


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com