14 Replies - 1629 Views - Last Post: 29 July 2009 - 11:34 AM

#1 adaml5   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-July 09

Perl: trying to figure out where i went wrong with my code

Post icon  Posted 22 July 2009 - 10:40 AM

I am trying to get my code to prompt the user to create a filename then prompt the user to create text to save to that file any help where I am going wrong would be great.

#!/usr/bin/perl -w 
print STDOUT "Enter the filename: ";
chomp($infile=<STDIN>);
open(INFILE,"<$infile");  
print STDOUT "Enter text: ";   
chomp($infile=<STDIN>);
while($line=<INFILE>){
$outfile=$infile;
open(OUTFILE,">$outfile");
print OUTFILE $line;
}
close(OUTFILE);



Is This A Good Question/Topic? 0
  • +

Replies To: Perl: trying to figure out where i went wrong with my code

#2 KevinADC   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Perl: trying to figure out where i went wrong with my code

Posted 22 July 2009 - 03:47 PM

I don't understand your question. Please try and explain it more clearly.
Was This Post Helpful? 0
  • +
  • -

#3 adaml5   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-July 09

Re: Perl: trying to figure out where i went wrong with my code

Posted 23 July 2009 - 06:53 AM

View PostKevinADC, on 22 Jul, 2009 - 02:47 PM, said:

I don't understand your question. Please try and explain it more clearly.




the code isn't working I want it to create a file then be able to write to the file
Was This Post Helpful? 0
  • +
  • -

#4 KevinADC   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Perl: trying to figure out where i went wrong with my code

Posted 23 July 2009 - 10:14 AM

#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;

print "Enter the filename: ";
chomp(my $infile = <STDIN>);

#here you should filter $infile to make sure its nothing "dangerous"

print "Enter some text to write to $infile: ";
chomp(my $text = <STDIN>);

open(my $FH, ">", $infile) or die "Can't open $infile for create/write: $!";
print $FH $text,"\n";
close $FH;

print "Text was printed to file\nGood-bye";
exit(0);


Was This Post Helpful? 1
  • +
  • -

#5 adaml5   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-July 09

Re: Perl: trying to figure out where i went wrong with my code

Posted 23 July 2009 - 10:26 AM

thank you now if i want to make it so i can type multiple lines to the file would i need to create a while loop or is there an easier way to write multiple lines. i need help creating a while loop to be able to write multiple lines

This post has been edited by adaml5: 23 July 2009 - 11:55 AM

Was This Post Helpful? 0
  • +
  • -

#6 KevinADC   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Perl: trying to figure out where i went wrong with my code

Posted 23 July 2009 - 02:19 PM

Lets see some effort on your part first to try and solve the multiple line input requirement.
Was This Post Helpful? 0
  • +
  • -

#7 adaml5   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-July 09

Re: Perl: trying to figure out where i went wrong with my code

Posted 27 July 2009 - 07:40 AM

not sure how to do a loop and my attempts keep failing but this is what i have after a few attempts

#!/usr/bin/perl

print "Enter the filename: ";
chomp(my $infile = <STDIN>);

print "Enter some text to write to $infile: ";
chomp(my $text = <STDIN>);

while($text=<INFILE>)
		print OUTFILE $line;
	
open(my $FH, ">", $infile) or die "Can't open $infile for create/write: $!";
print $FH $text,"\n";
close $FH;

print "Text was printed to file\nGood-bye";
exit(0);




also tried this but failed

#!/usr/bin/perl

print "Enter the filename: ";
chomp(my $infile = <STDIN>);

print "Enter some text to write to $infile: ";
chomp(my $text = <STDIN>);

