http://www.perlmonks.org?node_id=997328

jrp370 has asked for the wisdom of the Perl Monks concerning the following question:

Im working on a script that reads in a csv file and then prints specific information from it to a different file called password.passwrd. so far ive gotten by script to read the csv file however i need to create a user name by taking the first letter from the first name, the first 3 letters from the last name and finally the last 4 digits from the ssn number. Ive tried using splice but that doesnt seem to work, can anyone help point me in the right direction?

update: it seems like i have figured it out, i dont know why i didnt think of using split before.
#!/usr/bin/perl use strict; use warnings; open FILE,'>>',"password.passwd" or die 'Could not open file'; my $file = 'file.csv'; my @data; my $count=500; open(my $fh, '<', $file) or die "Can't read file '$file' [$!]\n"; while (my $line = <$fh>) { chomp $line; $count++; my @fields = split(/,/, $line); push @data, \@fields; #print "@fields\n"; my($Lname,$Fname,$ssn,$address,$city,$state,$zip,$phone1,$phone2)=($fi +elds[0],$fields[1],$fields[2],$fields[3],$fields[4],$fields[5],$field +s[6],$fields[7],$fields[8]); my $user=""; my $password="4cb9c8a8048fd02294477fcb1a4119la"; my $userId="$count"; my $groupId='25'; my $info="$Fname $Lname"; my $home='/home/payroll'; my $shell='/bin/payroll'; ##################################################### ##################create usr name######################### ########################################################## print FILE "$user:$password:$userId:$groupId:$info:$home:$shell\n"; } close $fh; close File;

Replies are listed 'Best First'.
Re: creating a username from a csv file
by Tanktalus (Canon) on Oct 04, 2012 at 22:34 UTC

    Oh, where to start.

    First off, you have both open FILE and open my $fh. Stick to one style. Specifically the latter. Yes, it's a minor nit normally, but it got promoted to a major nit (if not, at least a lieutenant nit) by virtue of inconsistency. Also, you don't check the first open's return - stick the "or die" in there as well. (Again, a lieutenant nit for the same reason.)

    Second, parsing CSV files without Text::CSV_XS. Not a capital crime, but maybe a minor bit of flogging. (Personally, I prefer DBD::CSV, but it uses Text::CSV_XS under the covers, so I save my back from the flogging that way.)

    Next, your assignment to all those variables. When I'm doing a massive split like this, I like to use a hash instead of an array. Or individually-named variables:

    my @fieldnames = qw(Lname Fname ssn address city state zip phone1 phon +e2); my %row; @row{@fieldnames} = split(/,/, $line); # yeah, I just told you to use +Text::CSV, but I'm keeping my suggestions independent.
    Again, this is just a nit, not a problem.

    You didn't quite show us what you were trying, only alluded to it and gave us context. But the function you're looking for is substr.

    my $user = substr($Fname, 0, 1) . substr($Lname, 0, 3) . substr($ssn, +-4);
    Though I have to wonder about anyone using ssn's for anything other than taxes. Seems like a privacy invasion.

Re: creating a username from a csv file
by Kenosis (Priest) on Oct 04, 2012 at 23:10 UTC

    ... i need to create a user name by taking the first letter from the first name, the first 3 letters from the last name...

    You may want to check the user names created that way. Someone may have the name Frank Arthur, Paul Issac, Sarah Luther, et al ...