Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Printing out an Array of an Array...

by rd48sec (Novice)
on Feb 03, 2017 at 18:30 UTC ( [id://1181002]=perlquestion: print w/replies, xml ) Need Help??

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

I am new to PERL. I am trying to create a loop to print out several text files created by the eslif switch. Each line from the input files are evaluated and then put in an array "bucket". I am trying to print out each array bucket with:

bias.txt has b40, b20, b80 x.txt has x40, x20, x80 y.txt has y40, y20, y80 z.txt has z40, z20, z80

Any suggestions are welcom. Thanks for looking

@ARGV = ("-40.txt", "+20.txt", "+80.txt");
while (<>) { ($null,$x,$y,$z,$xb,$yb,$zb,$tp,$f1,$f2) = split m!\+|\-|\s\+|\s\- +|\s!; if ($f2 == undef){next;} #skip incomplete line elsif (($x<10) && ($y<10) && ($z<10)){ $axis[0] = "b";} # Bias elsif ($x>30){ $axis[1] = "x";} # X elsif ($y>30){ $axis[2] = "y";} # Y elsif ($z>30){ $axis[3] = "z";} # Z # Temperature if (($tp<45) && ($tp>35)){$temp[0] = 40; $output[@axis][@temp] .= +$_;} #-40 elsif (($tp<25) && ($tp>15)){$temp[1] = 20; $output[@axis][@temp] +.= $_;} #+20 elsif (($tp<85) && ($tp>75)){$temp[2] = 80; $output[@axis][@temp] +.= $_;} #+80 } @file = qw(bias.txt x.txt y.txt z.txt); foreach $filename (@file) { open $fh, '>', $filename; foreach $temp (@{$output[$temp]}) { print $fh $output[$temp][$axis], "\n"; } $axis++; }

Replies are listed 'Best First'.
Re: Printing out an Array of an Array...
by poj (Abbot) on Feb 03, 2017 at 19:42 UTC

    Can you provide a sample of the input date and explain what the output should be. In the meantime this is my guess at what you are trying to achieve.

    #!perl use strict; my $axis; my %output; while (<DATA>){ my ($null,$x,$y,$z,$xb,$yb,$zb,$tp,$f1,$f2) = split ',',$_; next unless defined($f2) ; #skip incomplete line if (($x<10) && ($y<10) && ($z<10)){ $axis = 'bias'; } # Bias elsif ($x>30){ $axis = "x"; } # X elsif ($y>30){ $axis = "y"; } # Y elsif ($z>30){ $axis = "z"; } # Z else { next; } # Temperature if (($tp<45) && ($tp>35)){ $output{$axis}{40} .= $_; } #-40 elsif (($tp<25) && ($tp>15)){ $output{$axis}{20} .= $_; } #+20 elsif (($tp<85) && ($tp>75)){ $output{$axis}{80} .= $_; } #+80 } foreach my $axis (sort keys %output) { open my $fh, '>', $axis.'.txt'; print "Creating $axis.txt \n"; foreach my $temp (sort keys %{$output{$axis}}) { print $fh $output{$axis}{$temp}; } } #$null,$x,$y,$z,$xb,$yb,$zb,$tp,$f1,$f2 __DATA__ ,1,2,3,4,5,6,21,8,9 ,31,2,3,4,5,6,22,8,9 ,1,32,3,4,5,6,23,8,9 ,1,2,33,4,5,6,24,8,9 ,1,2,3,4,5,6,41,8,9 ,31,2,3,4,5,6,42,8,9 ,1,32,3,4,5,6,43,8,9 ,1,2,33,4,5,6,44,8,9 ,1,2,3,4,5,6,81,8,9 ,31,2,3,4,5,6,82,8,9 ,1,32,3,4,5,6,83,8,9 ,1,2,33,4,5,6,84,8,9
    poj

      Thank you so much!! You really helped me out. I am really close. The only thing that I need to get right is the order of each temperature in the file. Right now the "sort keys" puts the temperature order: 20, 40, 80. I need it to be 40, 20, 80.

      One line from input file is

      +00.28 +00.78 +00.36 +00.0 +00.0 +00.0 -40 20 43

      the -40 is the temperature

        Change this line

        #foreach my $temp (sort keys %{$output{$axis}}) { foreach my $temp (40,20,80) {
        poj

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1181002]
Approved by stevieb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-18 20:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found