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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

extract sentence 1

Status
Not open for further replies.

sun9

Programmer
Dec 13, 2006
31
US
I am trying to extract data that have been separated into columns in my csv using Tie::CSV_File, I am able to extract from all columns except one which is actually a sentence so when I try to get $date[4][4] I get just the first word from the sentence, how can I extract the whole sentence?
Code:
tie my @data, 'Tie::CSV_File', $file ,WHITESPACE_SEPARATED;
 
You don't need that open statement, Tie::File opens the file for you. Remove the open statement and see if it helps.

- Kevin, perl coder unexceptional!
 
nope, removing the open statement doesnt help.
 
if it was working before, and suddenly it's not working, then I don't know. Either you changed something or the server was changed. Or your data file is corrupted. Don't do this:

Code:
print ($mydata[3]);

do this:

Code:
print $mydata[3];

no parenthesis ()



- Kevin, perl coder unexceptional!
 
I tried that still doesnt work, I started having this problem after I added chdir to my code.after trying to run the code with the chdir I reverted back to the earlier working and code and it too did not work...could using chdir have something to do with my issue?
Code:
my $code = "chdir('C:/Projects/test1')";
eval($code) || die("$code\n$!");
open(DEST, ">test")|| die("$!");
print DEST  ("$data[3]\n ");
close (DEST);
my $mycode = "chdir('C:/Projects/test2')";
eval($mycode) || die("$mycode\n$!");
open(DEST1, ">test1")|| die("$!\n");
print DEST1 ("$data[3]\n");
close(DEST1);

 
sun9 said:
Code:
my $code = "chdir('C:/Projects/test1')";
eval($code) || die("$code\n$!");
open(DEST, ">test")|| die("$!");

Why would you choose to eval this code? It's a lot cleaner just to run it straight out. I can't imagine any reason why you would need to trap errors for the chdir function since I don't believe it throws any. So simplify the first two lines to this:

Code:
chdir('C:/Projects/test1');

Also, any time that you do an "or die", you really should use or instead of ||. They are obviously very similar, but "or" has a lower precedence, and so you can be sure that it will be called only after assignments and other operations. Also, it's just easier to read as well.

Code:
open(DEST, ">test") or die "$!";

None of these changes should have anything to do with whatever problem that your having. Just trying to help you clean up your code.
 
I can't imagine any reason why you would need to trap errors for the chdir function since I don't believe it throws any

sure it does but it's better written as a simple die check:

Code:
chdir('C:/Projects/test1') or die "Can't chdir to C:/Projects/test1: $!";

- Kevin, perl coder unexceptional!
 
Thanks for your help MillerH and Kevin. The problem is with my environment.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top