I have a file with some navigation (x and y's) and needed to fill in point between exiting points.
My in put file: 600000 6174210 600017 6174213 600028 6174206 600035 6174216 600045 6174209 For calculation I need to use line 1 and 2 for calculating one point and then line 2 and 3 and so on ... I solved it by using Xaviers tip on Tie::File: use Tie::File; tie my @line, 'Tie::File', $ARGV[0] or die "could not open $ARGV[0]: $!"; my $cnt1 = 0; my $cnt2 = 0; while ( $cnt1 < $ARGV[1] ) { $cnt2++; #print " $cnt1 $cnt2\n"; my @rec1 = split(' ', $line[$cnt1]); my @rec2 = split(' ', $line[$cnt2]); my $newin = (($rec1[0] - $rec2[0]) / 2) + $rec2[0]; my $newxl = (($rec1[1] - $rec2[1]) / 2) + $rec2[1]; print "@rec1\n"; print "$newin $newxl\n"; $cnt1 = $cnt2; } where $ARGV[1] is the number of lines in the file. Could I have used an array as you mentioned in the bottom of your comment? Thanks for the comment on "||" and "or" I was not aware of that. On 7/15/05, John W. Krahn <[EMAIL PROTECTED]> wrote: > Jakob Kofoed wrote: > > Hi, > > Hello, > > > I would like to load a file to a hash so I will be able to call a > > specific number in that hash: > > > > use strict; > > use warnings; > > > > my %hash = (); > > open IN, "< $ARGV[0]" || die "could not open $ARGV[0]\n"; > > That will never die() because of the high precedence of the || operator. > You need to either use open() with parentheses or use the lower > precedence 'or' operator. You should also include the $! variable in > the error message so you know why it died. > > open IN, "< $ARGV[0]" or die "could not open $ARGV[0]: $!"; > > > > my $cnt = "0"; > > If you are going to use $cnt numerically why are you assigning a string > to it? Why not just use the built-in $. variable? > > > > while (<IN>) { > > chomp; > > %hash = ( $cnt => $_ ); > > You are assigning a list to %hash which overwrites the hash each time > through the loop. What you want to do is assign the value to a specific > key. > > $hash{ $cnt } = $_; > > But if all of your keys are sequential numbers then you should just use > an array instead: > > push @array, $_; > > > > $cnt++; > > } > > print $hash{"837"}; > > > > This works fine with the last line only! how do I append to my hash? > > and secondly: here I use a counter to get a specific "line" in the > > hash - does a hash have a "build in" counter to refer to? > > > John > -- > use Perl; > program > fulfillment > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > <http://learn.perl.org/> <http://learn.perl.org/first-response> > > > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>