FAQ

On 2/4/10, Chris wrote:
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.
I'm not sure why you write the new values to a file. You could leave
the new values in an array, then later in your program you could
retrieve the new values from the array.


data1.txt
------------
0~s10~s100

====================

use strict;
use warnings;
use 5.010;


open my $IN_FILE, '<', 'data1.txt'
or die "couldn't open data1.txt for reading: $!";

chomp (my $line = <$IN_FILE>);
close $IN_FILE;

my @numbers = split /~s/, $line;
say "@numbers"; #0 10 100

for (@numbers) {
$_ += 1;
}
say "@numbers"; #1 11 101

my $output = join '~s', @numbers;
say $output; #1~s11~s101

open my $OUT_FILE, '>', 'data1.txt'
or die "couldn't open data1.txt for writing: $!";

say $OUT_FILE $output;

close $OUT_FILE;


data1.txt
-------------
1~s11~s101


print() is your friend.

Search Discussions

Discussion Posts

Previous

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 13 of 13 | next ›
Discussion Overview
groupbeginners @
categoriesperl
postedFeb 4, '10 at 7:28a
activeFeb 6, '10 at 11:03a
posts13
users6
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase