jonnyfolk has asked for the wisdom of the Perl Monks concerning the following question:

I'm sorry to trouble you with this, but I just can't quite seem to break through.

I have a spreadsheet (tab delimited) and I want to put the information from field in a certain row into an array, add more information to it, and then put it back! I thought I would be able to get the information from the required field with the following, but as a test I tried to print the field and nothing happened.

my $checkthree = param('check'); open (DATA, "$data") || die "Couldn't get data"; @all=<DATA>; close (DATA); #Read each line of the data file foreach $line (@all){ $line=~s/\n//g; ($one,$two,$three,$four,$five,$six,$seven,) = split ('\t',$line);} if ($checkthree == $three) { last;} print "$four";
Could anybody point me in the right direction, please?

Replies are listed 'Best First'.
Re: isolate a filed from within a spreadsheet
by helgi (Hermit) on Nov 20, 2002 at 16:17 UTC
    Is this a stealth CGI question? Including this:
    my $checkthree = param('check');

    would tend to indicate that it is.

    You really should tell people, it is often highly relevant, albeit I think not in this case.

    You really, really should start your scripts by enabling warnings and strictures. It may seem like a lot of work, but in the long run it will pay you back in saved time a thousandfold.

    use strict; use warnings; my $checkthree = param('check');

    You shouldn't use the filehandle DATA. DATA is a special filehandle for reading in data embedded in your script after the __END__ tag.

    open IN, $data or die "Cannot open $data for reading:$!\n";

    There seems to be no need to read the data file into an array since you are only looping through it once. Use a while loop instead. Don't define variables that you don't use when splitting up the line.

    while (my $line =<DATA>) { my (undef,undef,$three,$four,undef) = split "\t",$line; if ($three eq $checkthree) {print "$three found before $four in li +ne $.\n"; last;} } close DATA;

    --
    Regards,
    Helgi Briem
    helgi AT decode DOT is

      Thank you very much for your help. I'm sorry for my faults - I did not mean to do anything by 'stealth' but I think adding all salient information is down to experience, which I am sadly lacking.

      Your explanations were very clear and and I will follow your general advice. Thanks to you I am back on the rails. Much appreciated.

Re: isolate a filed from within a spreadsheet
by hydo (Monk) on Nov 20, 2002 at 16:04 UTC
    I was working on something like this last night and found DBI::CSV to work quite nicely. You can pass the seperator char during connect() so that you aren't bound to commas.
    my $hndl = DBI->connect( qq{"DBI:CSV:f_dir=/stuff:csv_sep_char=\\t} );
    DBI::CSV for more info.

    I suspect '\t' is not quite right. \011 maybe?