Well, if your data file looks EXACTLY like you stated (that is, a file with only 5 lines) using a database (either the tied hash variety or something using DBI) is a little overkill.

If you know about reading and writing Perl files, try reading in the whole file, changing the part you want to change, then writing the whole file back to update it. Like this:

# Load in the whole file open (FILE, "memberpoints.txt") or die "Can't read: $!"; while (<FILE>) { chomp; ($name, $number) = split "-"; $field ($name) = $number; } # Change whatever you want $field{'vegeta'} += 6; # ..etc # Write the whole file back (replacing what was there before) open (FILE, ">memberpoints.txt") or die "Can't write: $!"; foreach (keys %field) { print FILE "$_-$field{$_}\n"; }
But, keep in mind that although something like this works fine for tiny files it doesn't scale well, so if your file gets bigger later, or needs to be accessed lots and lots of times (multiple times per second) you should look into more flexible solutions like a tied hash (SDBM, NDBM, GDBM and siblings) or a full-bore database like MySQL or PostgreSQL.

Gary Blackburn
Trained Killer

Edited: Forgot to chomp the newline. Bad newline. Bad. Added the chomp in.


In reply to Re: Editing files...please help by Trimbach
in thread Editing files...please help by tanger

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.