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

Hello there, I have just started using Perl as of today and have run into a problem that I was hoping someone could help with, I have a file with several thousand records in the format: |0|PA|1|||BELL|FRANCIS|D|||1||ANGORA||FLEET|HAMPSHIRE|GU51 2UA| Now I require the first three data items (0 PA 1) from the record stored in three seperate arrays. I know how to cycle through all records however when I am trying to split on | occurance the data is getting lost. Thanks for any help!

Replies are listed 'Best First'.
Re: Reading data from file
by EdwardG (Vicar) on Sep 15, 2004 at 14:05 UTC

    I would need to see your code to guess why data is getting lost, but in general here is how you would split these records as you require(untested):

    while (my $line = <DATA>) { my @columns = split /\|/, $line; push @array1, $columns[1]; # Note the first element [0] is empty push @array2, $columns[2]; push @array3, $columns[3]; } __DATA__ |0|PA|1|||BELL|FRANCIS|D|||1||ANGORA||FLEET|HAMPSHIRE|GU51 2UA|

     

Re: Reading data from file
by Random_Walk (Prior) on Sep 15, 2004 at 14:09 UTC

    Did you escape the pipe | in your split otherwise it is trying to split on nothing or nothing so breaks your data into single characters.

    echo"|0|PA|1|||BELL|FRANCIS|D|||1||ANGORA||FLEET|HAMPSHIRE|GU51 2UA|"\ | perl -ne 'print join "+", split /\|/' +0+PA+1+++BELL+FRANCIS+D+++1++ANGORA++FLEET+HAMPSHIRE+GU51 2UA+ echo"|0|PA|1|||BELL|FRANCIS|D|||1||ANGORA||FLEET|HAMPSHIRE|GU51 2UA|"\ | perl -ne 'print join "+", split /|/' |+0+|+P+A+|+1+|+|+|+B+E+L+L+|+F+R+A+N+C+I+S+|+D+|+|+|+1+|+|+A+N+G+O+R+ +A+|+|+F+L+E+E+T+|+H+A+M+P+S+H+I+R+E+|+G+U+5+1+ +2+U+A+|+ +

    Cheers,
    R.

Re: Reading data from file
by jZed (Prior) on Sep 15, 2004 at 14:35 UTC
    The DBD::AnyData module provides a SQL interface to "pipe delimited" files of the format you describe. You can treat the flat file as a database table and query or modify it with SQL, even use joins on several tables.

    The AnyData module provides a tied-hash interface to the same kinds of data.

    As a new perl person, you certainly should learn how to parse these yourself as suggested in other responses. OTOH, if you have database experience you are probably better off using a database approach in the long run. Even if you are not, you may want to consider using one of the modules which take care of details such as file locking (which you may or may not need).

Re: Reading data from file
by ccn (Vicar) on Sep 15, 2004 at 14:09 UTC
Re: Reading data from file
by Roger (Parson) on Sep 15, 2004 at 15:02 UTC
    You could also use the Text::CSV_XS module (which has the core written in C for speed). Have a look at the manual page on CPAN. It's very fast and very easy to use.

Re: Reading data from file
by ranjan_jajodia (Monk) on Sep 15, 2004 at 14:06 UTC
    Hi there, Put some code so that people can see and point you the errors.
Re: Reading data from file
by rvosa (Curate) on Sep 15, 2004 at 20:41 UTC
    If you just want to separate pipe-delimited data to a file or standard out there's always the cut command, e.g.:
    cut -f1 -d"|" infile