Advertisement

09.10.2005 at 11:36AM PDT, ID: 21557103
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Remove white space or???

Tags: perl, remove, white, space, whitespace
I have this type of data repeated over 100 times that I need to extract and write to a .csv file.
(Just the numbers not the headers)

        CAUCPSCT
              KEY (CELL_NUMBER)
                CAUPGRES   CAUNOFOF   CAUTSUCC   CAUTBLKS   CAUEDLOT    CAUTRLS
                CAUOATTS   CAUOSUCC   CAUOBLKS   CAUORODR    CAUORLS   CAUERSFL
                CAUERLFL   SLTPGRES   CAUHINIT   CAUHATTS   CAUHBLKS   CAUHRLFL
                CAUHSUCC    CAUHRLS   CAUDROPR   CAUDROPN   CAUESWFL   CAUNOTCE
                CAUFWCAP   CAURECAP   CAUNOWCD   MCTAHRQF   MCTALLTO   MCTALLFU
                MCTAREQF   MCTAMIXF
       
         362 301Z
                        5           0           5          0          0          0
                      11         11           0          0          0          0
                        0           5           0          0          0          0
                        0           0           0          0          0          0
                        0           0           0          0          0          0
                        0           0
     
         363 302X
                      12          0         12          0          0          0
                      35         34          0          0          0          0
                        1         12          0          0          0          0
                        0           0          0          0          0          0
                        0           0          0          0          0          0
                        0           0

I had been doing a readline then got stuck as to the best way to remove the whitespace from between the numbers and add it to a scaler as csv or an array. Even though it doesn't show here very well the numbers are in columns.

Please keep in mind I am less than a NOVICE.  Perl is very new to me but I am determined to learn it.

Thanks for the help.
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: mayfieldjr
Solution Provided By: Adam314
Participating Experts: 3
Solution Grade: B
Views: 295
Translate:
Loading Advertisement...
09.10.2005 at 12:08PM PDT, ID: 14857026

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
09.10.2005 at 01:32PM PDT, ID: 14857237

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
09.12.2005 at 06:52AM PDT, ID: 14863949

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
09.12.2005 at 06:26PM PDT, ID: 14868665

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
09.13.2005 at 04:08AM PDT, ID: 14870553

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
09.13.2005 at 07:56AM PDT, ID: 14872317

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
09.13.2005 at 09:58AM PDT, ID: 14873592

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
09.13.2005 at 11:26AM PDT, ID: 14874468

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • Automotive
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Displays / Monitors
  • Handhelds / PDAs
  • Components
  • Peripherals
  • Laptops/Notebooks
  • Servers
  • Misc
  • Apple
  • Embedded Hardware
  • Networking Hardware
  • Storage
  • Desktops
  • New Users
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMware
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Virtualization
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • Web Computing
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Consulting
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMware
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Automation
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Web Services
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Web Computing
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Lounge
  • Business Travel
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
  • Automotive
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
09.10.2005 at 12:08PM PDT, ID: 14857026

Rank: Genius

Is there a reliable way of knowing when the headers stop ans the numbers start?
(e.g. can we be sure there are never any numbers in the header?)
and is 301Z a number or a header?
to remove whitespace and print as comma separated values, you could

    print join(',',split),"\n";
Assisted Solution
 
09.10.2005 at 01:32PM PDT, ID: 14857237
It looks to me like you want to read until you hit a blank line. That's the end of each group.

I'm assuming you have already skipped past the header.
Something like the following. This is untested as yet, but it gives you the idea:

use strict;
my $csv = "";
while (my $line = <>) {
  chomp $line; #strip off newline
  if ($line eq "") {
    $csv =~s/^\s+//; # replace leading spaces with nothing
    $csv =~s/\s+$//; #replace trailing spaces with nothing
    $csv =~ s/\s+/, /g; # replace every group of one or more spaces with comma space
    print $csv, "\n";
     $csv = "";
  } else {
     $csv .= $line;
  }
}
 
Assisted Solution
 
09.12.2005 at 06:52AM PDT, ID: 14863949
I will try dbkruger's solution.  You actually answered two questions in one here.

To answer ozo's question:  This data comes out every hour in exactly the same format and order.  The headers are ALWAYS the same as you saw them.  The only thing that might change is the first line of data.  It may not be   362 301Z.  (Site 301Z) and the last site might not be the same in the data so I can't say stop a loop when the site = XXX.

Using Visual basic I could just:

Dim Ky as integer:X as integer
Dim Site as string
Dim Dat(32) as long

'after I got past the headers
Do
   input #1,ln
   input #1, Ky:if Ky = 0 then exit for   '  This would get me to the end of the data.  The last Input #1, Ky is acting on a line like this "--------"
   input #1, Site
   for X = 1 to 32
      input #1,Dat(x)
   next X
   print #2, Site;",";
   for X = 1 to 31
      print #2, Dat(x);",";
   next x
   print #2, Dat(32)
