in reply to Replace Array Elements with Hash Values

Good morning,

From what I can tell, you need an array, not a hash:

#!/usr/bin/perl use Tk; use strict; use warnings; my $mw=new MainWindow; 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 my $filename="BJSFM_out.prn"; open my $fh, '<', $filename or die "Can't find allowables.txt! $!" +; my $string="Laminate Strain Allowables"; my @data=<$fh>; my $n = 0; for (@data) { chomp; if ($_ eq $string) { $data[$n] = $header[$n]; $n++; } } open my $write_fh, '<', 'temp.txt' or die $!; print $write_fh @data; close $write_fh; }

I also made a few other changes... three-arg form of open(), lexically scoped most of your vars within the sub (instead of having them global), and changed from using a bareword file handle to a scalar one.

Also, if you have a more current version of Perl, you can change that for loop to using while/each on the array:

while ( ( my $iter, $elem ) = each @data ){ if ( $elem eq $string ){ $data[ $iter ] = $header[ $iter ]; } }

Update: added in chomp per toolic's point in his above post. I can't test this code as I don't have Tk installed. I've also included the file open check that I missed, as noted by morgon below.

Replies are listed 'Best First'.
Re^2: Replace Array Elements with Hash Values
by morgon (Priest) on Jun 13, 2012 at 17:07 UTC
    Two further improvements:

    First, toolic is right about the need for chomping, so

    my @data = map { chomp; $_ } <$fh>;
    and of course check the open:
    open my $write_fh, '<', 'temp.txt' or die $!;
      Just an observation. List::MoreUtils has a function apply which does the chomp and passes the modified, (line less the newline), line to @data

      my @data= apply {chomp} <$fh>;.

      Chris

      Update: Or more simply, chomp(my @data = <$fh>);

        I figured it out. Thank you for all of the help!

        use Tk; use strict; use warnings; my $mw=new MainWindow; 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 my $filename="BJSFM_out.prn"; open my $fh, '<', $filename or die "Can't find $filename!"; my $string="1DO YOU WANT INSTRUCTIONS?"; my @data= map {chomp; $_} <$fh>; my $n=0; foreach my $line (@data) { if ($line eq $string) { $line = $header[$n]; $n++; } } open my $write_fh, '>', 'temp.txt' or die $!; print $write_fh join "\n",@data; close $write_fh; }

      I tried to run the following and it replaces $string with %header rather than $string with $header[0], $header1, etc. Also, I thought my print command would add the newlines but it doesn't.

      use Tk; use strict; use warnings; my $mw=new MainWindow; 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 my $filename="BJSFM_out.prn"; open my $fh, '<', $filename or die "Can't find $filename!"; my $string="1DO YOU WANT INSTRUCTIONS?"; my @data= map {chomp; $_ } <$fh>; my $n=0; for (@data) { if ($_ eq $string) { $data[$n] = $header[$n]; $n++; } } open my $write_fh, '>', 'temp.txt' or die $!; print $write_fh join " ",@data,"\n"; close $write_fh; }