sas429s has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a file which looks like this:
maps_c7 190_520 3195927 00 40 0K8632 maps_c7 210_520 3195928 00 41 0K8701 maps_c7 210_620 3195929 00 42 0K8702 maps_c7 230_560 3195930 00 43 0K8635 maps_c7 230_620 3195931 00 44 0K8703 maps_c7 230_660 3195932 00 45 0K8704 maps_c7 250_660 3195933 00 46 0K8638 maps_c7 250_800 3195934 00 47 0K8705 maps_c7 275_800 3195935 00 48 0K8640 maps_c7 300_860 3195936 00 49 0K8706 maps_c7 300_860_ER 3195937 00 50 0K8642 maps_c7 330_860_ER 3195938 00 51 0K8643 maps_c7 350_860_ER 3195939 00 52 0K8707 maps_c7 300_860_RV 3195940 00 53 0K8645 maps_c7 330_860_RV 3195941 00 54 0K8646 maps_c7 350_860_RV 3195942 00 55 0K8647 maps_c7 360_925_RV 3195943 00 56 0K9048
My program has a problem..Why Can't I loop through each line of the file, I get to print only the first line. It might sound silly asking this question but whats problem with my foreach loop?
#!/usr/bin/perl use strict; use warnings; use Tie::File; use Fcntl; $ENV{HOME}='C:\Documents and Settings\kompeS\Desktop\PERL SCRIPTS'; my $file= "$ENV{HOME}/partno_C7_5.03.4_prod"; my @array; my $line=""; my @data; my $map_dir=""; my $dir=""; my $part_no=""; my $chg_lvl=""; my $int_lock=""; my $t_spec=""; my @list=""; my $formatted_list=""; open (FILE,"$file"); close(FILE); tie (@data,'Tie::File',$file, mode=>O_RDWR) or die "Can't tie to $file +:$^E\n"; (tied @data)->defer; foreach (@data) { ($map_dir,$dir,$part_no,$chg_lvl,$int_lock,$t_spec) = split /\s+/; print "@list"; print "$formatted_list"; print " $map_dir,$dir,$part_no,$chg_lvl,$int_lock,$t_spec"; } (tied @data)->flush;
Thanks

Replies are listed 'Best First'.
Re: Problem with foreach loop
by ikegami (Patriarch) on Jan 25, 2008 at 16:06 UTC

    I can't duplicate your problem.

    You're not changing @data, so (tied @data)->defer; and (tied @data)->flush; are useless. So is Tie::File, for that matter.

    #!/usr/bin/perl use strict; use warnings; $ENV{HOME} = 'C:\Documents and Settings\kompeS\Desktop\PERL SCRIPTS'; my $file = "$ENV{HOME}/partno_C7_5.03.4_prod"; open(my $fh, '<', $file) or die("Unable to open input file \"$file\": $!\n"); while (<$fh>) { my ($map_dir,$dir,$part_no,$chg_lvl,$int_lock,$t_spec) = split /\s ++/; print " $map_dir,$dir,$part_no,$chg_lvl,$int_lock,$t_spec"; }
Re: Problem with foreach loop
by wfsp (Abbot) on Jan 25, 2008 at 16:11 UTC
    There are a few problems with your code. What was wrong with the advice you've already been given?
      Hi all, I made those changes here...actually copied from the old file and made changes to it. I did follow your advice and changed it but didn't upload the file with those changes.:) Ok..I struggled with it and found that if i read the file as an array as shown below in the code I was able to read each line in the file but not with Tie::File. Can some one tell me the reason?
      #!/usr/bin/perl use strict; use warnings; use Tie::File; use Fcntl; #Variable Declaration my @array; my $line=""; my @data; my $map_dir=""; my $dir=""; my $part_no=""; my $chg_lvl=""; my $int_lock=""; my $t_spec=""; my @list=""; my $formatted_list=""; my $i; #Program Main if ($#ARGV == -1) { print "\n"; print "Enter a file containing part number, <R> to Quit: "; chomp($file_name=<>); } else { $file_name=$ARGV[1]; } open (FILE,"$file_name"); ########## These two lines are commented out ################### #tie (@data,'Tie::File',$file, mode=>O_RDWR) or die "Can't tie to $fil +e:$^E\n"; #(tied @data)->defer; ############## ##I am reading the file as an array here ################ @data=<FILE>; foreach (@data) { ($map_dir,$dir,$part_no,$chg_lvl,$int_lock,$t_spec) = split /\s+/; print "@list"; print "$formatted_list"; print " $map_dir,$dir,$part_no,$chg_lvl,$int_lock,$t_spec"; } #(tied @data)->flush;
      So is there a problem using Tie::File? Thanks
        Since you're just beginning with perl, it might be better to forget about Tie::File for now and concentrate on the basics. Don't try to run before you can walk!

        ...roboticus