adding an element in existing file..
bran4ever
created: 2006-06-03 21:50:56

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 [id://17558|code] tags and rudimentary formatting( keep:0 edit:19 reap:0 )

Re: adding an element in existing file..
created: 2006-06-03 22:06:48

Can we see your code so far? Wrapped in 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..
created: 2006-06-03 22:12:06

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.

Re^2: adding an element in existing file..
created: 2006-06-03 22:35:45
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, ")
		{
		$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,$buffer3,1024)>0) 
               		     				 	{
                       						#output the data to browswer
                      					syswrite($browser, $buffer3);
															@nameLine = (split (/\n/, $line));
															@byteadd = (split (/:/, $line));
															open(FH, ">>userlist.txt") or die ("Error, cannot be opened");															$listuser[2] = $downloadTotal;
																
															print(FH $listuser[2]);
															close(FH);	
															
															last;
								} 
}
Re: adding an element in existing file..
created: 2006-06-04 17:17:41
use strict;
use warnings;

my ($user, $third, @data);

$user = 'Thomas';
$third = 'newvalue';

chomp(@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

perlmonks.org content © perlmonks.org and bradcathey, bran4ever, Tanktalus, TedPride

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03