Forum Moderators: coopster & phranque

Message Too Old, No Replies

Need script to separate numbers according to space

The result will be used as a table in PHP/MySQL

         

StevenHu

3:30 pm on Sep 17, 2008 (gmt 0)

10+ Year Member


I have two columns in my Excel file. Left column is a part number. Right column contains all the categories that part number belongs to (such as decal, car body, tools, etc.) The categories are separated by a space. The list contains over 3000 part numbers.

I need to make a separate line for each category so each row will consist of one part number and one category. This will then be output as a table in PHP/MySQL. The parts will be called from the table according to the category. It will result in a parts lookup on a web site.

Example:

part number categories
21030 3005 3006 3007

Question: What is the Perl script for me to change the above to the following:

21030 3005
21030 3006
21030 3007

I know Perl won't work on Excel files. Should I export it as CSV or Tab separated? I know nothing about Perl, unfortunately, but I do remember that it is good at the above sort of thing, and want to save myself a week or more of effort.

Would PHP be a better tool for this job?

Regards,
Steve

StevenHu

7:34 pm on Sep 22, 2008 (gmt 0)

10+ Year Member



That would be cool if you can make it into a table. It is simply a 2-column table that will become a linking table. PartNo and Category are the two field names, and they will be linked as a single Primary Key.

An added plus would be to make all text lower-case.

Thanks for your help!
Steve

Dabrowski

7:43 pm on Sep 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmmm, my test script is at work, but I think if you change 1 line like this it should lowercase everything.

[perl] my ( $part, $categories) = split( /,/, lc); [/perl]

I'm not too hot on database structure with all that linking. When I do it I just give everything an ID and sort it out myself! :D

I'm sure one of the other guys will help you out with that one.

[edited by: Dabrowski at 7:44 pm (utc) on Sep. 22, 2008]

StevenHu

7:50 pm on Sep 22, 2008 (gmt 0)

10+ Year Member



Curious. Here's the output:

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings>perl cat.pl
0 - 1

It resulted in 0 in colA and 1 in colB, on screen and CSV file, and nothing else.

Thanks,
Steve
C:\Documents and Settings>

Dabrowski

8:32 pm on Sep 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thought it might do that. Try this.

[perl]my ( $part, $categories) = split( /,/, lc( $_)); [/perl]

StevenHu

9:48 pm on Sep 22, 2008 (gmt 0)

10+ Year Member



The output is still "0 - 1" after trying your 7:43 pm and 8:32 pm posts. I wonder what changed, exactly - no error messages!

No need to make it a table. I can do that easily.

Thanks!
Steve

Dabrowski

10:02 pm on Sep 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmmm, can't see why. I'll have to test it when I get back to the office, won't be in until Thursday though. In the meantime someone else may find the problem.

StevenHu

10:31 pm on Sep 22, 2008 (gmt 0)

10+ Year Member



Thank you very much, Dabrowski!

StevenHu

7:56 pm on Sep 23, 2008 (gmt 0)

10+ Year Member



OK, after much trial and error, I managed to fix the script so it works!

Heart-felt thanks,
Steve

Dabrowski

12:00 pm on Sep 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad to hear it, what was the problem in the end?

StevenHu

2:26 pm on Sep 24, 2008 (gmt 0)

10+ Year Member



I believe I had put the NEWCSV command in the wrong place earlier. I kept moving lines around until it worked. Here is the script that did it.

Thanks!

use strict;

open( CSV, "part_info.csv") ¦¦ die "Open file failed!\n";
my @lines_in_csv = <CSV>;
close( CSV);

my $parts;
foreach( @lines_in_csv) {
my ( $part, $categories) = split( /,/, lc);
$parts -> {$part} = [ split( /\s+/, $categories)];
}

open( NEWCSV, ">new_part_info.csv") ¦¦ die "File open failed\n";
foreach my $partno ( sort keys( %$parts)) {
foreach my $category ( sort @{ $parts -> {$partno}}) {

## Now you can do something with $partno and $category
## $partno is your original part number
## $category will change each time singularly while $partno stays the same

print "$partno - $category\n";
print NEWCSV "$partno,$category\n";
}
}

This 40 message thread spans 2 pages: 40