bran4ever has asked for the wisdom of the Perl Monks concerning the following question:

i can add and remove an element from a file(the whole line)

however i am having some issues of add an element on third element of the array ($array [2]) for example in a text file "userlist.txt"

Username : Password : Download | Tom : 12345678 : Thomas : 12345678 : 121312313123131313

i tried to create a code that puts the counted bytes in the download section. I have logged in an account of TOM using http autho etc...and it counts the bytes and it wrote to Thomas account in download

just ask how to search for a specific string and modify that line..

many thanks

Edited by planetscape - added code tags and rudimentary formatting

( keep:0 edit:19 reap:0 )

Replies are listed 'Best First'.
Re: adding an element in existing file..
by Tanktalus (Canon) on Jun 04, 2006 at 02:12 UTC

    It kinda looks like this is really a colon-delimited format. If so, I recommend using DBI and DBD::CSV, and set the delimiter to a colon. It's way easier to adapt your code then to other formats, whether other formats handled by DBD::CSV or other database applications.

      My apologies...here it is:
      if($passwd ne "") { $depass = decode_base64($passwd); print "read this----->$depass\n"; @name = split(/:/,$depass); chomp $name[0]; chomp $name[1]; open(FH, "<userlist.txt"); while (<FH>) { $line = $_; chomp $line; @listuser = split(/:/,$line); chomp $listuser[0]; chomp $listuser[1]; #if user is eligible if (($name[0] eq $listuser[0]) && ($name[1] eq $listuser[1])) { print "this is @name[0] = @listuser[0] : @name[1] = @listuser[1]\n"; print "juz above web sock new\n"; $web = IO::Socket::INET->new(PeerAddr => +$host,PeerPort=>80) or die "$@"; #send request to web server print "I'm juz above the syswrite web\n"; print "WEB SOCK >> $web \n"; print "BUFFER >> $buffer1\n"; syswrite($web,$buffer1); #buffer3 is the web data while($bytes = sysread($web,$buffe +r3,1024)>0) { #output the data to bro +wswer syswrite($browser, $buffer3) +; @nameLine += (split (/\n/, $line)); @byteadd = + (split (/:/, $line)); open(FH, " +>>userlist.txt") or die ("Error, cannot be opened"); + $listuser[2] = $downloadTo +tal; print(FH $ +listuser[2]); close(FH); + last; } }
Re: adding an element in existing file..
by bradcathey (Prior) on Jun 04, 2006 at 02:06 UTC

    Can we see your code so far? Wrapped in <code> tags? Thanks. It's a little hard to understand just what you are doing.


    —Brad
    "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
Re: adding an element in existing file..
by TedPride (Priest) on Jun 04, 2006 at 21:17 UTC
    use strict; use warnings; my ($user, $third, @data); $user = 'Thomas'; $third = 'newvalue'; chomp(@data = <DATA>); for (@data) { # Skip if user is not $user next if !m/^\Q$user\E\s*:/; # Change third field and exit loop @_ = split /:/, $_; $_[2] = $third; $_ = join ':', @_; last; } # Print data back to file (replace this) print $_, "\n" for @data; __DATA__ Username : Password : Download | Tom : 12345678 : Thomas : 12345678 : 121312313123131313