Re: making a list
by Zaxo (Archbishop) on May 29, 2005 at 20:25 UTC
|
#!/usr/bin/perl
use warnings;
use strict;
print "enter the 1st number: \n";
chomp( my $number1 = <STDIN>);
print "enter 2nd number: \n";
chomp( my $number2 = <STDIN>);
my @list=($number1..$number2);
{
local $" = "\n";
open my $fh, '>', "$ENV{HOME}/number_list.txt"
or die $!;
print $fh "@list\n";
}
The bare block is to restrict the dynamic scope of $" and the lexical scope of $fh. That makes $fh be closed as soon as the block is exited. $" contains the element spacer text used for stringifying arrays.
I've added warnings and strict to help you in future.
| [reply] [d/l] |
|
|
thanks, but how could i add a prefix to those numbers? like
something1
something2
something3
etc., etc. | [reply] [d/l] |
|
|
I like it dirt - instead of
{
local $" = "\n";
open my $fh, '>', "$ENV{HOME}/number_list.txt"
or die $!;
print $fh "@list\n";
}
use
my $prefix = "something";
{
local $" = "\n$prefix";
open my $fh, '>', "$ENV{HOME}/number_list.txt"
or die $!;
print $fh "$prefix@list\n";
}
Of course it's just to show you that you can put whatever separator inside $"; if you want a clean solution, use Zaxo's.
Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')
Don't fool yourself.
| [reply] [d/l] [select] |
|
|
| [reply] [d/l] [select] |
Re: making a list and saving results
by trammell (Priest) on May 29, 2005 at 20:22 UTC
|
I assume you mean print each number on a new line:
print "$_\n" for @list;
Saving the output is something the shell can do trivially:
% perl myprog.pl > ~/mylist.txt
| [reply] [d/l] [select] |
Re: making a list and saving results
by newest_newbie (Acolyte) on May 29, 2005 at 20:46 UTC
|
I am not sure what you want. I am guessing that you want to create a file which gets appended with the range of numbers that are entered.
You will have to open a file, append the data to it.
#!/usr/bin/perl
print "enter the START number: \n";
$number1 = <STDIN>;
print "enter END number: \n";
$number2 = <STDIN>;
@list=($number1..$number2);
print "@list\n";
# if the file already exists.
open OUTFILE, ">>/path/to/outfile" or die "Cannot open $!\n";
# if you want to create new file each time the program is executed.
# open OUTFILE, ">/path/to/outfile" or die "Cannot open $!\n";
map { print OUTFILE $_, "\n"; } @list;
close (OUTFILE);
Hope this helps.
| [reply] [d/l] |
|
|
well it made the list and everything, but instead of the numbers each on a new line, its little squares, and they are all bunched up...i need each number on a new line
| [reply] |
|
|
Try 'type filename' or 'cat filename', depending on which machine you are on.
| [reply] |
Re: making a list and saving results
by TedPride (Priest) on May 29, 2005 at 21:13 UTC
|
That's because you're loading the file in Notepad, which requires both a line break and form feed at the end of each line. Replace \n in the code with \n\f.
EDIT: Oops, I keep forgetting that it's \r instead of \f. Thanks, fishbot_v2. It should be \r\n instead of \n\f as I stated above - though I don't think it matters if the \r comes before or after the \n, since what it does is put the cursor back at the beginning of the current line. | [reply] |
Re: making a list and saving results
by sh1tn (Priest) on May 29, 2005 at 21:56 UTC
|
...
for( 1..3 ){
print "enter the $_{st} number: ";
{
chomp(local $_ = <STDIN>);
push @list, "${prefix}$_";
}
}
...
| [reply] [d/l] |
Re: making a list
by jpeg (Chaplain) on May 29, 2005 at 20:31 UTC
|
You want a function that iterates over an array, or does something ... foreach ... element in an array.
For saving output, you want a function that would ..open..and ..close.. a file. Or maybe just use your shell's redirection.
Can I ask what resources you're using to learn perl?
| [reply] |
|
|
the o reilley book, but it puts me to sleep, and its usually finding to go on perlmonks...and thank you btw
| [reply] |
|
|
surfbass wrote: the o reilley book, but it puts me to sleep
Do you mean the llama book, Learning Perl? I encourage you to tank up on your favorite caffiene source and spend two days to work through the book. Do every single exercise. The physical act of writing the necessary code to do the exercises will implant some PerlThink(tm) in your brain. It may be a drag while you're doing it. Once you're done you'll have a foundation that will make your time spent here much more productive and valuable.
Be Appropriate && Follow Your Curiosity
| [reply] |