Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^3: Input files <>

by davido (Cardinal)
on Apr 04, 2012 at 07:48 UTC ( [id://963379]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Input files <>
in thread Input files <>

Can you please show us the exact command you are using to invoke the script?


Dave

Replies are listed 'Best First'.
Re^4: Input files <>
by Anonymous Monk on Apr 04, 2012 at 18:17 UTC
    Dave thank you for getting back to me. The command is written as follows: namelist.pl names.txt Ostra
      This is the script that I am using:
      #!/usr/bin/perl -w use strict; my $input=''; my @raw=(); while(defined ($input=<>)){ chomp($input); @raw=$input; } print "@raw\n";
      names.txt contents are: I am going home. When are you leaving? Any feedback will be appreciated. Ostra

      Code tags added by GrandFather

        Can you please learn to use <code> tags in your posts? They will render your code legible, at least.

        Ok, the code you posted is this:

        #!/usr/bin/perl -w use strict; my $input=''; my @raw=(); while(defined ($input=<>)){ chomp($input); @raw=$input; } print "@raw\n";

        Which means you didn't follow any of the suggestions I provided earlier -- kinda demotivates me from continuing, but I'll proceed.

        Modify your code to look like this:

        #!perl use strict; use warnings; my @raw; while( my $input = <> ) { chomp $input; push @raw, $input; } print "@raw\n";

        Now I'll explain the differences.

        1. #!perl instead of #!/usr/bin/perl -w: Strawberry Perl runs under Windows. Windows uses your PATH environment variable and your file extension (.pl) to determine what to do when you type myscript.pl. That means the shebang line is only really used for command-line switches. But just for clarity we'll leave it in there, minus the path information. Furthermore, the path information you were using is probably invalid (it's unlikely that your Perl interpreter is installed at C:/usr/bin/perl. See perlwin32 for further details.
        2. use warnings; The warnings pragma is lexically scoped, whereas the -w flag is global, which may result in unanticipated warning messages from modules. So I didn't use the -w flag, and did use the 'warnings' pragma. See perlrun and warnings for additional information.
        3. my $input has been moved to within the while(){} loop's scope; you're not using it at the broader scope, so there's no reason to scope it more broadly than that. And I've omitted the = '' part, because it was unnecessary. See perlsyn and perlsub for details on while(){} and lexical (my) scoping.
        4. I removed the = (); from my @raw = ();. It's unnecessary; the array will be empty to begin with. This isn't an auto from C.
        5. I removed the defined function call. It's the default behavior of the construct we're using. See the <> operator section of perlop.
        6. push @raw, $input; instead of @raw = $input;. When you assign a scalar variable ($input) to an array (@raw) you clobber whatever contents was already in the array, and assign the new value to $array[0] (the first element). I'm sure this is not what you intended. You intended to build an array by adding $input as the next element on each iteration. That's what push does.</c>

        If you don't need the entire file to be held in memory at the same time, you could rewrite the above like this:

        #!perl use strict; use warnings; while( <> ) { chomp; print; } print $/;

        You were seeing no output because the last line of your file was probably blank. Since your original code only kept the last line (throwing away all the rest), the output appeared blank.

        Now for how to invoke the script. If myscript.pl inputfile doesn't work out (perhaps your system isn't configured to recognize the .pl extension), you can instead type perl myscript.pl inputfile. But do ensure that your PATH environment variable points to the perl.exe executable.


        Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://963379]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-23 21:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found