while(<$text>)
{
   if (s#\\$#)
   {
	  $_ .= <$text>;
	  redo;
   }
}
	
open(my $FH, ">", $infile) or die "Can't open $infile for create/write: $!";
print $FH $text,"\n";
close $FH;

print "Text was printed to file\nGood-bye";
exit(0);



not sure how to create a correct loop yet

alright this loop is working but not writing the text i write to the file

#!/usr/bin/perl

print "Enter the filename: ";
chomp(my $infile = <STDIN>);

print "Enter some text to write to $infile: ";
chomp(my $text = <STDIN>);

$text = 0;
while($text++ < 3) {
  chomp(my $line = <STDIN>);
  last if $line =~ /quit/;
  next if $line =~ /while|until/i;
  redo;
}

open(my $FH, ">", $infile) or die "Can't open $infile for create/write: $!";
print $FH $text,"\n";
close $FH;

# end of the loop

print "Text was printed to file\nGood-bye";
exit(0);



any help would be great thank you

This post has been edited by adaml5: 27 July 2009 - 08:22 AM

Was This Post Helpful? 0
  • +
  • -

#8 adaml5   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-July 09

Re: Perl: trying to figure out where i went wrong with my code

Posted 27 July 2009 - 10:24 AM

i think i am getting closer but i am having trouble getting the text to print to the file here is my code now

#!/usr/bin/perl

print "Enter the filename: ";
chomp(my $infile = <STDIN>);

print "Enter some text to write to $infile: ";
chomp(my $text = <STDIN>);

$text = <STDIN>;
while(1) {
  my $line = <STDIN>;
  chomp($line);
  push(@variable, $line);
  last if $line =~ /./;
}

open(my $FH, ">", $infile) or die "Can't open $infile for create/write: $!";

foreach $var(@array){
  print $FH $text,"\n";
  close $FH;
}


print "Text was printed to file\nGood-bye";
exit(0);


Was This Post Helpful? 0
  • +
  • -

#9 adaml5   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-July 09

Re: Perl: trying to figure out where i went wrong with my code

Posted 27 July 2009 - 11:56 AM

#!/usr/bin/perl

print "Enter the filename: ";
chomp(my $infile = <STDIN>);

print "Enter some text to write to $infile: ";
chomp(my $text = <STDIN>);

$text = <STDIN>;
while(1) {
  my $line = <STDIN>;
  chomp($line);
  push(@variable, $line);
  last if $line =~ /./;
}

open(FH, ">", $infile) or die "Can't open $infile for create/write: $!";

foreach $var(@array){
  print FH $var."\n";
}

close FH;

print "Text was printed to file\nGood-bye";
exit(0);


this is the code i came up with but does not work correctly and i cant seem to fix it or figure out where i went wrong
Was This Post Helpful? 0
  • +
  • -

#10 KevinADC   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Perl: trying to figure out where i went wrong with my code

Posted 27 July 2009 - 12:55 PM

Untested code:

#!/usr/bin/perl

print "Enter the filename: ";
chomp(my $infile = <STDIN>);
open(my $FH, ">", $infile) or die "Can't open $infile for create/write: $!";

while(1) {
   print "\nEnter some text to write to $infile: ";
   chomp(my $text = <STDIN>);
   last if $text =~ /^./;
   print $FH "$text\n";
}
close $FH;

print "Text was printed to file\nGood-bye";
exit(0);


Was This Post Helpful? 0
  • +
  • -

#11 adaml5   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-July 09

Re: Perl: trying to figure out where i went wrong with my code

Posted 27 July 2009 - 01:10 PM

that code doesnt seem to work either it only allows one line to be written and it doesnt end up in the file
Was This Post Helpful? 0
  • +
  • -

#12 KevinADC   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Perl: trying to figure out where i went wrong with my code

Posted 27 July 2009 - 02:10 PM

oops, change this line:

last if $text =~ /^./;

change to:

last if $text =~ /^\./;

that way when you enter a dot the script breaks out of the loop instead of when you enter anything. That should do it.
Was This Post Helpful? 0
  • +
  • -

#13 adaml5   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-July 09

Re: Perl: trying to figure out where i went wrong with my code

Posted 28 July 2009 - 06:10 AM

thank you works perfect i think i was complicating it to much
Was This Post Helpful? 0
  • +
  • -

#14 adaml5   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-July 09

Re: Perl: trying to figure out where i went wrong with my code

Posted 29 July 2009 - 07:59 AM

i am now trying to print the text to the screen after printing it to the file to show what was printed to the file this is my code but i cant get it to print to the screen not sure if i am doing this right

#!/usr/bin/perl

print "Enter the filename: ";
chomp(my $infile = <STDIN>);
open(my $FH, ">", $infile) or die "Can't open $infile for create/write: $!";

while(1) {
   print "\nEnter text to write to $infile: ";
   chomp(my $text = <STDIN>);
   last if $text =~ /^\./;
   print $FH "$text\n";
}
close $FH;

open (MYFILE, '$infile, .txt');
while (<MYFILE>) {
chomp;
print "$_\n";
}
close (MYFILE);

print "This text was printed to file";


exit(0);


Was This Post Helpful? 0
  • +
  • -

#15 KevinADC   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Perl: trying to figure out where i went wrong with my code

Posted 29 July 2009 - 11:34 AM

In almost all programming languages, details are very important. In this line:

open (MYFILE, '$infile, .txt');


you have used single-quotes which kill variable interpolation and entered a comma and a space for unknown reasons. Your code is literally trying to open a file named "$infile, .txt" (without the double-quotes).

Should just be:

open (MYFILE, $infile);

but you should always check system level functions for success or failure:

open (MYFILE, $infile) or die "Can't open $infile: $!";

but if you are just wanting to print the contents of a file you can use very little code:

open (MYFILE, $infile) or die "Can't open $infile: $!";
print <MYFILE>;
close MYFILE;


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1