Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Best method of munging CSV data - using Text::CSV::Simple?

by Cody Pendant (Prior)
on Feb 08, 2006 at 05:39 UTC ( #528727=note: print w/replies, xml ) Need Help??


in reply to Best method of munging CSV data - using Text::CSV::Simple?

Text::CSV::Simple does a great job of ignoring the first column of each line (which is not needed)

Your code ignores the first two columns, because the columns are numbered from zero.

I don't quite understand your overall goal, but this should help:

#!/usr/bin/perl -w use strict; use Text::CSV::Simple; my $parser = Text::CSV::Simple->new; $parser->want_fields(1, 2, 3, 4); my @data = $parser->read_file(\*DATA); foreach my $line (@data) { if ($line->[0] =~ m/SMTP:/i) { print "here are the parts of the first column:\n"; my @parts_of_first_column = split(';', $line->[0]); foreach my $part (@parts_of_first_column) { print " * $part\n"; } } } __DATA__ "CN=MBX_MANA,OU= Mailboxes,DC=doman,DC=com",SMTP:Manager@domain.com;sm +tp:MBX_MANA@domain.com;FAX:MBX_MANA@domain.com;X400:c=us\;a= \;p=doma +in\;o=Exchange\;s=MANAGER\;,MBX_MANA,/o=Exchange Org/ou=First Adminis +trative Group/cn=Configuration/cn=Servers/cn=SERVER "CN=Guest,CN=Users,DC=domain,DC=com",,Guest,

Which prints out:

here are the parts of the first column: * SMTP:Manager@domain.com * smtp:MBX_MANA@domain.com * FAX:MBX_MANA@domain.com * X400:c=us\ * a= \ * p=domain\ * o=Exchange\ * s=MANAGER\

So Text::CSV::Simple splits up the CSV in a smart way, and then the code splits up the first (remaining) column by semicolons. From there on you should be able to test each part for the stuff you want.



($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
=~y~b-v~a-z~s; print

Replies are listed 'Best First'.
Re^2: Best method of munging CSV data - using Text::CSV::Simple?
by billie_t (Sexton) on Feb 08, 2006 at 06:15 UTC

    Apologies for the typo with my list of fields - I mistakenly pasted from old code - I've got the correct (1,2,3) in my new stuff, although I was pulling my hair out there for a while.

    That is helpful, thank you. So, the parts of the line are line[0], line[1], line[2] when they're divvied up by Text::CSV::Simple. You've dumped line[0] into an array to break up its components, that's great.

    So I can manipulate line[1] and line[2] to grab the other parts I need. In that loop, I should be able to output something line-by-line like (pseudocode) "@parts[0]    line[1]@line[2]", which is effectively "SMTP_ADDRESS    USER@SERVER". That's fantastic, I was imagining all kinds of wierd structures in my head, not a simple and understandable one.

      I was imagining all kinds of wierd structures in my head, not a simple and understandable one.

      That's why they invented Data::Dumper.

      I'd be lost without it.

      Just add use Data::Dumper; and do print Dumper(\$something); and it makes things a lot easier. That's how I figured out what you data was doing.



      ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
      =~y~b-v~a-z~s; print

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2021-09-22 23:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?