CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Post deleted by gerble1000

 



gerble1000
User

Apr 29, 2012, 11:28 AM

Post #1 of 11 (2454 views)
Post deleted by gerble1000

 


rovf
Veteran

Apr 30, 2012, 1:50 AM

Post #2 of 11 (2105 views)
Re: [gerble1000] need help with a simple code [In reply to] Can't Post


Quote
where do i start


You start like you would do with *any* problem, where you don't know how to code something in a particular language: Write down your algorithm without thinking about Perl - you can use plain English, some self-invented pseudo code, or any other programming language you are used to. This would also help us understand your problem, because not everything is clear from your description (I understand it only so far that you need to copy a part of a file to a different file).


gerble1000
User

Apr 30, 2012, 2:16 AM

Post #3 of 11 (2086 views)
Re: [rovf] need help with a simple code [In reply to] Can't Post

ok here goes

Code
 
my $target = 15;
my $num = 0;


:START
$num +=1;
open FILE, "input.txt" or die $!
my @lines = <FILE>;
delete @lines[1]
close FILE;
my $str = @lines[1]



open FILE, ">>", "output.txt" or die $!
print FILE $str;
close FILE;

if ($num == $target)
{
goto finish;
}
goto start;



:finish

hows that look


rovf
Veteran

Apr 30, 2012, 2:44 AM

Post #4 of 11 (2074 views)
Re: [gerble1000] need help with a simple code [In reply to] Can't Post

(1) Please, in any case,


gerble1000
User

Apr 30, 2012, 3:34 AM

Post #5 of 11 (2051 views)
Re: [rovf] need help with a simple code [In reply to] Can't Post

please :)


rovf
Veteran

Apr 30, 2012, 3:47 AM

Post #6 of 11 (2045 views)
Re: [gerble1000] need help with a simple code [In reply to] Can't Post

Ooops, looks that most of my post disappeared!!!! That's weird (I even had used the preview to check that everything was there!!!!!). Sorry, I have to put it together again!


rovf
Veteran

Apr 30, 2012, 3:49 AM

Post #7 of 11 (2043 views)
Re: [gerble1000] need help with a simple code [In reply to] Can't Post

Ah, luckily I still had a copy of my post in one of my browser tabs. Here we go - hope that it works this time:

----------------------------------------------------------------------------------
(1) Please, by any means, put at the beginning of your program

use strict; use warnings FATAL => 'all'; use diagnostics;

(the FATAL => 'all' and use diagsnostics part is not necessary, after you get more experienced with Perl, but for you as a beginner, it STRONGLY advisable).

(2) I hope that you won't use any GOTO in your final program, do you????

(3) Your statement


Code
delete @lines[1]


doesn't do what you expect. First, when you refer to one element of the array, you have to use $ instead of @, i.e. delete $lines[1]. Unless your array had exactly 2 elements, this would set the second element of the array (indices start at zero) to undef, i.e. the number of array elements stays unchanged. Only if your element had 2 elements before, the size of the array would shrink.

I think you want to remove an element of the array. To remove the first element, use the function shift. To remove an element from somewhere inside the array, use the function splice.

(4) I would not read the whole file into an array, but read the file line by line, and after each line decide, whether to copy it to the output file. In pseudocode, this would look like:

open inputfile
open outputfile
while (read next line from inputfile)
if this line should go to outputfile
write line to outputfile
end
end
close outputfile
close inputfile

When coding this, you will find it helpful to know that the special variable


Code
$.


contains the line number of the last line read so far.

Hope that helps. Please try to incorporate the changes suggested, ensure that your program at least compiles correctly, and if you have more questions, just respond here.


gerble1000
User

Apr 30, 2012, 7:11 AM

Post #8 of 11 (1955 views)
Re: [rovf] need help with a simple code [In reply to] Can't Post

ideal that helped..
here is what i have and works.
what do u think


Code
my $target = 14;  
my $num = 0;


START:
$num +=1;

open(FILE,'input.txt');
my @lines = <FILE>;
close(FILE);


my $str = $lines[0];

open INFILE, '<input.txt';
open OUTFILE, '>input1.txt';
<INFILE>; #discards first line
print OUTFILE $_ while (<INFILE>);
close OUTFILE;
close INFILE;

rename("input1.txt", "input.txt");






open (F, ">>", "output.txt");
print F $str;
close (F);

if ($num == $target)
{
goto FINISH;
}
goto START;



FINISH:



rovf
Veteran

Apr 30, 2012, 7:51 AM

Post #9 of 11 (1940 views)
Re: [gerble1000] need help with a simple code [In reply to] Can't Post

I think, with the exception of item (3), you have incorporated not a single one of my proposals.....


FishMonger
Veteran / Moderator

Apr 30, 2012, 8:48 AM

Post #10 of 11 (1912 views)
Re: [gerble1000] need help with a simple code [In reply to] Can't Post

perl -ne "print if 1..14" input.txt > output.txt

or

perl -ni.bak -e "print if 1..14" input.txt


(This post was edited by FishMonger on Apr 30, 2012, 8:49 AM)


gerble1000
User

Apr 30, 2012, 9:25 AM

Post #11 of 11 (1894 views)
Re: [rovf] need help with a simple code [In reply to] Can't Post

sorry.
but you did help
i tend to learn by copying someones code and modifying it to do what i need.
and what you posted got me to get the code :)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives