473,699 Members | 2,258 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

handling arrays.... "We are not in C anymore"

8 New Member
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
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. #define variables
  5. my $FileIn = 'shawneechamber.txt';
  6. my $FileOut = 'row2col.csv';
  7. my $data;
  8. my $lineno =1;
  9. my $linecounter=0;
  10. my @csvline;
  11. my $date;
  12. my $container;
  13.  
  14. #open file for reading
  15. open (FILEIN, "<$FileIn") or die ("Can not open $FileIn for reading:  $!.\n");
  16. open (FILEOUT, ">$FileOut") or die ("Can not open $FileOut for reading:  $!.\n");
  17. binmode(FILEIN);
  18. binmode(FILEOUT);
  19.  
  20. while (<FILEIN>) {
  21.  
  22.    $data=$_;
  23.    chomp($data);
  24.    print("Working with $data\n");
  25.  
  26. if ($lineno == 1){  #It is the header
  27.       print FILEOUT $data; 
  28.       $lineno++;
  29.    }elsif($data =~ /^\s*$/){
  30.       print("I have a space.\n");
  31.       $linecounter=0;
  32.    }elsif($data =~ /\d+\/{1}\d+\/{1}\d+/){
  33.       $date=$data;
  34.    }elsif($data =~ /"(\w*),\s*(\w+)\s+([0-9]{2,})"/){ #Is it a City State zip
  35.             print("City is $1 State is $2 and Zip is $3 Linecounter $linecounter.\n");
  36.             $csvline[$linecounter]= $1;
  37.             $linecounter++;
  38.             $csvline[$linecounter]= $2;
  39.             $linecounter++;
  40.             $csvline[$linecounter]= $3;
  41.             $linecounter++;
  42.             print ("@csvline\n");
  43.    }elsif($data =~/\s*\d+-\d+-\d+/){
  44.         print("I have a phone of $data\n");
  45.         $csvline[$linecounter] = $data;
  46.         print ("A complete record\n");
  47.         print ("$csvline[0], $csvline[1], $csvline[2], $csvline[3], $csvline[4]\n");
  48.         print ("A complete record: @csvline\n");          
  49.         $data=join(', ', @csvline);
  50.         print FILEOUT $data;
  51.         #print FILEOUT "\n";
  52.         $#csvline = -1;  #clear all values in the array  
  53.         $linecounter = 0;
  54.    }else{
  55.        chomp($data);
  56.         print("Value of line counter is $linecounter\n");
  57.         print("Data in else statement is: $data\n");
  58.         $csvline[$linecounter]=$data;
  59.         print("Data in array is $csvline[$linecounter]\n");
  60.         $linecounter++;
  61.         print("Data in array is $csvline[0]\n");
  62.    }
  63.  
  64. }#End of While Loop
  65. close FILEIN;
  66. close FILEOUT;
  67.  
Sep 24 '07 #1
9 1818
KevinADC
4,059 Recognized Expert Specialist
Are you talking about the data getting printed to the file that is messed up?
Sep 24 '07 #2
jaynemarie
8 New Member
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.
Sep 24 '07 #3
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]+$//;
Sep 24 '07 #4
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
Sep 24 '07 #5
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..."
Sep 25 '07 #6
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
Sep 25 '07 #7
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.

.........
Sep 25 '07 #8
jaynemarie
8 New Member
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.
Sep 25 '07 #9
KevinADC
4,059 Recognized Expert Specialist
What worked?

.......
Sep 25 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

10
60710
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
35
2720
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 &&
81
7305
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
188
17355
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
5
7313
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
6
4599
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: ========
10
2021
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...
4
3890
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 &quot;&quot; instead of "" otherwise I get an error message. Can anyone suggest how to write it so that it writes &quot; instead of "". I have tried all combinations of adding &quot; to the code but as soon as I think I am there I get throw out again. if...
0
8697
marktang
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...
0
8622
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,...
0
9184
Oralloy
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...
0
9045
jinu1996
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...
0
8892
tracyyun
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...
1
6538
isladogs
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...
0
5878
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();...
1
3061
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
3
2013
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.