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

Hi, i'm fairly new to perl and would really appreciate some help. I have an output file containing 15 columns of data, I have written a script to split the data into elements of an array which I want to print to a new file (columns 14 and 15). Its not working! error messages don't lke the print line below. should i have used a hash of arrays because there is more than piece of data in each column.?? if so how do you use them?? Thanks.
#! /usr/local/bin/perl -w use strict; open (FILEHANDLE, "<file.parse") or die "unable to open file"; my $line; my @array; while (<FILEHANDLE>) { $line = $_; chomp ($line); @array = (); @array = split (/\s+/, $line); # error messages say that something is uninitialised in the below line +! print STDOUT "$array[13]\t$array[14]\n"; } open (OUTFILE, ">$$.output"); print OUTFILE "$array[13]\t$array[14]\n"; close OUTFILE; exit;

Replies are listed 'Best First'.
Re: hashes of arrays ??
by davis (Vicar) on Apr 18, 2002 at 10:02 UTC
    Hi
    Good question, with sample code that uses strict and warnings. ++lolly
    My guess is the split isn't working - you're asking split to split on one or more whitespaces.
    I think to answer this, we'd need some sample data, then we might be able to help you.
    davis
    Is this going out live?
    No, Homer, very few cartoons are broadcast live - it's a terrible strain on the animator's wrist
Filter for the lines where your data is...
by RMGir (Prior) on Apr 18, 2002 at 12:07 UTC
    Is it possible that some blank lines exist, or header or footer lines that don't have all the fields?

    That happens pretty often in files I deal with. In that case, you have 2 options.

    If none of the header/footer/blank lines have all the fields, you can just do

    if(@array>14) { print STDOUT "$array[13]\t$array[14]\n"; print OUTFILE .... }
    (assuming you open OUTFILE above the loop, and want to write all these items to it, as someone else suggested...)

    If you have a "header" type line, or line of dividers or something like that, then you'll have to add some checks to skip this, like:

    while(<FILEHANDLE>) { next if /------/; # skip divider line next if /SALES/; # skip header line next if /TOTAL/; # skip summary line next if /^$/; # skip blank lines ... }
    with the correct regular expressions for your data, of course. Make sure that SALES can never appear on a normal line, for instance, if you're going to use that as a screen.

    Hope this helps!
    --
    Mike

Re: hashes of arrays ??
by TheHobbit (Pilgrim) on Apr 18, 2002 at 11:31 UTC
    Hi,

    as other monks pointed out, the problem lays probably in the fact that the output of split do not have 15 elements. Moreover the open OUTFILE should be moved <strng>before the while loop, and the print OUTFILE inside it.

    It is a <emph>good thing</emph>© to use 'use strict' and '-w', but you are a little to cautios. No need for the
    @array = ();
    line, nor for the
    $line =~ $_
    one. For the last one, you could either replace the loop by
    while($line = <FILEHANDLE>) { ... }
    or you could let the loop as it is, suppress the $line variable and use the fact that chomp and split default to $_ when no string is specified, so you'll have
    while (<FILEHANDLE>) { chomp; @array = split /\s+/; ... }

    Cheers
    Leo TheHobbit
    GED/CS d? s-:++ a+ C++ UL+++ P+++>+++++ E+ W++ N+ o K? !w O? M V PS+++
    PE-- Y+ PPG+ t++ 5? X-- R+ tv+ b+++ DI? D G++ e*(++++) h r++ y+++(*)
Re: hashes of arrays ??
by Chady (Priest) on Apr 18, 2002 at 10:24 UTC

    Just a little note on a bug that has nothing to do with the uninitialized print is that your code will print the last occurence of 14/15 colums in the output file. its print should be inside the while loop.


    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/
Re: hashes of arrays ??
by broquaint (Abbot) on Apr 18, 2002 at 15:00 UTC
    TIMTOWTDI ;-)
    use Inline AWK; awk(); __END__ __AWK__ { print $13 "\t" $14; }
    Just thought I'd include this because it's what awk is good at doing - munging space-delimited fields.
    For more info on our beloved perl's predecessor check out Effective AWK Programming online.
    HTH

    broquaint