> -----Original Message----- > From: Chris Stinemetz [mailto:chrisstinem...@gmail.com] > Sent: Thursday, December 15, 2011 10:47 AM > To: John W. Krahn > Cc: Perl Beginners > Subject: Re: split function > > I'm getting a bit closer. There a couple roadblocks I am up against. > > I am able to split the lines by white space, but for some reason the > program isn't capturing the first lines to the @fieldValue array after > the @headerNames array. > Once I get all the lines to go into the array correctly I would like to > combine the @headerNames and @fieldValue arrays. The way I am doing it > now only appends the later. > I would like the combination to be the below for each elements in the > two arrays. > > any help is greatly appreciated, > > Chris > > csno=1 > rfpi=1 > header_1=5.5 > header_2=5.5 > header_3=5.5 > header_4=5.5 > header_5=5.5 > header_6=5.5 > header_7=5.5 > header_8=5.5 > header_9=5.5 > I have not been following this too closely, but I don't understand the algorithm used to get the above output. > > > #!/usr/bin/perl > use warnings; > use strict; > use Data::Dumper; > > my $header; > my @headerNames; > my $field; > my @fieldValue; > my @apxScript; > > while (my $line = <DATA>) { > if($line =~ m|(.*_.*\n)|){ > $header = $1; > @headerNames = split(" ",$header); > } >
Why not just have an else statement instead of the 'if'? > if($line !~ m|.*_.*\n|){ > @fieldValue = split(" ",$line); > print "$fieldValue[0]\n"; > } > } > Not sure what you are trying to do, but each time through the loop above you are reassigning @fieldValue (I would have named it @fieldValues since arrays usually hold multiple values). Therefore, when you use @fieldValue below, it only contains data from the last line of input. > my @apxScript=(@headerNames, @fieldValue); print Dumper \@headerNames; > print Dumper \@fieldValue; print Dumper \@apxScript; > > > __DATA__ > csno rfpi header_1 header_2 header_3 header_4 header_5 header_6 > header_7 header_8 header_9 > 1 1 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 > 2 2 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 > 3 3 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 > 4 1 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 > 5 2 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 > 6 3 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 > 7 1 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 > 8 2 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 > 9 3 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 > 10 1 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 > 11 2 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 > 12 3 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 > HTH, Ken -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/