| |||||||||||
working with csv columns in Perl Posted by Heidi (Heidi), 28 May 2008 Hey, Pretty awesome site you've got here (spent most of my morning on it!). I am new to Perl (or any sort of data lengua for that matter), currently in chapter 10 of the lama. I am trying to get my perl program to focus only on one column (C) in my data set and from searching your site I thought that maybe something simple as referring to it as "colC" would cut it... but it is not working. As I said, I am new to this and if this is a completely rudementary question - I do appologize! I have attached my program below, and I hope you will take the time to look it over. Thanks, Heidi #!/usr/bin/perl/ use warnings; #--------------------------------------- # Need to: # put program and files to be include into the same folder, # on the command page print the name of this program; #--------------------------------------- # Objective: # to make the program pick out only one column in the csv data file, # disregard the header line, and count the number of times a number shows up. print "Make sure all your files are in the same folder as this program. print the name of your first file to be included.\n"; chomp (my $file1 = <STDIN>); open VRFILE, "$file1"; Code: $file1=','; my @data= $colC; # get the column of data from column c (what about the header?) my %count; # creating a new hash to use for count of each id number $count{$_}++ foreach @data; #creates the new keys and values print %count; close VRFILE; Posted by KevinAD (KevinAD), 29 May 2008 You appear to be missing some very fundamental concepts, did you read all the chapters up to 10 or are you skipping chapters? If it is a very simple CSV file you would use the split() function to parse each line into its columns. Posted by Heidi (Heidi), 29 May 2008 Thanks Kevin... I did indeed read all the chapters, didn't skip a single paragraph. However, since this book is the first introduction I have had to any sort of computer stuff there is bound to be stuff that I haven't gotten a hold on yet - despite intensive use of websources to find out definitions of multiple "x-names".As I said, I was aware that my question might be too simple. Heidi Posted by KevinAD (KevinAD), 29 May 2008 Trying to manually parse a CSV file can be a bad idea, but if it is a simple CSV file the general idea is:open (FH, 'yourcsvfile') or die "$!"; while(<FH>){ my $colc = (split(/,/))[2]; print $colc,"\n"; } close FH: assumes column C is the third column in a line, something like: 12,34,56,78,90 where 56 is column C. Posted by Heidi (Heidi), 30 May 2008 Thanks Kevin,I will give that a whirl! I would consider the csv file simple in that it is automatically generated with "natural columns" but the file is rather large so I am wondering why you are saying that it can be a bad idea. Also, do you know of any tutorial or book that emphasises data management/extraction that you could refer me to? Thanks again! Posted by KevinAD (KevinAD), 30 May 2008 on 05/30/08 at 17:20:47, Heidi wrote:
Parsing a CSV file is really a bit tricky, doing it manually can lead to problems. I recommend you look into Text::CSV_XS to pase CSV files: http://search.cpan.org/~hmbrand/Text-CSV_XS-0.45/CSV_XS.pm Data management ( a very wide open topic ) is not one of my strong points, but there is a good book, if a little old now, written by Dave Cross (long time perl developer and contributor): Data Munging http://www.manning.com/cross/ Posted by Heidi (Heidi), 6 June 2008 Hi Kevin,I have a csv file(1) with time (Gmt) m/d/yr hr:min in column A. A csv file(2) with time (Gmt) hr:min in column A (start) and column B (end) (these are all the same date which is provided else where) I need to get the time interval between file(2) column A and B, use that to extract the data from within file(1) column C with corresponding times (file(1)columnA). Any suggestions? How do I use the "$time = timegm" function? Thanks Posted by admin (Graham Ellis), 7 June 2008 There's a example of timelocal athttp://www.wellho.net/resources/ex.php4?item=p216/tim - just replace local with gm in the call. Note also down the right hand side of that page there are links to a few other date and time functions. Seriously - so many people have written time and date handlers for Perl that there was a joke that there was a module for every day of the month! This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |