David,Thanks much. I'll give it a shot and comeup with the code. This is a new learning to me today :)
regards -Ajey On Sat, 23 Oct 2004, David le Blanc wrote: > On Fri, 22 Oct 2004 20:13:05 -0700 (PDT), Ajey Kulkarni <[EMAIL PROTECTED]> wrote: > > howdy!! > > I've a code snipped that reads the file into array. > > The file is a comma separated file. I want to say > > get 4th column of the whole file in an array. > > > > @lines = LIB::readFile($opt{f}); > > > > My @lines has the whole comma separated file. > > > > I would like to now get the 4th column of whole file (using @lines) > > in an array. I can do this by parsing the @lines again ,split() and > > get each array element,but this may be slow. > > This is difficult to optimise because perl doesn't have any way to > pre-index a data structure in preparation for extracting data. > > Keep down the work by avoiding loop variables, iterate using internal > functions and avoid splitting more than necessary.. Example: > > @lines = LIB::readfile(...); > > $col = 4; > @coldata = map { (split(",",$_,$col+1))[$col] } @lines; > > Here, each element is spit into '$col + 1' columns, after all why split > a data stream into 50 elements when you only want the fourth.. > > The result of split is treated as an array, and the particular column [$col] > is extracted. > > The result of the 'MAP' call is an array containing the fourth column of every > line. > > Now, if you are going to do this more than once, you would split the whole file > first, and then use two separate column extractions. > > Example: > > @lines = LIB::readfile(...); > > # Convert array of SCALARS to array of references to arrays of SCALARS. > @line_data = map { [ split( ",", $_ ) ] } @lines; > > # Get col 3 > @col_3 = map { $_->[3] } @line_data; > > # Get col 7 > @col_7 = map { $_->[7] } @line_data; > > # - I use the $_->[x] syntax here because $$_[x] is ugly. > > HTH > Cheers. > > > > > > Is there better way to achive this?? > > > > Thanks a ton, > > Ajey > > > > -- > > To unsubscribe, e-mail: [EMAIL PROTECTED] > > For additional commands, e-mail: [EMAIL PROTECTED] > > <http://learn.perl.org/> <http://learn.perl.org/first-response> > > > > > > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>