I have a simple program that attempts to convert lines to a csv file. The way I see it it shoule output like this:
Applebee's Neighborhood Grill & Bar*, 11500 Shawnee Mission Pkwy.,Shawnee, KS, 66203, 913-962-1133
as one line. But it is not and I don't see why. I am putting each line into an array and then at the end doing a join. Why am I getting a newline after the company name and then again after the street address?
As an example...
INPUT FILE
"Shawnee, KS -- Chamber of commerce"
Applebee's Neighborhood Grill & Bar*
11500 Shawnee Mission Pkwy.
"Shawnee, KS 66203"
913-962-1133
Arby's
7404 Nieman Rd.
"Shawnee, KS 66203"
913-631-1425
Arizona's Neighborhood Grille & Bar
7182 Renner Rd.
"Shawnee, KS 66217"
913-962-9988
OUTPUT FILE
"Shawnee, KS -- Chamber of commerce"
Applebee's Neighborhood Grill & Bar*
, 11500 Shawnee Mission Pkwy.
, Shawnee, KS, 66203, 913-962-1133
Arby's
, 7404 Nieman Rd.
, Shawnee, KS, 66203, 913-631-1425
Arizona's Neighborhood Grille & Bar
, 7182 Renner Rd.
, Shawnee, KS, 66217, 913-962-9988
SOURCE -
#!/usr/bin/perl
-
use warnings;
-
use strict;
-
#define variables
-
my $FileIn = 'shawneechamber.txt';
-
my $FileOut = 'row2col.csv';
-
my $data;
-
my $lineno =1;
-
my $linecounter=0;
-
my @csvline;
-
my $date;
-
my $container;
-
-
#open file for reading
-
open (FILEIN, "<$FileIn") or die ("Can not open $FileIn for reading: $!.\n");
-
open (FILEOUT, ">$FileOut") or die ("Can not open $FileOut for reading: $!.\n");
-
binmode(FILEIN);
-
binmode(FILEOUT);
-
-
while (<FILEIN>) {
-
-
$data=$_;
-
chomp($data);
-
print("Working with $data\n");
-
-
if ($lineno == 1){ #It is the header
-
print FILEOUT $data;
-
$lineno++;
-
}elsif($data =~ /^\s*$/){
-
print("I have a space.\n");
-
$linecounter=0;
-
}elsif($data =~ /\d+\/{1}\d+\/{1}\d+/){
-
$date=$data;
-
}elsif($data =~ /"(\w*),\s*(\w+)\s+([0-9]{2,})"/){ #Is it a City State zip
-
print("City is $1 State is $2 and Zip is $3 Linecounter $linecounter.\n");
-
$csvline[$linecounter]= $1;
-
$linecounter++;
-
$csvline[$linecounter]= $2;
-
$linecounter++;
-
$csvline[$linecounter]= $3;
-
$linecounter++;
-
print ("@csvline\n");
-
}elsif($data =~/\s*\d+-\d+-\d+/){
-
print("I have a phone of $data\n");
-
$csvline[$linecounter] = $data;
-
print ("A complete record\n");
-
print ("$csvline[0], $csvline[1], $csvline[2], $csvline[3], $csvline[4]\n");
-
print ("A complete record: @csvline\n");
-
$data=join(', ', @csvline);
-
print FILEOUT $data;
-
#print FILEOUT "\n";
-
$#csvline = -1; #clear all values in the array
-
$linecounter = 0;
-
}else{
-
chomp($data);
-
print("Value of line counter is $linecounter\n");
-
print("Data in else statement is: $data\n");
-
$csvline[$linecounter]=$data;
-
print("Data in array is $csvline[$linecounter]\n");
-
$linecounter++;
-
print("Data in array is $csvline[0]\n");
-
}
-
-
}#End of While Loop
-
close FILEIN;
-
close FILEOUT;
-
9 1818 KevinADC 4,059
Recognized Expert Specialist
Are you talking about the data getting printed to the file that is messed up?
Are you talking about the data getting printed to the file that is messed up?
Yes I want each customer record to be a line in a csv file. This program is putting new lines or return carriages and I don't know why. I am chomping the strings.
KevinADC 4,059
Recognized Expert Specialist
Any change the input file was created on one operating system and you are processing it on a different one? If so, chomp() may not work correctly. You may need to try a regexp instead of chomp: $data =~ s/[\r\n]+$//; numberwhun 3,509
Recognized Expert Moderator Specialist
Any change the input file was created on one operating system and you are processing it on a different one? If so, chomp() may not work correctly. You may need to try a regexp instead of chomp: $data =~ s/[\r\n]+$//;
There is also the possibility of extending your Perl script to use the DBI module, along with the DBD::CSV module. That pretty much enables you to interact with a CSV file as if it were a database, using SQL statements and such.
It may not work for you, but hey, I figured I would throw the option on the pile for you.
Regards,
Jeff
KevinADC 4,059
Recognized Expert Specialist
They don't don't have a CSV file, they are converting to a CSV file.
In my above post I said "Any change..." it should have been "Any chance..."
numberwhun 3,509
Recognized Expert Moderator Specialist
They don't don't have a CSV file, they are converting to a CSV file.
In my above post I said "Any change..." it should have been "Any chance..."
I understand that they don't have a CSV file, but can't you use the SQL CREATE to make one and go from there? Again, just an idea.
Regards,
Jeff
KevinADC 4,059
Recognized Expert Specialist
I understand that they don't have a CSV file, but can't you use the SQL CREATE to make one and go from there? Again, just an idea.
Regards,
Jeff
I'm not sure.
.........
I'm not sure.
.........
Both of these are great ideas and worked thank you very much. I am used to getchar instead of reg expressions and using modules to assist. I appreciate you both breaking the mind lock.
KevinADC 4,059
Recognized Expert Specialist Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Dieter Salath? |
last post by:
Hi,
in our webpage, a user could open a windows explorer to his temp
directory with a simple link and usage of the file protocol:
<a href="file://C:\temp" target="_blank">C:\temp</a>
This worked very well a long time, but now it does not work anymore.
We use IE6 and Microsoft Windows XP Professional 2002 SP2.
I guess it has something to do with new IE security features. Does
|
by: David Cleaver |
last post by:
Hello all,
I was wondering if there were some sort of limitations on the "if"
statement? I'm writing a program which needs to check a bunch of
conditions all at the same time (basically). And I'm pretty sure the
rest of the program is working just fine. The only thing I could think
might be wrong is that the if statement can only hold so many values in
itself? Let me show what I'm doing:
if (table001]>>5]&b&0x1f] != 0 &&
|
by: Matt |
last post by:
I have 2 questions:
1. strlen returns an unsigned (size_t) quantity. Why is an unsigned
value more approprate than a signed value? Why is unsighned value less
appropriate?
2. Would there be any advantage in having strcat and strcpy return a
pointer to the "end" of the destination string rather than returning a
|
by: infobahn |
last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
|
by: Stuart |
last post by:
Ok
let me explain: I am writing a c# program that calls into
an unmanaged C++ third-party DLL. I have to make a number
of calls and for simplification the protype I am calling
is:
bool fn(int *pResult);
This result is used by other subsequent functions that I
have to call.
Q: Is it possible to call this function from C# declaring
| |
by: José Joye |
last post by:
Hello,
I'm currently reading the MS Developing Web applications with c# (and
VB.net).
In the chapter related to Error management, there is a sample about
"Page-Level Error Pages"
eg:
In my form:
========
|
by: Luke Meyers |
last post by:
So, just a little while ago I had this flash of insight. It occurred
to me that, while of course in general there are very good reasons for
the conventional two-file header/implementation separation for each C++
class, there are cases in which this paradigm contributes nothing and
simply introduces extra boilerplate overhead.
The particular case I have in mind is CppUnit tests. Each test header
is only ever included by the...
|
by: fran7 |
last post by:
Hi, from help in the javascript forum I found the error in some code but need help. This bit of code works perfectly, trouble is I am writing it to a javascript function so the height needs to be in "" instead of "" otherwise I get an error message.
Can anyone suggest how to write it so that it writes " instead of "".
I have tried all combinations of adding " to the code but as soon as I think I am there I get throw out again.
if...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |