Home  |  Linux  | Mysql  | PHP  | XML
From:Shlomi Fish Date:Fri Feb  5 11:15:37 2010
Subject:Re: 1-line datafile, need data for another routine
Hi Chris!

On Friday 05 Feb 2010 11:56:49 Chris Coggins wrote:
> I posted a question to this list yesterday from the google groups
> interface, asking for help with the following. I have since tried to
> post additional details in replies to that message using the google
> groups site and none of my posts have shown up on the list so let's try
> this again the right way.
>
> I have a datafile with 1 line of data, 30 numbers delimited by tildes.
> (Sample code below only uses 4 numbers to save time)
>
> 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 neglected to mention in my original post that I need to access these
> new values elsewhere in the script to perform additional math functions
> with them.
>
> I've tried several variations of the code below, including using arrays,
> and none of them got through without the script failing. The new
> datafile gets written, but it merely contains a ~. The errors I get are
> "use of uninitialized values in such and such line" and "variables needs
> explicit package name". When the script does run, the variables lose
> their values as soon as I close the file after inhaling it, making all
> the rest of the actions in the subroutine futile.
>
> the datafile used below initially contains 30 values that were written
> previously, and this has been verified over and over.
>

Commenting on your code. Also, did you try using the Perl debugger ("perl -d")
to debug the code?

> sub newValues {
> my($file) = shift;
>

1. Your code needs indentation.

2. Either do:

<<
my $file = shift;
>>

Or:

<<
my ($file) = @_;
>>

Otherwise, it is confusing and the meaning may be lost.


> open(FILE, "<$file") or die("Unable to open data file.");

1. Don't use bareword filehandles - use lexicals.

2. Use three args open -

open my $fh, "<", $file or die...;

> while (<FILE>) {

Better do while (my $line = <FILE>) instead of relying on $_.

> my($a,$b,$c,$d) = split(/~/, $_);

Don't call your lexical variables $a and $b - they are special built in
variables. And you probably want an array here.

> $a = $a+$previousarray[4]; # I've tried to perform this math action
> after close(FILE) and get the same result -> failure.
> $b = $b+$previousarray[7];
> $c = $c+$previousarray[0];

You are updating the values of $a, $b, and $c, but they:

1. Will disappear after the loop has exited.

2. Won't be available after the loop's exit.

> }
> close(FILE);
> open(FILE, ">$file") || die ("unable to open file");

Again, see my remarks about open.

> print FILE ("$a~$b~$c \n");

Are you using "strict" and "warnings"?

Regards,

Shlomi Fish

> close(FILE);
> }
> #end sub
>
> Now my datafile just contains a single tilde and no values. I've also
> tried to bring the original data from the datafile into my(@array) and
> get the same errors.
>
> Keep in mind I need other subroutines to be able use these new values,
> but these local variables lose their values once the file is closed.
> I've also tried to declare them as global variables but I'm not doing
> something right because the script won't execute.
>
> I really need some help figuring this out if somebody would be kind
> enough to help a novice.

--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
First stop for Perl beginners - http://perl-begin.org/

Deletionists delete Wikipedia articles that they consider lame.
Chuck Norris deletes deletionists whom he considers lame.

Please reply to list if it's a mailing list post - http://shlom.in/reply .
Navigate in group perl.beginners at sever nntp.perl.org
Previous Next


Your recent visits
Re: complex subroutine arguments
Re: module installation
Re: module installation
Re: Traversing Hash printing two times
Re: Need help with Mail::Sender



  
© No Copyright
You are free to use Anything, but please consult your advocate before doing so as this website
also list content from other sources which may be copyrighted.
Site Maintained by Zareef Ahmed
Powered By PHP Consultants