Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^3: load a file in memory and extract parts

by toolic (Bishop)
on May 06, 2015 at 16:52 UTC ( [id://1125863]=note: print w/replies, xml ) Need Help??


in reply to Re^2: load a file in memory and extract parts
in thread load a file in memory and extract parts

Tie::File lets you access each line of your file as an array element. It does not load the file into a multidimensional array. The reason for the warning is that you are trying to access an element in an Array-of-Arrays data structure, but you just have a simple array (perldsc). Maybe split will help:
use warnings; use strict; my @file_array = qw(a|b|c d|e|f); foreach my $row (0 .. @file_array - 1) { foreach my $column (split /\|/, $file_array[$row]) { print "$row $column\n"; } } __END__ 0 a 0 b 0 c 1 d 1 e 1 f

Replies are listed 'Best First'.
Re^4: load a file in memory and extract parts
by healingtao (Novice) on May 06, 2015 at 17:17 UTC

    Actually that didn't work, I get the same error at line foreach my $row (0..@file_array-1) Can you try it with tie by loading the following file col1|col2|col3|col4|col5 data1|data2|data3|data4|data5 data1b|data2b|data3b|data4b|data5b Thanks

      If you want to learn by experimenting, remember that a string and an array are very different data structures. Tie::File gives you an array of strings, but you treat it like an array of arrays (containing strings). Perl constructs arrays of arrays as arrays containing array references. See perldsc. Use Data::Dumper to see what your data actually looks like.

      If you just want to solve the original problem, don't parse CSV manually, use Text::CSV_XS to get your data in a format that you can work with. Consider using DBI and DBD::CSV instead for an even shorter way (the SQL INNER JOIN proposed by sundialsvc4) to read AND combine AND export your data. Note that the DBI way also allows to migrate the program to SQLite or any other database in a later version.

      Unless you know your CSV files in every single detail, and especially unless you are absolutely sure that they don't contain nasty things like optional quotes, quoted separator chars, escaped quotes, newlines in data, NULLs, binary data, and so on, you better AVOID split and use Text::CSV_XS. split alone can't handle any of those problems, Text::CSV_XS can handle all of them.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-03-29 11:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found