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

ok i made a simple perl app that prints a range of numbers easily enough, but how would i make it so it enters each new number on a new line and saves the list to the ~ directory? and also giving each number the same prefix, like 'prefix$number'
#!/usr/bin/perl print "enter the 1st number: \n"; $number1 = <STDIN>; print "enter 2nd number: \n"; $number2 = <STDIN>; @list=($number1..$number2); print "@list\n";

Replies are listed 'Best First'.
Re: making a list
by Zaxo (Archbishop) on May 29, 2005 at 20:25 UTC

    Here:

    #!/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.

    After Compline,
    Zaxo

      thanks, but how could i add a prefix to those numbers? like
      something1 something2 something3
      etc., etc.
        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.

            print $fh "@{[ map { 'something' . $_ } @list ]}\n";

        Added: Or else you could decorate them beforehand: my @list = map { 'something' . $_ } $number1 .. $number2;

        After Compline,
        Zaxo

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
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.
      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
        Try 'type filename' or 'cat filename', depending on which machine you are on.
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.

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}$_"; } } ...


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?

    --
    jpg
      the o reilley book, but it puts me to sleep, and its usually finding to go on perlmonks...and thank you btw

        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