Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

OK... I am confused and lost :( Editing FlatFile

by LostS (Friar)
on Aug 27, 2001 at 22:35 UTC ( #108214=perlquestion: print w/ replies, xml ) Need Help??
LostS has asked for the wisdom of the Perl Monks concerning the following question:

Hey Everyone Again

OK I have this script where a user can update an entry however when I do the modify the record it doesn't work... Here is my code... Can someone help??

open (DBASE,"$hrdata/$qfile"); @ODB=<DBASE>; close (DBASE); open (DATABASE,">$hrdata/$qfile"); foreach $rec (@ODB){ chomp($rec); ($oldpositionid,$oldinstitution,$olddepartment,$oldconfirstname,$oldc +onlastname,$oldconemail,$oldconphone,$oldtitleposition,$oldposnumber, +$oldtypeposition,$oldprogramtype,$olddvmonth,$olddvday,$olddvyear,$ol +dedmonth,$oldedday,$oldedyear,$oldnumposition,$oldnumvacunit,$oldtots +alary,$oldbudgetsalary,$oldstatefundsources,$oldfedfundsources,$oldst +atebudgetcat,$oldpriclass,$oldsubip,$oldthetime,$oldapproved) = split +(/\t/,$rec); if ($oldpositionid != $viewpositionid) { print DATABASE "$oldpositionid\t$oldinstitution\t$olddepartment\t$ol +dconfirstname\t$oldconlastname\t$oldconemail\t$oldconphone\t$oldtitle +position\t$oldposnumber\t$oldtypeposition\t$oldprogramtype\t$olddvmon +th\t$olddvday\t$olddvyear\t$oldedmonth\t$oldedday\t$oldedyear\t$oldnu +mposition\t$oldnumvacunit\t$oldtotsalary\t$oldbudgetsalary\t$oldstate +fundsources\t$oldfedfundsources\t$oldstatebudgetcat\t$oldpriclass\t$o +ldsubip\t$oldthetime\t$oldapproved\n"; } else { print DATABASE "$viewpositionid\t$institution\t$department\t$confirs +tname\t$conlastname\t$conemail\t$conphone\t$titleposition\t$posnumber +\t$typeposition\t$programtype\t$dvmonth\t$dvday\t$dvyear\t$edmonth\t$ +edday\t$edyear\t$numposition\t$numvacunit\t$totsalary\t$budgetsalary\ +t$statefundsources\t$fedfundsources\t$statebudgetcat\t$priclass\t$ENV +{REMOTE_ADDR}\t$thetime\tNo\n"; } } close (DATABASE);

What is happened is it is reading each entry but when it writes it writes the same line as many times as there are entries... All of them being the entry I edited :(


-----------------------
Billy S.
Slinar Hardtail - Guildless
Datal Ephialtes - Guildless
RallosZek.Net Admin/WebMaster
Aerynth.Net Admin/WebMaster

perl -e '$cat = "cat"; if ($cat =~ /\143\x61\x74/) { print "Its a cat! +\n"; } else { print "Thats a dog\n"; } print "\n";'

Comment on OK... I am confused and lost :( Editing FlatFile
Select or Download Code
Re: OK... I am confused and lost :( Editing FlatFile
by runrig (Abbot) on Aug 27, 2001 at 22:41 UTC
    use strict and warnings (-w). Consider using an array or a hash instead of all those variable names. E.g.:
    my @fields = qw(position institution dept etc); for my $rec (@records) { chomp $rec; my %old; @old(@fields} = split /\t/, $rec; if ($old{position} != $viewposition) { print join("\t", @old{@fields}),"\n"; } else { ... } }
      Thanks for the kick in the head... found the issue... :)


      -----------------------
      Billy S.
      Slinar Hardtail - Guildless
      Datal Ephialtes - Guildless
      RallosZek.Net Admin/WebMaster
      Aerynth.Net Admin/WebMaster

      perl -e '$cat = "cat"; if ($cat =~ /\143\x61\x74/) { print "Its a cat! +\n"; } else { print "Thats a dog\n"; } print "\n";'
Re (tilly) 1: OK... I am confused and lost :( Editing FlatFile
by tilly (Archbishop) on Aug 28, 2001 at 21:14 UTC
    If you find yourself writing long strings of named variables, particularly if you have to synchronize them between different pieces of code you should use hashes and hash slices.

    For instance suppose that somewhere you have:

    @fields = qw( viewpositionid institution department confirstname conlastname conemail conphone titleposition posnumbert typeposition programtype dvmonth dvday dvyear edmonth edday edyear numposition numvacunit totsalary budgetsalary statefundsources fedfundsources statebudgetcat priclass subip thetime approved );
    Then your loop could be replaced with:
    foreach my $rec (@ODB) { chomp($rec); my %db_rec; @db_rec{@fields} = split /\t/, $rec; if ($db_rec{viewpositionid} == $cur_rec{viewpositionid}) { # Replace the existing with the updated. %db_rec = %cur_rec; $cur_rec{sub_ip} = $ENV{REMOTE_ADDR}; $cur_rec{approved} = "No"; } print DATABASE join "\t", @db_rc{@fields}; print DATABASE "\n"; }
    which is much clearer, and less error prone. (I would choose easier names to read and also try to lose the flatfile database, but one step at a time...)

    The key points here are that people tend to mess up positional logic (computers don't) and you want to try to reduce the number of places that any given item of information (eg what fields appear in what order). Plus losing a ton of globals is generally a Good Thing...

    Updated Steve_p - fixed the opening code tag on the second block of code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2014-07-13 18:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (251 votes), past polls