> -----Original Message-----
> From: Ken Slater [mailto:kl...@psu.edu]
> Sent: Thursday, December 15, 2011 11:09 AM
> To: 'Chris Stinemetz'; 'John W. Krahn'
> Cc: 'Perl Beginners'
> Subject: RE: split function
> 
> > -----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)|){

The above line could be more easily written as 
If ($line =~ m/_/) {
based on the fact that underscores apparently only appear in headers.

> >   $header = $1;

Above line is unecessary


> >   @headerNames = split(" ",$header);

@headerNames = split(" ",$line);

> >   }
> >
> 
> 
> Why not just have an else statement instead of the 'if'?
> 
> >   if($line !~ m|.*_.*\n|){
> >   @fieldValue = split(" ",$line);

As mentioned below, you are not saving off any of these values. So at the
end, you only have the values from the last line. You need to print here, or
save off your data.

To print in the format you desire, you could use a counted loop or a hash
slice.

For example, a hash slice could be used as follows:

     my %hash;
     @hash{@headerNames} = @fieldValue;

You could then print each key and value on a line (to get them in the
desired order you may have to loop over @headerNames to get the key values).

> >   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/


Reply via email to