Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Replacing a text field in a spreadsheet

by jonnyfolk (Vicar)
on Nov 21, 2002 at 16:01 UTC ( [id://214802]=perlquestion: print w/replies, xml ) Need Help??

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

#!/usr/bin/perl -w use strict; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); use CGI ':standard'; use Date::Calc qw(:all); my $data="path/data.txt"; my $checkthree = param('check'); my $d = param('days'); #2 digit my $m = param('months'); #2 digit my $y = param('years'); #4 digit my $Dd = param('Dd'); my $days = Date_to_Days($y,$m,$d); my $count = 0; my @Calendar; while ($count < $Dd){ push (@Calendar, $days); $count += 1; $days += 1; } print header(), start_html; open IN, $data or die "Cannot open $data for reading:$!\n"; my @line; while (my $line =<IN>) { my ($one,$two,$three,$four,$five,$six,$seven) = split "\t",$line; if ($three eq $checkthree) { push (@Calendar, $seven); my $replacementline = "$one,$two,$three,$four,$five,$six,@Calendar" +; push (@line, $replacementline); last; } } close IN; print "@line";
The aim is to add new dates to those already in the spreadsheet in field $seven. Using input via DATE::CALC I have (finally) succeeded in combining the two sets of dates and getting them out of the 'while' loop in the form of an array (@line).

I thought I would use

foreach (@line) { ($one,$two,$three,$four,$five,$six,$seven) = $_; }
To put the @line into the correct fields then read file $data into a temporary file with the new line and then rename the temp file.

Unfortunately I can't work out how to replace the new line with the old, and I was wondering if someone could give me some advice on how to get the array @line into the correct place in the spreadsheet (or some other way of doing it if I am not on the right track). Thanks very much.

Replies are listed 'Best First'.
Re: Replacing a text field in a spreadsheet
by Nitrox (Chaplain) on Nov 21, 2002 at 16:06 UTC
    You could always reffer to the items you need as:
    $line[0] $line[6]
    etc...

    -Nitrox

      I thought I was getting somewhere with this:
      ... open (IN, "$data") || die "Couldn't get $data page"; my @all = <IN>; close (IN); open (IN2, ">$data.tmp") || die "Couldn't open temporary file"; foreach my $line (@all) { my ($one,$two,$three,$four,$five,$six,$seven) = split "\t",$line; if ($three eq $checkthree) { ($one,$two,$three,$four,$five,$six,$seven) = @line; last; } } rename("$data.tmp", "$data") || die "Couldn't rename file"; print "okay";
      As it went through and printed "okay".
      Unfortunately it wiped the file so obviously my definition of "okay" is different to perl's :-)
      What to do?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://214802]
Approved by cfreak
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2024-04-20 06:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found