perl array filling *NOOB question*


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers perl array filling *NOOB question*
# 1  
Old 04-12-2012
perl array filling *NOOB question*

First time poster here and I'm pretty much a total noob with UNIX and Perl. So please bear with me.

With Perl, I'm trying to fill an array with data that is in a CSV file. I would like to fill the array with only one of the columns in the CSV file.

I have a file called data.csv:

Code:
Name,Age,Gender,Location
Andrew,30,M,Toronto
Phuong,29,F,LA
Stan,32,M,China
Slam,33,M,Richmond
Lisa,27,F,China

What I would to have is an array that contains only one of the columns: i.e.

Code:
Gender
M
F
M
M
F

As far as code goes, as far as I can get is to print every line in the file:

Code:
#!/usr/bin/perl
use warnings;
use strict;

open FILE, "data.csv", or die $!;

while (<FILE>) {
    chomp;
    my @arr = $_;
    print "$_ \n";
}

I believe that I have to use the split() function, but i'm not sure how.
Also, I have seen lines of code that look like Tie::Hash... But I don't know what its used for.

Thanks in advance

Moderator's Comments:
Mod Comment Use code tags for code, please.

Last edited by Corona688; 04-12-2012 at 02:04 PM..
# 2  
Old 04-12-2012
Hi WongSifu,

Push, pop, shift and unshift are instructions to handle arrays. Next program should work for your problem:
Code:
use warnings;
use strict;

die qq[There is no argument\n] if @ARGV < 1;

open my $file, qq[<], $ARGV[0] or die $!;

my @arr;

while ( <$file> ) {

        ## Omit header.
        next if $. == 1;

        ## Omit blank lines.
        next if m/\A\s*\Z/;

        ## Remove last '\n'.
        chomp;

        ## Split fields with comma.
        my @f = split /,/;

        ## Add third file to array.
        push @arr, $f[2];
}

exit 0;

# 3  
Old 04-12-2012
Thanks birei!!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Noob Expect Scripting Question

I'm having some difficulty with convincing Expect to do what I need.. I have a loop that waits for input, a specific phrase of text followed by a single word. I need Expect to capture that word following the specific phrase. It should then store the word in a variable. I'm fairly sure it's... (6 Replies)
Discussion started by: LongLeafTea
6 Replies

2. Shell Programming and Scripting

For loop -- noob question

Hello, I am new to shell scripting and i am trying to figure why is this not working with else statement. I am searching for every directory in that DIR i am in, however the "else" seems to be triggered whenever the run the script.. Much thanks in advance! #!/bin/shell for item in... (3 Replies)
Discussion started by: Reb0rn
3 Replies

3. UNIX for Dummies Questions & Answers

Noob question about parsing a website

I'm trying to parse the website, finance.yahoo.com/q?s=ge&ql=1, and retrieve the info between <span id="yfs_l84_ge">18.98</span>, so 18.98. What would be the best way to go about this in a bash script? Any help or suggestions will be much appreciated. Thanks! (2 Replies)
Discussion started by: mayson
2 Replies

4. Shell Programming and Scripting

Perl question - How do I print contents of an array on a single line?

I have the following code: print @testarray; which returns: 8 8 8 9 How do I return the array like this: The output is: 8, 8, 8, 9 (5 Replies)
Discussion started by: streetfighter2
5 Replies

5. Ubuntu

Simple Noob Question

I am editing the squid.confi on my server. I am done editing. How do I exit the confi file? Thank you. (2 Replies)
Discussion started by: sethartha
2 Replies

6. Shell Programming and Scripting

perl array question from going through hash

suppose my @{$data1{$callid}}; cotains one two three three five six one two three of random patterns but each item is separated by white space or tab, Below code extract and get rid of the whitespace perfectly so that it shows now like this onetwothree threefivesix... (2 Replies)
Discussion started by: hankooknara
2 Replies

7. UNIX for Dummies Questions & Answers

Noob question on comparing #'s.

I have a file with 3 digit numbers in it formatted as such: 123 065 321 How would I go about seeing if each number is less than 100 and if so outputting it to another file Yes, I am a bit of a noob. I have tried with grep but I don't think it'll work. Any general direction would be... (6 Replies)
Discussion started by: kirkm76
6 Replies

8. UNIX for Dummies Questions & Answers

Noob sorting question

Ok here is the deal, I have a command given to me by some systems guy who I cannot get ahold of on the weekend without paying him alot of money to help me. I need to get this done before Monday as I am just getting pummeled by DOS attacks. The comand given was.... netstat -ntu | awk '{print... (1 Reply)
Discussion started by: Hexabah
1 Replies

9. Programming

Question about compiling (noob)

I'm just getting started to lean C and I'm using Ubuntu today I found a tutorial at this site: http://einstein.drexel.edu/courses/CompPhys/General/C_basics/c_tutorial.html and I got an error after compiling the fist code: #include < stdio.h> void main() { printf("\nHello World\n"); } ... (9 Replies)
Discussion started by: arya6000
9 Replies

10. Shell Programming and Scripting

Perl question - looping through an array of hashrefs

I have an array of hashrefs that look like the following: my @LAYOUT = ( {SQL_1 => "select count (*) FROM prospect WHERE PROCESS_DATE = To_date('INSERT_DATE_HERE', 'mm/dd/yyyy') and tiff_filename is not null ... (2 Replies)
Discussion started by: kregh99
2 Replies
Login or Register to Ask a Question