perl newbie - getting length of argv array

Discussion in 'Perl Misc' started by xhoster@gmail.com, May 4, 2006.

  1. Guest

    wrote:
    > Hi all,
    >
    > Just starting to learn perl.


    You should almost always use strict and use warnings.

    > I don't seem to be able to get the length
    > of the argv array correctly. Here's my script called perlScript.pl:
    >
    > #! /usr/bin/perl
    > #
    > #
    >
    > $length = @array;
    > print "Length = @argv\n";
    > $length = $#array + 1;
    > print "Length = $length \n";


    Perl is case sensitive. There is no @argv, unless you define one.
    There is an @ARGV.


    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , May 4, 2006
    #1
    1. Advertising

  2. Lionel Guest

    Hi all,

    Just starting to learn perl. I don't seem to be able to get the length
    of the argv array correctly. Here's my script called perlScript.pl:

    #! /usr/bin/perl
    #
    #

    $length = @array;
    print "Length = @argv\n";
    $length = $#array + 1;
    print "Length = $length \n";



    Here's commandline output:

    ../perlScript.pl somearg secondarg third
    Length =
    Length = 0

    Any help appreciated.

    Lionel.
     
    Lionel, May 4, 2006
    #2
    1. Advertising

  3. Dr.Ruud Guest

    Lionel schreef:

    > #! /usr/bin/perl


    #!/usr/bin/perl


    Missing:
    use strict;
    use warnings;


    > $length = @array;


    my $length = @argv;

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, May 4, 2006
    #3
  4. "Dr.Ruud" <> wrote in news:e3bl83.1b4.1
    @news.isolution.nl:

    > Lionel schreef:
    >
    >> #! /usr/bin/perl

    >
    > #!/usr/bin/perl
    >
    >
    > Missing:
    > use strict;
    > use warnings;
    >
    >
    >> $length = @array;

    >
    > my $length = @argv;


    Quick correction:

    my $length = @ARGV;

    In most cases, however, there is no need to save the length. Using @ARGV
    in scalar context works fine, and is more transparent:

    unless ( @ARGV == 2 ) {
    die <<EO_USAGE;
    bzzt: Please provide an input and output filename

    EO_USAGE
    }

    Sinan

    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, May 4, 2006
    #4
  5. Lionel Guest

    wrote:
    > wrote:
    >> Hi all,
    >>
    >> Just starting to learn perl.

    >
    > You should almost always use strict and use warnings.


    What is strict? I was using warnings but didn't included it on this post.

    >
    >> I don't seem to be able to get the length
    >> of the argv array correctly. Here's my script called perlScript.pl:
    >>
    >> #! /usr/bin/perl
    >> #
    >> #
    >>
    >> $length = @array;
    >> print "Length = @argv\n";
    >> $length = $#array + 1;
    >> print "Length = $length \n";

    >
    > Perl is case sensitive. There is no @argv, unless you define one.
    > There is an @ARGV.


    Cheers, that got it. I also noticed that I had "@array" instead of @ARGV
    above . . . bad copy and paste :).

    Lionel.
     
    Lionel, May 4, 2006
    #5
  6. Lionel Guest

    A. Sinan Unur wrote:
    > "Dr.Ruud" <> wrote in news:e3bl83.1b4.1
    > @news.isolution.nl:
    >
    >> Lionel schreef:
    >>
    >>> #! /usr/bin/perl

    >> #!/usr/bin/perl
    >>
    >>
    >> Missing:
    >> use strict;
    >> use warnings;
    >>
    >>
    >>> $length = @array;

    >> my $length = @argv;


    Thanks to both replies :).


    > Quick correction:
    >
    > my $length = @ARGV;


    Yes. Although the code I posted here was one of the wrong versions of
    what I was trying, the problem I was having is that I didn't know that
    argv was supposed to be uppercase. Thanks for that.
    >
    > In most cases, however, there is no need to save the length. Using @ARGV
    > in scalar context works fine, and is more transparent:
    >


    > unless ( @ARGV == 2 ) {
    > die <<EO_USAGE;
    > bzzt: Please provide an input and output filename
    >
    > EO_USAGE
    > }


    I was getting to that eventually :).

    On track now . . . thanks. What a stupid question I asked :).

    Lionel.
     
    Lionel, May 4, 2006
    #6
  7. Lionel <> wrote in news:e3bf5g$15jr$1
    @bunyip2.cc.uq.edu.au:

    > wrote:
    >> wrote:
    >>> Hi all,
    >>>
    >>> Just starting to learn perl.

    >>
    >> You should almost always use strict and use warnings.

    >
    > What is strict? I was using warnings but didn't included it on this
    > post.


    perldoc strict

    While you are at it, you should read the posting guidelines as well

    >>> #! /usr/bin/perl
    >>> #
    >>> #
    >>>
    >>> $length = @array;
    >>> print "Length = @argv\n";
    >>> $length = $#array + 1;
    >>> print "Length = $length \n";


    How should I put this? You are not telling the truth. If you had run
    this script with warnings enabled, you would have gotten:

    D:\Home\asu1\UseNet\clpmisc\cafe> perl -Mwarnings t.pl
    Possible unintended interpolation of @argv in string at t.pl line 6.
    Name "main::argv" used only once: possible typo at t.pl line 6.
    Length =
    Length = 0


    At which point, you would have checked

    perldoc perlvar

    to see if there really was a variable called @argv.

    Sinan
    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, May 4, 2006
    #7
  8. Lionel Guest

    A. Sinan Unur wrote:
    > Lionel <> wrote in news:e3bf5g$15jr$1
    > @bunyip2.cc.uq.edu.au:
    >
    >> wrote:
    >>> wrote:
    >>>> Hi all,
    >>>>
    >>>> Just starting to learn perl.
    >>> You should almost always use strict and use warnings.

    >> What is strict? I was using warnings but didn't included it on this
    >> post.

    >
    > perldoc strict


    ***
    DESCRIPTION
    If no import list is supplied, all possible restrictions are
    assumed.
    (This is the safest mode to operate in, but is sometimes too
    strict for
    casual programming.)
    ***

    So leaving it out means it is included anyway? I understand though that
    it is best to include it implicitly.

    > While you are at it, you should read the posting guidelines as well


    I've been posting in usenet for a fair while, what did I do wrong?


    >>>> #! /usr/bin/perl
    >>>> #
    >>>> #
    >>>>
    >>>> $length = @array;
    >>>> print "Length = @argv\n";
    >>>> $length = $#array + 1;
    >>>> print "Length = $length \n";

    >
    > How should I put this? You are not telling the truth. If you had run
    > this script with warnings enabled, you would have gotten:
    >
    > D:\Home\asu1\UseNet\clpmisc\cafe> perl -Mwarnings t.pl
    > Possible unintended interpolation of @argv in string at t.pl line 6.
    > Name "main::argv" used only once: possible typo at t.pl line 6.
    > Length =
    > Length = 0
    >
    >
    > At which point, you would have checked
    >
    > perldoc perlvar
    >
    > to see if there really was a variable called @argv.


    Ok, my bad again. I was using warnings but I didn't use them
    specifically on that script. That was a mistake, I had trimmed down
    script and run it without warnings to just check it did the same thing
    in terms of output and it did. As I said, that is a mistake on my behalf
    for not running it again with warnings.

    I'm just learning of tutorials I find online. I suppose my downfall is
    not taking all the time to go through every detail because I'm fluent in
    a number of programming languages, perl however is a little different.

    Thanks for your help

    Lionel.
     
    Lionel, May 4, 2006
    #8
  9. Guest

    wrote:
    > A. Sinan Unur wrote:
    > > Lionel <> wrote in news:e3bf5g$15jr$1
    > > @bunyip2.cc.uq.edu.au:
    > >
    > >> wrote:
    > >>> wrote:
    > >>>> Hi all,
    > >>>>
    > >>>> Just starting to learn perl.
    > >>> You should almost always use strict and use warnings.
    > >> What is strict? I was using warnings but didn't included it on this
    > >> post.

    > >
    > > perldoc strict

    >
    > ***
    > DESCRIPTION
    > If no import list is supplied, all possible restrictions are
    > assumed.
    > (This is the safest mode to operate in, but is sometimes too
    > strict for
    > casual programming.)
    > ***
    >
    > So leaving it out means it is included anyway?


    No, it means leaving out the *import list* out is the same as including
    the full import list.

    use strict;
    is the same as
    use strict "vars", "refs", "subs";

    Both of them are quit different from not having a "use strict" at all.

    If you used strict, it would have told you that @argv wasn't a
    declared variable or a special variable.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , May 4, 2006
    #9
  10. Lionel Guest

    wrote:
    > No, it means leaving out the *import list* out is the same as including
    > the full import list.
    >
    > use strict;
    > is the same as
    > use strict "vars", "refs", "subs";
    >
    > Both of them are quit different from not having a "use strict" at all.
    >
    > If you used strict, it would have told you that @argv wasn't a
    > declared variable or a special variable.


    Cheers. I've been using "use strict" for about the last hour now and it
    has been very useful. Things are starting to flow :).

    Lionel.
     
    Lionel, May 4, 2006
    #10
    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. Bret

    char **argv vs. char* argv[]

    Bret, Aug 31, 2003, in forum: C Programming
    Replies:
    21
    Views:
    4,708
    Richard Heathfield
    Sep 3, 2003
  2. David
    Replies:
    10
    Views:
    6,079
    Richard Heathfield
    Sep 15, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,716
    Old Wolf
    Jan 20, 2004
  4. =?ISO-8859-1?Q?Thomas_N=FCcker?=

    sys.argv[0] - 'module' object has no attribute 'argv'

    =?ISO-8859-1?Q?Thomas_N=FCcker?=, Jun 30, 2003, in forum: Python
    Replies:
    0
    Views:
    1,002
    =?ISO-8859-1?Q?Thomas_N=FCcker?=
    Jun 30, 2003
  5. jab3

    char **argv & char *argv[]

    jab3, Dec 4, 2004, in forum: C Programming
    Replies:
    5
    Views:
    722
    Chris Torek
    Dec 8, 2004
Loading...

Share This Page