Changing an array in Perl












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 "@copyn";
}
}


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?










share|improve this question


















  • 3





    We don't know what ->tbse_line() returns.

    – choroba
    Nov 19 '18 at 17:46
















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 "@copyn";
}
}


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?










share|improve this question


















  • 3





    We don't know what ->tbse_line() returns.

    – choroba
    Nov 19 '18 at 17:46














0












0








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 "@copyn";
}
}


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?










share|improve this question














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 "@copyn";
}
}


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?







arrays perl parsing






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 17:43









Final_SunFinal_Sun

32




32








  • 3





    We don't know what ->tbse_line() returns.

    – choroba
    Nov 19 '18 at 17:46














  • 3





    We don't know what ->tbse_line() returns.

    – choroba
    Nov 19 '18 at 17:46








3




3





We don't know what ->tbse_line() returns.

– choroba
Nov 19 '18 at 17:46





We don't know what ->tbse_line() returns.

– choroba
Nov 19 '18 at 17:46












1 Answer
1






active

oldest

votes


















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) { ... }





share|improve this answer
























  • 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.

    – Final_Sun
    Nov 19 '18 at 17:52











  • You should accept the answer if it's solved your problem :)

    – Grinnz
    Nov 19 '18 at 18:18











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380028%2fchanging-an-array-in-perl%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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) { ... }





share|improve this answer
























  • 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.

    – Final_Sun
    Nov 19 '18 at 17:52











  • You should accept the answer if it's solved your problem :)

    – Grinnz
    Nov 19 '18 at 18:18
















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) { ... }





share|improve this answer
























  • 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.

    – Final_Sun
    Nov 19 '18 at 17:52











  • You should accept the answer if it's solved your problem :)

    – Grinnz
    Nov 19 '18 at 18:18














5












5








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) { ... }





share|improve this answer













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) { ... }






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 17:48









mobmob

97.2k14130251




97.2k14130251













  • 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.

    – Final_Sun
    Nov 19 '18 at 17:52











  • You should accept the answer if it's solved your problem :)

    – Grinnz
    Nov 19 '18 at 18:18



















  • 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.

    – Final_Sun
    Nov 19 '18 at 17:52











  • You should accept the answer if it's solved your problem :)

    – Grinnz
    Nov 19 '18 at 18:18

















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.

– Final_Sun
Nov 19 '18 at 17:52





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.

– Final_Sun
Nov 19 '18 at 17:52













You should accept the answer if it's solved your problem :)

– Grinnz
Nov 19 '18 at 18:18





You should accept the answer if it's solved your problem :)

– Grinnz
Nov 19 '18 at 18:18


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380028%2fchanging-an-array-in-perl%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?