2

How could i assign a specific array value into $skip? I would start to read from a.txt or b.txt from a specific line (88 for a.txt and 64 for b.txt)

#!/usr/bin/perl
# Libraries
use strict;
use warnings;

# Main script
my @filename = ('a.txt', 'b.txt');
my @nrows = ('88', '64');

foreach my $file_input(glob("*.txt")) {
    open my $fh, '<', $file_input or die "can't read open $IN_FILE";
    for my $i (0 .. $#nrows) {
        if ( $file_input eq $filename[$i] ) {
            my $skip = $nrows[$i];
        }
    }
    $/ = "\n\n";  # record separator
    while( <$fh> ) {
    next unless '$skip' .. undef;
    my @lines = split /\n\n/;
    **... some manipulations ...**
}
close ($fh);
}

I Receive following error:

Use of uninitialized value $skip in concatenation (.) or string at ./exercise.n24.pl line 14, <$fh> chunk 11.

I've made a lot of test in last 4 hours, and I don't understand where I'm wrong

1
  • 2
    In addition to what Dave Cross has noted, $skip will start at the 88th 'paragraph', not the 88th line. @lines should probably be split into with my @lines = split /\n/ (only 1 newline). Commented Jul 31, 2018 at 18:24

2 Answers 2

3

I can see a couple of obvious errors here.

You declare $skip in a block that immediately ends.

if ( $file_input eq $filename[$i] ) {
    my $skip = $nrows[$i];
}

So you can never see the value of $skip.

Then, when you're trying to access $skip, you put it in single quotes. And variables don't expand in single quotes, so Perl just sees it as the five characters $, s, k, i and p.

But I don't think either of those explain the error you're seeing. Which line in your sample code is line 14.

It's far more useful to us if you give us a code sample that we can run.

I'd suggest an alternative approach, but I'm afraid it's really not clear what you're trying to do.

Sign up to request clarification or add additional context in comments.

6 Comments

Line 14 isn't in that code: there's no "concatenation (.) or string" involving $skip anywhere there.
@Borodin: My guess is that this isn't the real code and the real code has next unless "$skip" .. undef as line 14. Just a guess though.
Yes, in the head of script I have #!/usr/bin/perl and more comment, so, $skip declaration is at 14th row
@ThomasAnowez: And you didn't think that information would be useful to us in your original question?
I've exclude only the comments about exercise :(
|
1

The error you're getting is because in your code, you're trying to use $skip outside the scope it was declared in.

But on a broader level, It seems like you just want to skip a certain number of lines depending on the filename. You should use a hash for that instead of parallel arrays.

use strict;    

my %lines_to_skip = (
    'a.txt' => 88,
    'b.txt' => 64
);

for my $file (glob("*.txt")) {
    my $skip = $lines_to_skip{$file};
    open my $fh, '<', $file;
    # local $/ = "\n\n"; # note that this would read the file in paragraph mode
    while (<$fh>) {
         next unless $. > $skip;
         # do something
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.