CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Selective choosing

 



davidcassidy22
Novice

Jul 7, 2015, 9:29 AM

Post #1 of 4 (1846 views)
Selective choosing Can't Post

Hi guys and gals, hope everyone is doing great. The noobie needs some help again. I have files that look like this:


Quote
null 0.022000 0.022000 null null
null null null null null
null 0.021000 0.021000 null null
null 0.024000 0.024000 null null
null null null null null
null null null null null
null 0.025000 0.025000 null null
null null null null null
null null null null null
null 0.024000 0.024000 null 0.023000
null null null 0.023000 null
null null null null null
null null null null null


What I want to do is choose specific ones and put them in a list. If there are 'null' values for every column, then I assign the variable for that line to a 'null'. If there is one non-null value in a column, then I want to assign it to that value. And if there are values in multiple columns, I want it to choose the column "farthest to the right". This sounds a little vague, I apologize, but as an example, in the 10th line since there are 3 different values, I'd like my script to choose the one in the 5th column.

I have something written for the first part, but can't really wrap my head around what I need to write for the other scenarios. I wanted to ask if there was a way to write what I want without an unnecessary amount of "else if's". Here is my code:


Code
#!/usr/bin/perl -w 
use strict;

my $ir_path = "\/home\/master\/files\/data\/glimpse";
my $intab = "$ir_path\/glimpse\_flux1\.txt";
my $outtab = "$ir_path\/flux1\.txt"

die " FILE $intab NOT FOUND\!\n" if (! -f $intab) ;
unlink ("$outtab") if (-e $outtab) ;
open INT, "$intab" or die "Cannot open file $intab" ;
open OUTT, ">$outtab" or die "Cannot open file $outtab";

my $nn=0;
my @flux11;
my @flux12;
my @flux13;
my @flux14;
my @flux15;

while (<INT>) {
next if (! m/^\s+/);
($flux11[$nn],$flux12[$nn],$flux13[$nn],$flux14[$nn],$flux15[$nn]) = (split)[0,1,2,3,4];
$nn=$nn+1;
}

my @flux1;

if (($flux11[$nn] eq 'null') && ($flux12[$nn] eq 'null') && ($flux13[$nn] eq 'null') && ($flux14[$nn] eq 'null') && ($flux15[$nn] eq 'null')) {
$flux1[$nn] = 'null';
}


I'm guessing I would have to add else if's after the initial if statement, but again, I am having a bit of trouble thinking about this in a logical manner.

Some extra info is that there are never instances in which all 5 columns have non-null values, but there are instances where there are 2, 3, and 4 non-null values per line.

Thanks!


Laurent_R
Veteran / Moderator

Jul 7, 2015, 10:15 AM

Post #2 of 4 (1841 views)
Re: [davidcassidy22] Selective choosing [In reply to] Can't Post

Perhaps something like this:

Code
while (<INT>) {  
my @non_nulls = grep { $_ ne "null";} split;
m $line;
if (@non_null) {
$line = $non_null[-1];
} else {
$line = "null";
}
# do something with $line
}



(This post was edited by Laurent_R on Jul 7, 2015, 10:16 AM)


davidcassidy22
Novice

Jul 7, 2015, 11:04 AM

Post #3 of 4 (1835 views)
Re: [Laurent_R] Selective choosing [In reply to] Can't Post

Alright, I'll have to try that out, thanks a ton!


BillKSmith
Veteran

Jul 7, 2015, 3:53 PM

Post #4 of 4 (1824 views)
Re: [davidcassidy22] Selective choosing [In reply to] Can't Post

You should consider using a module.

Code
use strict; 
use warnings;
use List::MoreUtils qw(last_value);

while (my $line = <DATA>) {
chomp $line;
my $var = last_value {$_ ne 'null'} split( /\s+/, $line);
$var //= 'null';
printf "%-40s: %-20s\n", $line, $var;
}
__DATA__
null 0.022000 0.022000 null null
null null null null null
null 0.021000 0.021000 null null
null 0.024000 0.024000 null null
null null null null null
null null null null null
null 0.025000 0.025000 null null
null null null null null
null null null null null
null 0.024000 0.024000 null 0.023000
null null null 0.023000 null
null null null null null
null null null null null


Output:

Code
null 0.022000 0.022000 null null        : 0.022000 
null null null null null : null
null 0.021000 0.021000 null null : 0.021000
null 0.024000 0.024000 null null : 0.024000
null null null null null : null
null null null null null : null
null 0.025000 0.025000 null null : 0.025000
null null null null null : null
null null null null null : null
null 0.024000 0.024000 null 0.023000 : 0.023000
null null null 0.023000 null : 0.023000
null null null null null : null
null null null null null : null

Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives