New to perl - Dynamically entering a value for an array (a column) from a csv using perl -


i new perl , need write perl script following requirements:

  1. need read csv file
  2. store columns in array
  3. suppose there 7 fields (columns) in csv: field 1, field 2, field 3,field 4,field 5,field 6,field 7

i need able give fields input parameter dynamically. suppose if give input parameters field 3, field 7 , data in csv is:

no of orders'|date'|year'|exp_date'|month'|time'|committed 12'|12122002'|2013'|02022012'|12'|1230'|yes 

then want output be:

year~committed                           2013~yes  

and remaining columns in same format:

no of orders~date~exp_date~month~time 12~12122002~02022012~12~1230 

currently got perl script net provides me left sided result in hardcoded format. want give input in run time , want result generated.help appreciated.

$filename = 'xyz.csv';  # use perl open function open file open(file, $filename) or die "could not read $filename, program halting.";  # loop through each line in file # typical "perl file while" loop while(<file>) {    chomp;    # read fields in current line array   @fields = split('\`\|', $_);   # print concat  print "$fields[0]~$fields[1]\n"; }   close file; 

i not going argue whether use text::csv or not. not relevant question , split acceptable.

here how solve problem, assuming poster wants have input files of more 1 line of data.

#!/usr/bin/perl -w  # take file name command line, instead of hard coding ($filename) = @argv; @title; @table;  open file, "<", $filename or die "could not read $filename, program halting."; while (<file>) {   chomp;   if (!@title) {     @title = split '\'\|';     next;   }   @row = split '\'\|';   # build array of array references, 2 dimensional table   push @table, \@row; } close file;  print "access row? "; $row = <stdin> - 1; die "row value out of range $filename\n" if (($row < 0) || ($row >= scalar(@table)));  print "access column? "; $col = <stdin> - 1; die "column value out of range $filename\n" if (($col < 0) || ($col >= scalar(@title)));  print "$title[$col]\~$table[$row][$col]\n"; 

the first pass of while loop store split values in our title array. remaining passes append our data, row row, array references table. user prompting , basic error checking, print our results.


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -