On Feb 4, 10:59 am, shlo...@iglu.org.il (Shlomi Fish) wrote:
> Hi Chris!
>
> Have you visitedhttp://perl-begin.org/yet and read a good introductory book
> or tutorial?
>
> On Thursday 04 Feb 2010 09:27:32 Chris wrote:
>
> > I need some help with this problem.
> > I've got a text file datafile with 1 line of data comprised of 30
> > different numbers delimited with ~s.
>
> > I need to open this file, grab this line of data, split it into
> > individual numbers, perform some simple math (addition) on each
> > number, and then put the new values back into the datafile, replacing
> > the original data that was there.
>
> > I have tried for two days to figure out what I'm doing wrong, but I
> > keep getting "needs a package name" errors for every variable
> > nomenclature I try to work with. I've declare arrays and variables
> > until my fingers are numb and nothing is working. I'm using Perl 5.008
> > if that matters.
>
> You need to declare variables using "my". Can you show us what you have so
> far?
>
> Regards,
>
>         Shlomi Fish
>
> > Can somebody please help?
>

Yes I have read several tutorials, and have a basic understanding of
how to open a file and read the data into an array. The problem I run
into is that once I close the file and perform my math functions and
re-open the file to write the data to, the data is gone. Empty values
are written to the file, making the file itself empty. I need to know
the proper nomenclature for the arrays and variables so that the
values of variables/elements are static for the next action. Here's
one of the methods I tried to use that failed:

#open file, get data, close file
open(FILE, "<datafile.txt") or die("Unable to open data file.");
    while (<FILE>) {
        my($a,$b,$c) = split(/~/, $_);
    }
    close(FILE);
#perform math
    $a = $a+$previousarray[0]; #add the value of $a and the value of
an element in a previous array
    $b = $b+$previousarray[1];
    $c = $c+$previousarray[2];
#write new values to the datafile
open(FILE, ">datafile.txt") or die ("unable to open file.");
print FILE ("$a~");
print FILE ("$b~");
print FILE ("$c~");
close(FILE);
#end of problem section

The above produces an empty datafile. I have tried to declare an
explicit array for the file data using the following:

open(FILE, "<datafile.txt") or die("Unable to open data file.");
@values=<FILE>;
close(FILE);
foreach $element (@values)
{
($a,$b,$c) = split(/~/, $element);
    $a = $a+$previousarray[0];
    $b = $b+$previousarray[1];
    $c = $c+$previousarray[2];
#then open the datafile and write new values.

I keep getting "use of unititialized values" errors, and "needs
explicit package name" errors, and an empty datafile.

That's why I came to this forum for help. I also neglected to note in
my original post that I need to access these new values elsewhere in
the script. This specific action (open file, get data, perform math,
write data) occurs in a subroutine in a much larger script, and I need
to use these values in other subroutines in the script. But that's not
the main problem yet. The main problem that prompted me to ask the
question in the first place is getting this portion to work correctly
so that I have the correct values in the datafile after these actions
are performed.


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to