Can't get past 'use strict' :(

Discussion in 'Perl Misc' started by Karlo Lozovina, Dec 15, 2004.

  1. Here I go again: the following code:

    while(my $line = <DIRLIST>) {
    my $dir_list[$i] = $line;
    chomp($dir_list[$i]);
    $i++;
    }

    produces this error:

    syntax error at ./backup.pl line 24, near "$dir_list["

    when i use "use strict". I read Programming Perl's chapter on strict,
    but I can't get this thing working :(.

    --
    _______ Karlo Lozovina - Mosor
    | | |.-----.-----.
    | || _ | _ | Na osami blizu mora, dok se sunce zemlji smije
    |__|_|__||_____|_____| Balun gledat, za njin letit...
     
    Karlo Lozovina, Dec 15, 2004
    #1
    1. Advertising

  2. Op Wed, 15 Dec 2004 16:37:05 +0000, schreef Karlo Lozovina:
    > Here I go again: the following code:
    >
    > while(my $line = <DIRLIST>) {
    > my $dir_list[$i] = $line;
    > chomp($dir_list[$i]);
    > $i++;
    > }
    >
    > produces this error:
    >
    > syntax error at ./backup.pl line 24, near "$dir_list["
    >


    I'm not sure where the problem lies, but this can definitely be done more
    efficient. Try:

    chomp(my @line = <DIRLIST>);


    -leendert bottelberghs
     
    Leendert Bottelberghs, Dec 15, 2004
    #2
    1. Advertising

  3. Karlo Lozovina wrote:

    > while(my $line = <DIRLIST>) {
    > my $dir_list[$i] = $line;

    ^^ ^ ^ ^
    > chomp($dir_list[$i]);
    > $i++;
    > }


    Besides Leendert's much better hint:
    I think you meant to define @dir_list out of this loop and just push new
    elements to the array. You don't have to declare every element of that ary.
    How about this (untested):

    my @dir_list;
    while(my $line = <DIRLIST>) {
    chomp $line;
    push @dir_list, $line;
    }
    my $i = @dir_list # if needed, but i don't think so..

    g,
    Robert

    --
    http://www.dunkelheit.at/
    thou shallst fear...
     
    Robert Sedlacek, Dec 15, 2004
    #3
  4. "Karlo Lozovina" <_karlo_@_mosor.net_> wrote in message
    news:Xns95C0B356F954Bkarlomosornet@130.133.1.4...
    > Here I go again: the following code:
    >
    > while(my $line = <DIRLIST>) {
    > my $dir_list[$i] = $line;

    ^^^^^^^^^^^ this is an array.... declare it above


    > chomp($dir_list[$i]);
    > $i++;
    > }
    >


    use strict;

    my(@dir_list, $i);

    while(my $line = <DIRLIST>) {
    $dir_list[$i] = $line;
    # Removed my();
    chomp($dir_list[$i]);
    $i++;
    }

    Richard Zilavec
     
    Richard Zilavec, Dec 15, 2004
    #4
  5. Karlo Lozovina wrote:

    > Here I go again: the following code:
    >
    > while(my $line = <DIRLIST>) {
    > my $dir_list[$i] = $line;
    > chomp($dir_list[$i]);
    > $i++;
    > }
    >
    > produces this error:
    >
    > syntax error at ./backup.pl line 24, near "$dir_list["
    >
    > when i use "use strict". I read Programming Perl's chapter on strict,
    > but I can't get this thing working :(.
    >

    You can't declare an array variable like that. Nor can I see
    *why* you'd want to declare an array variable like that. You
    can get it to compile like this:

    while (my $line = <DIRLIST>) {
    my @dir_list;
    $dir_list[$i] = $line;
    chomp($dir_list[$i]);
    $i++;
    }

    This makes @dir_list disappear as soon as your while loop
    ends. Is this what you want? If you want @dir_list to
    be visible outside the list, then you want this:

    my @dir_list;
    while (my $line = <DIRLIST>) {
    $dir_list[$i] = $line;
    chomp($dir_list[$i]);
    $i++;
    }

    --
    Christopher Mattern

    "Which one you figure tracked us?"
    "The ugly one, sir."
    "...Could you be more specific?"
     
    Chris Mattern, Dec 15, 2004
    #5
  6. Leendert Bottelberghs wrote:

    > Op Wed, 15 Dec 2004 16:37:05 +0000, schreef Karlo Lozovina:
    >> Here I go again: the following code:
    >>
    >> while(my $line = <DIRLIST>) {
    >> my $dir_list[$i] = $line;
    >> chomp($dir_list[$i]);
    >> $i++;
    >> }
    >>
    >> produces this error:
    >>
    >> syntax error at ./backup.pl line 24, near "$dir_list["
    >>

    >
    > I'm not sure where the problem lies, but this can definitely be done more
    > efficient. Try:
    >
    > chomp(my @line = <DIRLIST>);
    >

    Well, yes, that's the best way, but let's go one step at a time...

    --
    Christopher Mattern

    "Which one you figure tracked us?"
    "The ugly one, sir."
    "...Could you be more specific?"
     
    Chris Mattern, Dec 15, 2004
    #6
  7. Chris Mattern wrote:
    > You can get it to compile like this:
    >
    > while (my $line = <DIRLIST>) {
    > my @dir_list;
    > $dir_list[$i] = $line;
    > chomp($dir_list[$i]);
    > $i++;
    > }
    >
    > This makes @dir_list disappear as soon as your while loop
    > ends.


    Not only does it go out of scope, but it also contains only the last
    line before doing so, since my() clears the array at each iteration.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Dec 15, 2004
    #7
  8. Chris Mattern <> wrote in
    news::

    > my @dir_list;
    > while (my $line = <DIRLIST>) {
    > $dir_list[$i] = $line;
    > chomp($dir_list[$i]);
    > $i++;
    > }


    Yup, this is what I needed. Thank you very much for clearing this out.
    I had one day to write a rather big Perl program today, but I never
    programmed in Perl before. Althoug Perl is quite easy to catch up,
    after a days work it is easy to mix things up :).

    --
    _______ Karlo Lozovina - Mosor
    | | |.-----.-----.
    | || _ | _ | Na osami blizu mora, dok se sunce zemlji smije
    |__|_|__||_____|_____| Balun gledat, za njin letit...
     
    Karlo Lozovina, Dec 15, 2004
    #8
  9. Karlo Lozovina

    Robin Guest

    "Karlo Lozovina" <_karlo_@_mosor.net_> wrote in message
    news:Xns95C0B356F954Bkarlomosornet@130.133.1.4...
    > Here I go again: the following code:
    >
    > while(my $line = <DIRLIST>) {
    > my $dir_list[$i] = $line;
    > chomp($dir_list[$i]);
    > $i++;
    > }
    >
    > produces this error:
    >
    > syntax error at ./backup.pl line 24, near "$dir_list["
    >
    > when i use "use strict". I read Programming Perl's chapter on strict,
    > but I can't get this thing working :(.
    >
    > --
    > _______ Karlo Lozovina - Mosor
    > | | |.-----.-----.
    > | || _ | _ | Na osami blizu mora, dok se sunce zemlji smije
    > |__|_|__||_____|_____| Balun gledat, za njin letit...


    actually, your code looks pretty tight.
    -Robin
     
    Robin, Dec 16, 2004
    #9
  10. Karlo Lozovina

    Peter Wyzl Guest

    "Karlo Lozovina" <_karlo_@_mosor.net_> wrote in message
    news:Xns95C0B356F954Bkarlomosornet@130.133.1.4...
    : Here I go again: the following code:
    :
    : while(my $line = <DIRLIST>) {
    : my $dir_list[$i] = $line;
    : chomp($dir_list[$i]);
    : $i++;
    : }

    It won't compile even without 'use strict'. You can't declare a slice of an
    array like that, you need to declare the whole array and then assign to the
    slices. However, a better, or more Perlish way to do that would be to take
    advantage of some of the majic offered by the <> operator in array context
    and read the whole file into the array in one step.

    my @dir_list = <DIRLIST>;

    You can then chomp() the array without having to chomp every element as you
    read it...

    chomp @dir_list;

    If you find after doing the above that you still need to have $i (if you are
    using it for something other than knowing which array element to assign the
    line to and chomp, then assigning an array in scalar contect is the number
    of elements in the array.

    my $i = @dir_list;

    Which reduces the above to:->

    my @dir_list = <DIRLIST>;
    chomp @dir_list;
    my $i = @dir_list; # may not be needed..

    --
    Wyzelli
     
    Peter Wyzl, Dec 16, 2004
    #10
  11. "Peter Wyzl" <> wrote in
    news:ayewd.74895$:

    > It won't compile even without 'use strict'. You can't declare a
    > slice of an array like that, you need to declare the whole array
    > and then assign to the slices.


    I know - looking at that code day after, and I seem like an idiot
    :).

    > However, a better, or more Perlish way to do that would be to take
    > advantage of some of the majic offered by the <> operator in array
    > context and read the whole file into the array in one step.
    > my @dir_list = <DIRLIST>;


    Wow! Didn't know Perl can do that. It rocks, thanks.

    > You can then chomp() the array without having to chomp every
    > element as you read it...
    > chomp @dir_list;


    Didn't now I can do that, either :). Guess I'll have to finish
    reading Programming Perl before beginning another Perl project.

    Anyway, thanks a lot Peter, you've been most helpfull.

    --
    Karlo Lozovina - Mosor
     
    Karlo Lozovina, Dec 16, 2004
    #11
  12. Robin <> wrote:
    > "Karlo Lozovina" <_karlo_@_mosor.net_> wrote in message
    > news:Xns95C0B356F954Bkarlomosornet@130.133.1.4...



    >> while(my $line = <DIRLIST>) {
    >> my $dir_list[$i] = $line;
    >> chomp($dir_list[$i]);
    >> $i++;
    >> }



    [ snip quoted .sig, you aren't supposed to do that you know ]


    > actually, your code looks pretty tight.



    Nonsense alert!!


    Doing explicit indexing when explicit indexing is not required
    by the algorithm is "error prone" rather than "tight".

    chomp(@dir_list = <DIRLIST>);

    Does the same thing, and seems much closer to what a reasonable
    person might call "tight".


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Dec 16, 2004
    #12
  13. "Robin" <webmaster @ infusedlight.net> wrote in news:WY6dne9XT_dMyFzcRVn-
    :

    >
    > "Karlo Lozovina" <_karlo_@_mosor.net_> wrote in message
    > news:Xns95C0B356F954Bkarlomosornet@130.133.1.4...


    >> while(my $line = <DIRLIST>) {
    >> my $dir_list[$i] = $line;
    >> chomp($dir_list[$i]);


    ....

    >> syntax error at ./backup.pl line 24, near "$dir_list["


    >> --
    >> _______ Karlo Lozovina - Mosor
    >> | | |.-----.-----.
    >> | || _ | _ | Na osami blizu mora, dok se sunce zemlji smije
    >> |__|_|__||_____|_____| Balun gledat, za njin letit...

    >
    > actually, your code looks pretty tight.
    > -Robin


    For various values of tight.

    How can something that is reported by perl to be a syntax error be
    considered "tight".

    Oh, by the way, please take a look at the posting guidelines and definitely
    do not quote people's signatures.

    Sinan.
     
    A. Sinan Unur, Dec 16, 2004
    #13
  14. "Peter Wyzl" <> wrote in
    news:ayewd.74895$:

    > "Karlo Lozovina" <_karlo_@_mosor.net_> wrote in message
    > news:Xns95C0B356F954Bkarlomosornet@130.133.1.4...
    >: Here I go again: the following code:
    >:
    >: while(my $line = <DIRLIST>) {
    >: my $dir_list[$i] = $line;
    >: chomp($dir_list[$i]);
    >: $i++;
    >: }
    >
    > It won't compile even without 'use strict'. You can't declare a slice
    > of an array like that, you need to declare the whole array and then
    > assign to the slices.


    Minor correction: $dir_list[$i] is not a slice but an array element.

    Sinan
     
    A. Sinan Unur, Dec 16, 2004
    #14
  15. Karlo Lozovina

    frans abels Guest

    On Thu, 16 Dec 2004 10:31:12 +0100, Robin wrote:


    > "Karlo Lozovina" <_karlo_@_mosor.net_> wrote in message
    > news:Xns95C0B356F954Bkarlomosornet@130.133.1.4...
    >> Here I go again: the following code:
    >>
    >> while(my $line = <DIRLIST>) {
    >> my $dir_list[$i] = $line;
    >> chomp($dir_list[$i]);
    >> $i++;
    >> }
    >>
    >> produces this error:
    >>
    >> syntax error at ./backup.pl line 24, near "$dir_list["
    >>


    > actually, your code looks pretty tight. -Robin


    I don't know what that means,
    but it has a syntax error...
    my is not allowed with array-element assignments.
    If you want to code this way and not the most efficient way
    it should be:

    my @dir_list;
    while(my $line = <DIRLIST>) {
    $dir_list[$i] = $line;
    chomp($dir_list[$i]);
    $i++;
    }
     
    frans abels, Dec 16, 2004
    #15
  16. A. Sinan Unur wrote:

    > "Robin" <webmaster @ infusedlight.net> wrote in news:WY6dne9XT_dMyFzcRVn-
    > :
    >>
    >>actually, your code looks pretty tight.

    >
    > For various values of tight.


    The only one I think he means is is meaning #6 as defined in
    Merriam-Webster Online dictionary. :)
     
    Brian McCauley, Dec 16, 2004
    #16
    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. Lloyd Sheen
    Replies:
    13
    Views:
    465
    Hermit Dave
    Jan 25, 2004
  2. knutivar
    Replies:
    2
    Views:
    341
    bruce barker
    Dec 9, 2004
  3. Replies:
    2
    Views:
    1,688
    Steve C. Orr [MVP, MCSD]
    Mar 29, 2006
  4. Replies:
    5
    Views:
    678
  5. Ting Wang
    Replies:
    5
    Views:
    188
    John Bokma
    Oct 6, 2005
Loading...

Share This Page