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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Arrays and delimiters, IFS help



May 4, 2012, 8:34 AM

Post #1 of 3 (1732 views)
Arrays and delimiters, IFS help Can't Post

Hello, Iím sure someone must have asked thisÖ it seems like a very basic question but I guess Iím not searching on the right words or phrases.

I have a file that has many rows in it. Each row has 75 columns. The delimiter that was used was hex 7f (ďdeleteĒ, chr(127))Ö not sure why they did that but it was before my time. Now I have to find rows that have a null value in the 72 column.

Iím loading the whole file in to an array called @data. I then iterate through the lines of the array with a foreach. But, Iím using brute force by loading 75 values in to variables using the split command and chr(127) as my delimiter. Isnít it possible for me to put the whole line in another array, @line, set the IFS to be chr(127) and then check directly for $line[72]?

This is for work so I felt uncomfortable posting the actual data. Below is a shorter version of the brute force method Iím using but itís for only four columns.

open(FH,"<$filename") or die "Could not open $filename: $!";

my @data = <FH>;

close (FH);

#...set the value for the column to sum up

foreach my $inline (@data){

chomp $inline;

my ($value1, $value2, $value3, $value4) = split(chr(127),$inline);

print "Value1: $value1, Value2: $value2, Value3: $value3, value4: $value4\n";

<pseudo code> then evaluate if $value3 is null </pseudo code>




Veteran / Moderator

May 4, 2012, 9:23 AM

Post #2 of 3 (1724 views)
Re: [dw.worker.bee] Arrays and delimiters, IFS help [In reply to] Can't Post

Don't slurp the entire file into an array. Instead, loop over it line-by-line. Then split the line and possibly use an array slice when doing that split so that you can extract only the desired fields.

On another note, use a lexical var for the filehandle instead of the bareword. And, use the 3 arg form of open.


open my $FH, '<', $filename or die "Could not open $filename: $!"; 
my $delim = char(127);

while (my $line = <$FH>) {

# extract the 73 field
my $field = (split(/$delim/, $line))[72];

# process as needed


May 4, 2012, 9:31 AM

Post #3 of 3 (1719 views)
Re: [FishMonger] Arrays and delimiters, IFS help [In reply to] Can't Post

Thanks so much FishMonger. I did get something working and was about to post a reply when I saw your post. You have a few other suggestions I will adopt. Thanks again.


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

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