Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

extract row,column

Status
Not open for further replies.

sun9

Programmer
Dec 13, 2006
31
US
I am new to perl and working on a script to extract data from a csv file- my current code parses each line and prints the words from each line using the parse_line method of the Text::parseWords module. When I try to print @words[1] it prints all the contents of the second column. How should I go about extracting the content of a particular row,column?
Code:
while (defined($line = <SOURCE>))
{
@words = &parse_line('\s+', 0, $line);
  $i = 0;
  foreach ($line) {
   print (@words);
   $i++;
   
  }
}

thanks,
sun
 
Before parsing the words, you can separate the columns of your CSV file with a modules such as Text::CSV or Text::CSV_XS. That will allow you to call parse_line() on only the necessary column.
 
ok thanks ishnid, will try it out and post if I have questions.
 
Code:
while (defined($line = <SOURCE>))
{
   @columns = split(/,/$line);
   @words = &parse_line('\s+', 0, $columns[1]);
   print "@words\n";
}

but look into the modules ishnid listed above, they are more flexible for parsng CSV files than the simple regexp I posted, athough that might work fine if there are no embedded commas in the CSV columns.

- Kevin, perl coder unexceptional!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top