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

This code doens't work, what could be the issue here.
#!/bin/env perl open(SYSLOG, "/tmp/log100lines") or die "Can't open file: $!\n"; foreach $line (<SYSLOG>) { chomp $line; @string = join(',', split(/ /, $line)); print "$string[4]\n"; }

Replies are listed 'Best First'.
Re: reading the 5th element from file doesn't work.
by 2teez (Vicar) on Apr 24, 2013 at 05:38 UTC

    Hi singho,
    There are several things wrong here.
    1. According to your post title, do you what the 5th Element of the whole file, or each line in the file?
    If your intention is the 5th Element of the whole file, then do this:

    my @string; open(SYSLOG, "/tmp/log100lines") or die "Can't open file: $!\n"; foreach my $line (<SYSLOG>) { chomp $line; push @string, $line } print $string[4],$/;
    2. But it seem, to me you want the 5th element of each line, which in that case you need do this:
    open(SYSLOG, "/tmp/log100lines") or die "Can't open file: $!\n"; foreach my $line (<SYSLOG>) { chomp $line; my @string = split(/ /, $line); ## remove the function join print "$string[4]\n"; }
    NOTE:
    a.) The scope of the array variable "@string" in the both cases,
    b.) In the second script, you don't need the function join, because it Joins the separate strings of LIST into a single string with fields separated by the value of EXPR which in your case is ',', so your 5th element of each line has an uninitialized value.

    That been said, there are a number of things, I think one should take note:
    1. use these pragma: warnings and strict
    2. use lexically scoped file-handles and use 3 -argument open function like
      open my $fh, '<', "file_to_open" or die "can't open file: $!";
    3. use a while loop, to walk through a file, step-wise like:
      while(<$fh>){ ... }
    4. close all opened file handles
    5. Please, also check your shebang line

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
      Thanks a lot for your help a explantion, i ended up using join, since the above with just split failed eariler, may be i missed something before. I tried using the above code it worked neatly. Thanks a lot.
Re: reading the 5th element from file doesn't work.
by hdb (Monsignor) on Apr 24, 2013 at 05:15 UTC

    After splitting your $line into pieces you are joining it back into one, so there is no 5th element (sad, Leeloo, isn't it?). If you remove the join bit, it should work assuming that your line has 5 elements...

Re: reading the 5th element from file doesn't work.
by AnomalousMonk (Archbishop) on Apr 24, 2013 at 18:49 UTC
    ... what could be the issue here.

    Allow Perl to suggest what the issue might be by using the warnings pragma, which would have warned you that  $string[4] was undefined (and if you're a beginner, also diagnostics for more verbose suggestions). In addition, I would strongly suggest using the strict pragma to disallow certain practices that experience has shown to be problematic, e.g., use of variables that have not been pre-declared. So:
        use strict;
        use warnings;
        use diagnostics;  # when you are desperate