I'm extremely new to programming in Perl, and new to the programming world in general.
I'm trying to write a program that opens a CSV file, reads it into an array, and parses through it using split().
Each field of the CSV file contains four things:
$full_name, $user_name, $password, $con_password
My problem is that I need to access the second element of each line (user_name). Then I need to put all the user names into an array so I can work with that.
I've done the split, but I don't know how to make sure I get the second element of each line.
This is what I have:
#!/usr/bin/perl
use CGI qw(:standard);
use strict;
use warnings;
print "Content-type: text/html\n\n";
my $file = '/home/2014/amosqu/public_html/cgi-bin/Members.csv';
open(my $csv, '>>', $file) or die "Could not open '$file' $!";
#i get these values from a form in an html file
my $full_name = param('full_name');
my $user_name = param('user_name');
my $password = param('password');
my $con_password = param('con_password');
#array to store user names
my @users = ();
while(my $lines = <$csv>) {
chomp $lines;
my @fields = split(/,/, $lines);
}
#access second element and put into @users ???
1 Answer
You would need to write something like this...
my @list;
open (my $csv, '<', $file) || die "cant open";
foreach (<$csv>) {
chomp;
my @fields = split(/\,/);
push @list, $fields[1];
}
In your code the file is opened for appending not reading.