loop

Thank you for your comments - I'll get back to you ASAP.
 
09.12.2005 at 06:26PM PDT, ID: 14868665
I'm a little confused??  It looks like the if statement is saying if the line is blank then get the data values from it else  $csv .= $line??  Guess I better look in the Perl book I have to figure out what that means.  Told you I am a real novice.

use strict;
my $csv = "";
while (my $line = <>) {
  chomp $line; #strip off newline
  if ($line eq "") {
    $csv =~s/^\s+//; # replace leading spaces with nothing
    $csv =~s/\s+$//; #replace trailing spaces with nothing
    $csv =~ s/\s+/, /g; # replace every group of one or more spaces with comma space
    print $csv, "\n";
     $csv = "";
  } else {
     $csv .= $line;
  }
}
I hope to have time to get to this Tuesday.  
Thanks.
 
09.13.2005 at 04:08AM PDT, ID: 14870553

Rank: Genius

#!/bin/perl
$/='';
while( <> ){
   print join(',',split),"\n";
}


or
perl -n00e "BEGIN{<>}print join(', ',split),qq(\n)" data > data.csv
 
09.13.2005 at 07:56AM PDT, ID: 14872317
Ozo,

I don't get it. Won't that just replace each line with comma separation? He has multiple lines that are one record, separated by a blank line (I thought).
 
09.13.2005 at 09:58AM PDT, ID: 14873592
Here is sample data with just TWO records (of the 150 or so)   So you are seeing the begining to end
       
        CAUCPSCT
              KEY (CELL_NUMBER)
                CAUPGRES   CAUNOFOF   CAUTSUCC   CAUTBLKS   CAUEDLOT    CAUTRLS
                CAUOATTS   CAUOSUCC   CAUOBLKS   CAUORODR    CAUORLS   CAUERSFL
                CAUERLFL   SLTPGRES   CAUHINIT   CAUHATTS   CAUHBLKS   CAUHRLFL
                CAUHSUCC    CAUHRLS   CAUDROPR   CAUDROPN   CAUESWFL   CAUNOTCE
                CAUFWCAP   CAURECAP   CAUNOWCD   MCTAHRQF   MCTALLTO   MCTALLFU
                MCTAREQF   MCTAMIXF
        #  Just one space here   FROM HERE TO.....
         361 301Y
                      12          0         12          0          0          0
                      19         18          0          0          0          0
                       1         12          0          0          0          0
                       0          0          0          0          0          0
                       0          0          0          0          0          0
                       0          0
     #  just one blank line here     .....HERE IS ONE RECORD WITH 34 FIELDS
         362 301Z
                       7          0          7          0          0          0
                      15         15          0          0          0          0
                       0          7          0          0          0          0
                       0          0          1          0          0          0
                       0          0          0          0          0          0
                       0          0
        # Just one blank line and this is the end of the records  This is where I need to stop.
             ------------------------------------------------------------------  
                    2147          0       2108          5          0         15
                    4393       4267         27          0         39         28
                      77       2147         89         89          0          2
                      87          0         88          2          0         28
                       0          0          0          0          0          0
                       0          0

So one record that I would write to a .csv file would look like this using the first record above:
361,301Y,12,0,12,0,0,0,19,18,0,0,0,0,1,12,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

Hope this explains what I am working with better.  I also tried what dbkruger sent (exactly as written above) and I have to do a ctrl C to stop it.  Runs forever.  Maybe I put it in wrong ???

Thanks again.
 
09.13.2005 at 11:26AM PDT, ID: 14874468

Rank: Genius

Using suggestions from both dbkruger and ozo, i came up with this... (it is clearly based on the code from dbkruger)
Change the InFilename and OutFilename as needed.

#!/usr/bin/perl
use strict;

#Change the following lines to be the name of your input data and output data
my $InFilename="data1.txt";
my $OutFilename="data2.txt";

my $csv = "";
#Use this if your data is in a text file...Comment out the following line
open(INFILE,"<$InFilename") or die "Couldn't open $InFilename: $!\n";
open(OUTFILE,">$OutFilename") or die "Couldn't open $OutFilename: $!\n";

my $line="";
while ($line = <INFILE>) {
      chomp($line);
      if ($line =~ /^\s*$/) {
            $csv =~ s/^\s+//;
            print OUTFILE join(',',split(/\s+/,$csv)),"\n";
            $csv = "";
      } else {
            $csv .= $line;
      }
}
if($csv ne ""){
      $csv =~ s/^\s+//;
      print OUTFILE join(',',split(/\s+/,$csv)),"\n";
}

close(INFILE);
close(OUTFILE);
Accepted Solution
 
 
20080236-EE-VQP-29