Dear Monks,

I would like to avoid overwriting elements in my array. Could you give me some advice ? The value are overwritten because I sort the data into several categories and some elements belong to the same catogory. Thanks

Here is my code (checked)

#! usr/bin/perl -w use strict; use File::Copy; my $summary_excel_file = "all_weeks_classified_per_hour.csv"; open my $OUTFILE, q{>}, $summary_excel_file; print $OUTFILE "hour_position;before H-24;"; my @list = (1..24); @list = reverse @list; my $i; foreach $i(@list){ my $j = $i - 1; print $OUTFILE "between H-$i and H-$j;"; } @list = (1..24); foreach $i(@list){ my $j = $i - 1; print $OUTFILE "between H+$j and H+$i;"; } print $OUTFILE "\n"; print $OUTFILE "legend;"; my @Elements; my @TXT = glob("*.txt"); print STDOUT "@TXT\n"; foreach my $txt (@TXT){ open my $TXT, q{<}, $txt or die; while(<$TXT>){ my ($reg, $value, $anticipation) = (split /;/)[0,1,5]; my $relative_position = -$anticipation; if ($relative_position <= -1440){ $Elements[0] = $value; } foreach my $k (-24..23){ ### ] inf_limit ____________ pos in time ______________ +sup_limit____________> time my $inf_limit = 60*$k; my $sup_limit = 60*($k+1); my $l= $k + 25; if (($relative_position > $inf_limit) && ($relative_positi +on <= $sup_limit)){ $Elements[$l] = $anticipation; } } if ($relative_position > 1440 ){ $Elements[49] = $value; } } } my $nb = @Elements; foreach my $i(0..$nb){ if (defined $Elements[$i]){ $Elements[$i] =~ s/\s+//; print $OUTFILE "$Elements[$i];"; } else{ print $OUTFILE ";"; } } close $OUTFILE;
and here are the "*.txt" files
EGLLA22;0.1;380;01;22/10/2006;100
EGLLA22;0.1;380;01;22/10/2006;90
EGLLA22;0.1;380;01;22/10/2006;110

In reply to adding elements to already exisiting elements in an array by steph_bow

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.