Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: Can Text::CSV_XS return key-value pairs?

by Lady_Aleena (Curate)
on Jun 13, 2017 at 14:05 UTC ( #1192689=note: print w/replies, xml ) Need Help??


in reply to Re: Can Text::CSV_XS return key-value pairs?
in thread Can Text::CSV_XS return key-value pairs?

choroba, could you please include where you got the headings/headers? You used the method new() instead of the function csv(). Since new() doesn't have a header option, I don't know where the headers are invoked if they are not the first line of the file being parsed.

As for the secondary new() splitting (or whatever does it) at ;, that would be triggered whenever a header has a + at the end of it (and the + being removing from the header after parsing). So, for example, my movies.txt has the following headers...

'headers' => ['title','start year','end year',qw(media format+ Wikiped +ia allmovie IMDb TV.com Flixster genre+ source company)],

format and genre would be parsed at the ; while all other fields are strings.

So for a file where I only want a key-value pair for each line, it looks like I will have to put in 2 headers, and if the second one has a + it is to be parsed with the value being split (or whatever) on the ;.

I gravitated directly to csv() because it looked easier to use than new() since I could not figure out what did what. Like what makes an array of hashes and what makes a hash of hashes (what I use mostly). The whatchamacallits (like getline, parse, etc) are not grouped together in such a way as to make it obvious to me.

So, would you please expand the code so I can see everything you are doing? I am a bit lost.

No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
Lady Aleena

Replies are listed 'Best First'.
Re^3: Can Text::CSV_XS return key-value pairs?
by choroba (Bishop) on Jun 13, 2017 at 23:19 UTC
    I used new instead of csv because it gives you more control over what you can do. Here, no headers are defined at all, which means the row is split into an array reference $row. The second part of the row is processed by the very same module, as you recognised, to split the string on semicolons—if I understand your comment correctly, you'd need to do that for each column with the + in the general case. The output structure is being built on line 31, where the first part of the line is used as the key, and the result of the secondary processing is used as the value.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2019-06-27 10:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Is there a future for codeless software?



    Results (111 votes). Check out past polls.

    Notices?