Ask a Question related to PERL Beginners, Design and Development.
-
Lonewolf #1
big switching...
I have a 12 field 3000 line file (| delimited) and need to check the 7th
entry of each line to see what it is, and based on that it needs to be
written out to a file with a new entry in the 7th spot. I could do it with
a few entries to check, but the permutation is of 253 different choices.
Example:
data.txt
ww-a10991t|rolls|5765|365.3|385|12|RR|Rolls Royce|2|0|1|A
ww-a10991w|rolls|5763|365.3|385|12|RR|Rolls Royce|2|0|1|A
ww-a10991b|rolls|5790|365.3|385|12|RR|Rolls Royce|2|0|1|A
RR is keyed as category 235 so the outputed file will be:
ww-a10991t|rolls|5765|365.3|385|12|235|Rolls Royce|2|0|1|A
ww-a10991w|rolls|5763|365.3|385|12|235|Rolls Royce|2|0|1|A
ww-a10991b|rolls|5790|365.3|385|12|235|Rolls Royce|2|0|1|A
My problem is just that the size of the switching and checking seems
attrocious to do.
Thoughts, suggestions?
This is a flat file that I am then loading into MySQL and pulling the data
from it, and without this step of swapping category codes for a category
identification number gives me a HORRIBLE looking output as well as a TON of
errors.
TIA!
Robert
Lonewolf Guest
-
Switching Cameras
Hello Im trying to do a lil something that gets the available cameras in a broadcasting computer and puts em in a combo box. When the broadcaste... -
Switching a camera
I've looked through the threads and can't find/understand if there is an answer to this question: I have a SW3D called "Brooklyn" with a camera... -
Switching to ID
I recently began working in the marketing department of a business that uses Pagemaker for our page layout tool. We use this program because that is... -
Switching from one projector to another
Greetings Situation: Initially I have 2 independent projectors. I launch one of them and on some frame it should close itself and open another... -
switching between layers
Hi, i have five layers and I want to swich between them using MM_showHideLayers() function. My question is how to put function parametrs to the... -
Paul Kraus #2
RE: big switching...
Not tested
While (<file>){
my @record = split /\|/;
if ($record[6]){ #remember arrays start counting at zero
do what ever you want with the data here
}
}
HTH
Paul
-----Original Message-----
From: LoneWolf [mailto:lonewolf@nc.rr.com]
Sent: Tuesday, September 16, 2003 10:42 AM
To: [email]beginners@perl.org[/email]
Subject: big switching...
I have a 12 field 3000 line file (| delimited) and need to check the 7th
entry of each line to see what it is, and based on that it needs to be
written out to a file with a new entry in the 7th spot. I could do it
with a few entries to check, but the permutation is of 253 different
choices.
Example:
data.txt
ww-a10991t|rolls|5765|365.3|385|12|RR|Rolls Royce|2|0|1|A
ww-a10991w|rolls|5763|365.3|385|12|RR|Rolls Royce|2|0|1|A
ww-a10991b|rolls|5790|365.3|385|12|RR|Rolls Royce|2|0|1|A
RR is keyed as category 235 so the outputed file will be:
ww-a10991t|rolls|5765|365.3|385|12|235|Rolls Royce|2|0|1|A
ww-a10991w|rolls|5763|365.3|385|12|235|Rolls Royce|2|0|1|A
ww-a10991b|rolls|5790|365.3|385|12|235|Rolls Royce|2|0|1|A
My problem is just that the size of the switching and checking seems
attrocious to do.
Thoughts, suggestions?
This is a flat file that I am then loading into MySQL and pulling the
data from it, and without this step of swapping category codes for a
category identification number gives me a HORRIBLE looking output as
well as a TON of errors.
TIA!
Robert
--
To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
For additional commands, e-mail: [email]beginners-help@perl.org[/email]
Paul Kraus Guest
-
Paul Kraus #3
RE: big switching...
Oppps
Should be if ($record[6] .... ).
If you used it as is if ($record[6]) its just going to see if something
is a there.
-----Original Message-----
From: Paul Kraus [mailto:pkraus@pelsupply.com]
Sent: Tuesday, September 16, 2003 11:02 AM
To: [email]LoneWolf@nc.rr.com[/email]; [email]beginners@perl.org[/email]
Subject: RE: big switching...
Not tested
While (<file>){
my @record = split /\|/;
if ($record[6]){ #remember arrays start counting at zero
do what ever you want with the data here
}
}
HTH
Paul
-----Original Message-----
From: LoneWolf [mailto:lonewolf@nc.rr.com]
Sent: Tuesday, September 16, 2003 10:42 AM
To: [email]beginners@perl.org[/email]
Subject: big switching...
I have a 12 field 3000 line file (| delimited) and need to check the 7th
entry of each line to see what it is, and based on that it needs to be
written out to a file with a new entry in the 7th spot. I could do it
with a few entries to check, but the permutation is of 253 different
choices.
Example:
data.txt
ww-a10991t|rolls|5765|365.3|385|12|RR|Rolls Royce|2|0|1|A
ww-a10991w|rolls|5763|365.3|385|12|RR|Rolls Royce|2|0|1|A
ww-a10991b|rolls|5790|365.3|385|12|RR|Rolls Royce|2|0|1|A
RR is keyed as category 235 so the outputed file will be:
ww-a10991t|rolls|5765|365.3|385|12|235|Rolls Royce|2|0|1|A
ww-a10991w|rolls|5763|365.3|385|12|235|Rolls Royce|2|0|1|A
ww-a10991b|rolls|5790|365.3|385|12|235|Rolls Royce|2|0|1|A
My problem is just that the size of the switching and checking seems
attrocious to do.
Thoughts, suggestions?
This is a flat file that I am then loading into MySQL and pulling the
data from it, and without this step of swapping category codes for a
category identification number gives me a HORRIBLE looking output as
well as a TON of errors.
TIA!
Robert
--
To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
For additional commands, e-mail: [email]beginners-help@perl.org[/email]
--
To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
For additional commands, e-mail: [email]beginners-help@perl.org[/email]
Paul Kraus Guest
-
Jenda Krynicky #4
Re: big switching...
From: LoneWolf <lonewolf@nc.rr.com>
use a lookup hash:> I have a 12 field 3000 line file (| delimited) and need to check the
> 7th entry of each line to see what it is, and based on that it needs
> to be written out to a file with a new entry in the 7th spot. I could
> do it with a few entries to check, but the permutation is of 253
> different choices.
%map = (
RR => 255,
RX => 256,
...
)
while (<>) {
chomp;
@record = split /\|/, $_;
$record[6] = $map{$record[6]} || 'unknown';
print join('|', @record),"\n";
}
If you have the mapping between the codes and numbers in the database
you will probably want to populate the hash from the database.
Jenda
===== [email]Jenda@Krynicky.cz[/email] === [url]http://Jenda.Krynicky.cz[/url] =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
Jenda Krynicky Guest
-
John W. Krahn #5
Re: big switching...
Lonewolf wrote:
You should probably use a hash to map the choices to their new values:>
> I have a 12 field 3000 line file (| delimited) and need to check the 7th
> entry of each line to see what it is, and based on that it needs to be
> written out to a file with a new entry in the 7th spot. I could do it with
> a few entries to check, but the permutation is of 253 different choices.
>
> Example:
> data.txt
> ww-a10991t|rolls|5765|365.3|385|12|RR|Rolls Royce|2|0|1|A
> ww-a10991w|rolls|5763|365.3|385|12|RR|Rolls Royce|2|0|1|A
> ww-a10991b|rolls|5790|365.3|385|12|RR|Rolls Royce|2|0|1|A
>
> RR is keyed as category 235 so the outputed file will be:
> ww-a10991t|rolls|5765|365.3|385|12|235|Rolls Royce|2|0|1|A
> ww-a10991w|rolls|5763|365.3|385|12|235|Rolls Royce|2|0|1|A
> ww-a10991b|rolls|5790|365.3|385|12|235|Rolls Royce|2|0|1|A
>
> My problem is just that the size of the switching and checking seems
> attrocious to do.
>
> Thoughts, suggestions?
my $file = 'data.txt';
my %choices = (
AA => 1,
BB => 2,
...
RR => 235,
...
ZZ => 253,
);
open FILE, $file or die "Cannot open $file: $!";
while ( <FILE> ) {
my @fields = split /\|/;
$fields[ 6 ] = $choices{ $fields[ 6 ] } if exists $choices{ $fields[
6 ] };
do_something_with_modified_data( @fields );
}
close FILE;
John
--
use Perl;
program
fulfillment
John W. Krahn Guest



Reply With Quote

