I am having difficulty replacing array elements with hash values using the following script. $string appears 8 times in my output file. I want to replace the first occurance with $header{1} amd the second with $header{2} and so on. Any help is appreciated! Thanks!

use Tk; use strict; use warnings; my $mw=new MainWindow; my $filename; my $string; my @data; my $n; my %header = ("FHT_neg45","FHT_0","FHT_45","FHT_90","OHT_neg45","OHT_0 +","OHT_45","OHT_90"); my $button = $mw->Button(-text=>"temp", -command=> \&button)->pack(); MainLoop; sub button { #Read BJSFM_out.prn into an array @data $filename="BJSFM_out.prn"; open(OUTPUT, "< $filename") or die "Can't find allowables.txt!"; $string="Laminate Strain Allowables"; @data=<OUTPUT>; $n=1; for (@data) { if ($_ eq $string) { s/$_/$header{$n}/; $n++; } } open (FILE, '>temp.txt'); print FILE @data; close FILE; }

In reply to Replace Array Elements with Hash Values by shortyfw06

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.