Code Comments
Programming Forum and web based access to our favorite programming groups.Readers, I have a csv file (greater than 256 columns hence unable to open in spreadsheet) of the following format: column header1, column header2, column header3 1,0.0e0,0.0e0,5e-6 2,0.0e0,0.0e0,6e-7 3,0.0e0,0.0e0,0.0e0 I want to perform: "if column headerx contains only values of 0.0e0, delete the column (including the column header). How do I start please? Yours, mandriva 2008 perl 588121
Post Follow-up to this messagee-letter wrote: > > I have a csv file (greater than 256 columns hence unable to open in > spreadsheet) of the following format: > > column header1, column header2, column header3 > 1,0.0e0,0.0e0,5e-6 > 2,0.0e0,0.0e0,6e-7 > 3,0.0e0,0.0e0,0.0e0 > > I want to perform: "if column headerx contains only values of 0.0e0, > delete the column (including the column header). > > How do I start please? The Text::CSV module will be useful. I suggest you do this in two passes: run through the file once to work out which columns should be deleted, then a second time copying all but the deleted columns to an output file. Does this help get you started? Rob
Post Follow-up to this messagee-letter wrote: > Readers, Hello, > I have a csv file (greater than 256 columns hence unable to open in > spreadsheet) of the following format: > > column header1, column header2, column header3 > 1,0.0e0,0.0e0,5e-6 > 2,0.0e0,0.0e0,6e-7 > 3,0.0e0,0.0e0,0.0e0 > > I want to perform: "if column headerx contains only values of 0.0e0, > delete the column (including the column header). > > How do I start please? This may work for you: #!/usr/bin/perl use warnings; use strict; use Fcntl ':s'; @ARGV = 'somefile.csv'; $^I = '.back'; my @headers = map 1, split /,/, <>, -1; my @keep = ( undef ) x @headers; while ( <> ) { chomp; my @fields = split /,/, $_, -1; for my $i ( 0 .. $#fields ) { $keep[ $i ] = $i if $fields[ $i ] ne '0.0e0'; } last if @headers == grep defined, @keep; } @keep = grep defined, @keep; s
ARGV, 0, SEEK_SET or die "Cannot s
on '$ARGV' $!"; while ( <> ) { chomp; print join( ',', ( split /,/, $_, -1 )[ @keep ] ), "\n"; } __END__ John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order. -- Larry Wall
Post Follow-up to this messageJohn W. Krahn wrote: > e-letter wrote: > > Hello, > > > This may work for you: > > #!/usr/bin/perl > use warnings; > use strict; > use Fcntl ':s'; > > @ARGV = 'somefile.csv'; > $^I = '.back'; > > my @headers = map 1, split /,/, <>, -1; > my @keep = ( undef ) x @headers; > > while ( <> ) { > chomp; > my @fields = split /,/, $_, -1; > for my $i ( 0 .. $#fields ) { > $keep[ $i ] = $i if $fields[ $i ] ne '0.0e0'; > } > last if @headers == grep defined, @keep; That would probably be more efficient as: exit 0 if @headers == grep defined, @keep; > } > > @keep = grep defined, @keep; > > s
ARGV, 0, SEEK_SET or die "Cannot s
on '$ARGV' $!"; > > while ( <> ) { > chomp; > print join( ',', ( split /,/, $_, -1 )[ @keep ] ), "\n"; > } > > __END__ John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order. -- Larry Wall
Post Follow-up to this message> > This may work for you: > > That would probably be more efficient as: > > exit 0 if @headers == grep defined, @keep; > > > > I tried the original code above and received the following error: s() on closed filehandle ARGV at filename.pl line 24, <> line 52. Cannot s
osn 'filename2.csv' Bad file descriptor at filename.pl line 24, <> line 52 Since the file has 32 lines I was
by the reference to 'line 52', so I deleted the empty line below _END_ and then repeated the command: perl filename.pl Nothing happened except that the cursor in the command terminal moved to the next line and remained, flashing without the [...] prefix. I pressed crtl c and looked at the file using mc. The result was that my csv file was totally emptied of content, i.e. became an empty file. So I went to search for the text::csv module. I tried to check if this was installed, with the resulting command terminal output: perl -MText::CSV e -1 Can't open perl script "e": No such file or directory $ perl -e "print qq(@INC)" /usr/lib/perl5/5.8.8/i386-linux /usr/lib/perl5/5.8.8 /usr/lib/perl5/site_perl/5.8.8/i386-linux /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.8/i386-linux /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl . $ perl -MText::CSV_XS e -1 Can't open perl script "e": No such file or directory So now I have no idea how to even check whether the module is already instal led. Next I tried to see if there is any documentation: below is my command terminal output: ls /usr/share/doc/perl perl-AppConfig/ perl-IO-Socket-INET6/ perl-Archive-Cpio/ perl-IO-Socket-SSL/ perl-Archive-Tar/ perl-IO-Tty/ perl-Authen-SASL/ perl-IO-Zlib/ perl-Cairo/ perl-IP-Country-2.23/ perl-CGI-3.29/ perl-Libconf/ perl-Class-Singleton/ perl-libwww-perl/ perl-Compress-Raw-Zlib/ perl-Locale-gettext/ perl-Compress-Zlib/ perl-Mail-SPF/ perl-Config-IniFiles/ perl-Mail-SPF-Query/ perl-Crypt-SSLeay/ perl-MailTools/ perl-Curses/ perl-MDK-Common/ perl-Curses-UI/ perl-MDV-Distribconf/ perl-DateTime/ perl-MDV-Packdrakeng/ perl-DateTime-Locale/ perl-MIME-Lite/ perl-DateTime-TimeZone/ perl-Net-CIDR-Lite/ perl-DBD-mysql/ perl-Net-DBus/ perl-DB_File/ perl-Net-DNS/ perl-DBI/ perl-Net-Ident/ perl-Digest-HMAC/ perl-Net-IP/ perl-Digest-SHA1-2.11/ perl-Net-Jabber-2.0/ perl-Encode-Detect/ perl-Net_SSLeay.pm/ perl-Error/ perl-Net-Telnet/ perl-Expect/ perl-Net-XMPP/ perl-File-BaseDir/ perl-Params-Validate/ perl-File-DesktopEntry/ perl-RPM4/ perl-File-FnMatch-0.02/ perl-SOAP-Lite/ perl-File-MimeInfo/ perl-Socket6/ perl-Glib/ perl-String-CRC32/ perl-Gnome2-Print/ perl-String-ShellQuote/ perl-Gnome2-Vte/ perl-SVN/ perl-Gtk2/ perl-Sys-Hostname-Long/ perl-Gtk2-Html2/ perl-Term-ReadKey/ perl-Gtk2-NotificationBubble/ perl-TimeDate/ perl-Gtk2-SourceView-1.000/ perl-URI/ perl-Gtk2-TrayIcon/ perl-URPM/ perl-HTML-Parser/ perl-version/ perl-HTML-Tagset/ perl-XML-Parser/ perl-IO-Compress-Base/ perl-XML-Stream-1.22/ perl-IO-Compress-Zlib/ perl-Youri-Package-RPM-Updater/ No idea what this all means! I thought I would try and learn word by word, so trying to find out what 'fcntl' means; no idea where to start??? Totally bewildered! Yours, > > > John > -- > Perl isn't a toolbox, but a small machine shop where you > can special-order certain sorts of tools at low cost and > in short order. -- Larry Wall > > -- > To unsubscribe, e-mail: beginners-unsubscribe@perl.org > For additional commands, e-mail: beginners-help@perl.org > http://learn.perl.org/ > > >
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.