Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. 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...
    4. 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...
    5. 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...
  3. #2

    Default 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

  4. #3

    Default 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

  5. #4

    Default Re: big switching...

    From: LoneWolf <lonewolf@nc.rr.com>
    > 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.
    use a lookup hash:

    %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

  6. #5

    Default Re: big switching...

    Lonewolf wrote:
    >
    > 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?
    You should probably use a hash to map the choices to their new values:

    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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139