Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

[SOLVED] Output to another file help

by jaffinito34 (Acolyte)
on Nov 08, 2012 at 21:14 UTC ( #1002984=perlquestion: print w/ replies, xml ) Need Help??
jaffinito34 has asked for the wisdom of the Perl Monks concerning the following question:

I'm working on a script where I'm given a CSV file with info (first and last names, ssn, address, etc) and I first need to separate everything, which the code does, but then I need to make an output file with the first letter of the first name, last 3 letters of last name, and more... My problem is the output file is blank after I run the script to see if anything is getting written in. See code below

#! /usr/bin/perl # CSC 310 Project # 1 print "Enter filename: "; chomp($fileName = <STDIN>); open (FILE, '<', $fileName) || die ("Could not open $fileName\n"); while ($line = <FILE>){ print "\n"; ($lastName,$firstName,$ssn,$address,$city,$state,$zip,$phone) = sp +lit ',', $line; print "$lastName, $firstName\n"."$ssn\n"."$address\n", "$city $sta +te $zip\n"; open (PSWD, '>>', 'passwd.txt'); print PSWD "$firstName[1]\n"; }

Any help or reference to help is appreciated!

Comment on [SOLVED] Output to another file help
Download Code
Re: Output to another file help
by roboticus (Canon) on Nov 08, 2012 at 21:21 UTC

    jaffinito34:

    You're treating $firstName as an array reference, but it's presumably a string. What you want to do is create a variable that holds the first initial, then you can print that. Something like this:

    my $firstName = 'Marco'; my $firstInitial = substr($firstName, 0, 1); print "First initial is '$firstInitial'\n";

    Read the documentation for substr to see how it works.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      PERFECT!, any ideas how to quickly convert the firstInitial to lowercase? Right now my script outputs the first initial, first 3 of the last name, and last 4 of ssn, but it does so that it looks like this:

      First, Last, 000-000-0000, etc...

      output: FLas0000

      I'm trying to get it like: flas0000

      Nevermind, found out about lc() function.

Re: Output to another file help
by Kenosis (Priest) on Nov 08, 2012 at 22:13 UTC

    ...I need to make an output file with the first letter of the first name, last 3 letters of last name...

    use strict; use warnings; my $firstName = 'PATRICK'; my $lastName = 'STERL'; my $fileNamePart = "$firstName$lastName" =~ s/^(.).+(...)$/\L$1$2/r; warn qq{Be careful: "$fileNamePart" may be an obscene word.};
Re: Output to another file help
by space_monk (Chaplain) on Nov 09, 2012 at 01:29 UTC
    Please in the name of whatever deity/deities you worship, stop using "split" to break up lines of CSV files and use Text::CSV or a similar library instead.

    In the particular example, the original program is reading in an address, which is almost certain to contain a comma somewhere, and the normal way round that is to quote the field. split won't handle this, but CSV parsing libraries such as Text::CSV will take it in their stride.

    A Monk aims to give answers to those who have none, and to learn from those who know more.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1002984]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2014-09-03 04:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (35 votes), past polls