file name from command line

Discussion in 'Perl Misc' started by nicolas-laurent, Mar 23, 2005.

  1. New to Programming, I am trying to open a file from a command line
    split.pl in.txt and print each word on separate line. Something is
    missing but I don<t know what ? Please help

    #
    while ( $_ = <ARGV> ) {

    # splits the line into an array of words
    @words = split(/ /, $line);
    }
    print "@words;
    nicolas-laurent, Mar 23, 2005
    #1
    1. Advertising

  2. nicolas-laurent

    phaylon Guest

    nicolas-laurent wrote:

    > New to Programming, I am trying to open a file from a command line
    > split.pl in.txt and print each word on separate line. Something is missing
    > but I don<t know what ? Please help


    First, please read the posting guidelines that are posted frequently.
    There are many helpful hints which make your and our life easier.

    First of all, you should use

    use warnings;
    use strict;

    which raise the common errors. For example, it says

    Can't find string terminator '"' anywhere before EOF at - line 6.

    > while ( $_ = <ARGV> ) {


    Try using

    while (<>) {
    # do_stuff();
    }

    hth,p

    --
    http://www.dunkelheit.at/
    bellum omnium pater.
    phaylon, Mar 23, 2005
    #2
    1. Advertising

  3. nicolas-laurent <> wrote:

    > New to Programming,



    We can easily see that for ourselves.


    > I am trying to open a file from a command line
    > split.pl in.txt and print each word on separate line. Something is
    > missing but I don<t know what ?



    There are several things missing:

    It is not even a Perl program, it has syntax errors.

    You put stuff into $_, but never do anything else with $_.

    You are attempting to split $line when you have not given it a value.

    Since you stomp over @words each loop iteration, the print outside
    the loop will show only words from the very last line of the file.
    If the last line happens to be blank, it will make no output at all.


    > while ( $_ = <ARGV> ) {
    >
    > # splits the line into an array of words
    > @words = split(/ /, $line);
    > }
    > print "@words;



    while ( <> ) {
    print "$_\n" for split / /;
    }


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Mar 23, 2005
    #3
  4. phaylon <> wrote:
    > nicolas-laurent wrote:


    > First of all, you should use
    >
    > use warnings;
    > use strict;
    >
    > which raise the common errors. For example, it says
    >
    > Can't find string terminator '"' anywhere before EOF at - line 6.



    You will get that message even *without* strict and warnings,
    as it is for all syntax errors.


    >> while ( $_ = <ARGV> ) {

    >
    > Try using
    >
    > while (<>) {



    That is the same thing, only written differently, so it is unlikely
    to solve the problem.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Mar 23, 2005
    #4
  5. nicolas-laurent

    phaylon Guest

    Tad McClellan wrote:

    > You will get that message even *without* strict and warnings, as it is for
    > all syntax errors.


    Ouch. Ok, that was a bad error.

    > That is the same thing, only written differently, so it is unlikely to
    > solve the problem.


    That was my bad memory, sorry for not double-checking before posting.

    --
    http://www.dunkelheit.at/

    Ordinary morality is only for ordinary people.
    -- Aleister Crowley
    phaylon, Mar 23, 2005
    #5
  6. nicolas-laurent

    Guest

    use strict;

    if (!@ARGV) {
    print "Enter a filename as a cmdline argument.\n";
    exit (1);
    }
    if (!open(FILE, $ARGV[0])) {
    die "can't open $ARGV[0]\n";
    }

    print "\nFile: @ARGV[0]\n";
    my $lcnt = 1;
    while (<FILE>) {
    chomp;
    print "L - ".$lcnt++."\n";
    my $wcnt = 1;
    for (my @words=split(' ')) {
    my $info = sprintf (" %d\t%s", $wcnt++, $_);
    print "$info\n";
    }
    }
    close (FILE);

    1;
    , Mar 24, 2005
    #6
  7. <> wrote:

    > if (!@ARGV) {
    > print "Enter a filename as a cmdline argument.\n";
    > exit (1);
    > }



    Error message should go on STDERR, so you can replace
    that whole if statement with:

    warn "Enter a filename as a cmdline argument.\n" unless @ARGV;


    > print "\nFile: @ARGV[0]\n";


    You should always enable warnings when developing Perl code!

    (especially when you purport to be answering someone's question.)


    > my $lcnt = 1;



    Why not just use the $. variable instead of maintaining your own?


    > for (my @words=split(' ')) {



    A pattern match should *look like* a pattern match.

    Why do you bother putting the words into @words?

    You never use the @words variable after this.


    for ( split / / ) {


    > my $info = sprintf (" %d\t%s", $wcnt++, $_);
    > print "$info\n";



    Why not just use printf()?

    printf (" %d\t%s\n", $wcnt++, $_);


    > 1;



    Why is there a one there?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Mar 24, 2005
    #7
  8. Tad McClellan <> wrote:
    > <> wrote:
    >
    >> if (!@ARGV) {
    >> print "Enter a filename as a cmdline argument.\n";
    >> exit (1);
    >> }

    >
    >
    > Error message should go on STDERR, so you can replace
    > that whole if statement with:
    >
    > warn "Enter a filename as a cmdline argument.\n" unless @ARGV;



    Doh!

    I meant die() instead of warn(), else there will be no exit().


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Mar 24, 2005
    #8
  9. nicolas-laurent

    Guest

    On Thu, 24 Mar 2005 07:30:42 -0600, Tad McClellan
    <> wrote:

    >Tad McClellan <> wrote:
    >> <> wrote:
    >>
    >>> if (!@ARGV) {
    >>> print "Enter a filename as a cmdline argument.\n";
    >>> exit (1);
    >>> }

    >>
    >>
    >> Error message should go on STDERR, so you can replace
    >> that whole if statement with:
    >>
    >> warn "Enter a filename as a cmdline argument.\n" unless @ARGV;

    >
    >
    >Doh!
    >
    >I meant die() instead of warn(), else there will be no exit().


    What the **** do u mean man?
    , Apr 1, 2005
    #9
  10. nicolas-laurent

    Guest

    On Thu, 24 Mar 2005 07:30:42 -0600, Tad McClellan
    <> wrote:

    >Tad McClellan <> wrote:
    >> <> wrote:
    >>
    >>> if (!@ARGV) {
    >>> print "Enter a filename as a cmdline argument.\n";
    >>> exit (1);
    >>> }

    >>
    >>
    >> Error message should go on STDERR, so you can replace
    >> that whole if statement with:
    >>
    >> warn "Enter a filename as a cmdline argument.\n" unless @ARGV;

    >
    >
    >Doh!
    >
    >I meant die() instead of warn(), else there will be no exit().


    What the **** did u mean man?
    , Apr 1, 2005
    #10
  11. <> wrote:
    > On Thu, 24 Mar 2005 07:30:42 -0600, Tad McClellan
    ><> wrote:
    >
    >>Tad McClellan <> wrote:
    >>> <> wrote:
    >>>
    >>>> if (!@ARGV) {
    >>>> print "Enter a filename as a cmdline argument.\n";
    >>>> exit (1);
    >>>> }
    >>>
    >>>
    >>> Error message should go on STDERR, so you can replace
    >>> that whole if statement with:
    >>>
    >>> warn "Enter a filename as a cmdline argument.\n" unless @ARGV;

    >>
    >>
    >>Doh!
    >>
    >>I meant die() instead of warn(), else there will be no exit().

    >
    > What the **** do u mean man?



    It's "computer stuff".

    You wouldn't understand.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Apr 1, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hugo
    Replies:
    10
    Views:
    1,289
    Matt Humphrey
    Oct 18, 2004
  2. kaushikshome
    Replies:
    4
    Views:
    749
    kaushikshome
    Sep 10, 2006
  3. scad
    Replies:
    23
    Views:
    1,136
    Alf P. Steinbach
    May 17, 2009
  4. Iwillpassc
    Replies:
    1
    Views:
    368
    Iwillpassc
    Dec 19, 2009
  5. Ting Wang
    Replies:
    3
    Views:
    185
    Paul Lalli
    Dec 13, 2005
Loading...

Share This Page