0

I am trying to use Perl to parse output from a (C-based) program. Every output line is a (1D) Perl array, which I sometimes want to store (based on certain conditions).

I now wish to (deep) copy an array when its first element has a certain keyword, and print that same copied array if another keyword matches in a later line-array.

So far, I have attempted the following:

#!/usr/bin/env perl
use strict; # recommended
use Storable qw(dclone);
...
while(1)   # loop over the lines
    {
        # subsequent calls to tbse_line contain
        # (references to) arrays of data 
        my $la = $population->tbse_line();
        my @copy;
        my $header = shift @$la;

        # break out of the loop:
        last if ($header eq 'fin');

        if($header eq 'keyword')
        {
            @copy = @{ dclone \@$la };
        }

        if($header eq 'other_keyword')
        {
            print "second condition met, print first line:\n"
            print "@copy\n";
        }
    }

However, this prints an empty line to the screen, instead of the contents of the copied array. I don't have a lot of Perl experience, and I can't figure out what I am doing wrong.

Any idea on how to go about this?

1
  • 3
    We don't know what ->tbse_line() returns. Commented Nov 19, 2018 at 17:46

1 Answer 1

5

my @copy allocates a new Perl array named @copy in the current scope. It looks like you want to set @copy during one iteration of your while loop and print it in a different iteration. In order for your array not to be erased each time a new while loop iteration starts, you should move the my @copy declaration outside of the loop.

my @copy;
while (1) { ... }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that fixed the issue! The code above is just a small part of a very large code that I did not write myself, but am trying to adapt to my current processing needs. I must have overlooked the positioning of the array declaration.
You should accept the answer if it's solved your problem :)

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.