shortyfw06 has asked for the wisdom of the Perl Monks concerning the following question:
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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Replace Array Elements with Hash Values
by toolic (Bishop) on Jun 13, 2012 at 16:48 UTC | |
|
Re: Replace Array Elements with Hash Values
by stevieb (Canon) on Jun 13, 2012 at 16:58 UTC | |
by morgon (Priest) on Jun 13, 2012 at 17:07 UTC | |
by Cristoforo (Curate) on Jun 13, 2012 at 18:19 UTC | |
by shortyfw06 (Beadle) on Jun 13, 2012 at 18:36 UTC | |
by shortyfw06 (Beadle) on Jun 13, 2012 at 18:13 UTC |