Professional Web Applications Themes

re-format database file - PERL Beginners

Hi All, I have a database file, some_sch.txt that is arranged like this: username1 First Last Some Community Sch 1000 S Adams St Town, Indiana 12345 U.S.A. username2 First Last Some Community Sch 1000 S Adams St Town, Indiana 12345 U.S.A. username3 First Last Some Community Sch 1000 S Adams St Town, Indiana 12345 U.S.A. I want the file to look like this: username1 First Last sch_name 1000 S Adams St Town,Indiana 12345 U.S.A. username2 First Last sch_name 1000 S Adams St Town,Indiana 12345 U.S.A. username3 First Last sch_name 1000 S Adams St Town,Indiana 12345 U.S.A. I don't know how ...

  1. #1

    Default re-format database file

    Hi All,

    I have a database file, some_sch.txt that is arranged like this:

    username1
    First Last
    Some Community Sch
    1000 S Adams St
    Town, Indiana 12345 U.S.A.

    username2
    First Last
    Some Community Sch
    1000 S Adams St
    Town, Indiana 12345 U.S.A.

    username3
    First Last
    Some Community Sch
    1000 S Adams St
    Town, Indiana 12345 U.S.A.

    I want the file to look like this:

    username1 First Last sch_name 1000 S Adams St Town,Indiana
    12345 U.S.A.
    username2 First Last sch_name 1000 S Adams St Town,Indiana
    12345 U.S.A.
    username3 First Last sch_name 1000 S Adams St Town,Indiana
    12345 U.S.A.

    I don't know how to create a loop that will assign each scalar variable
    to the next line (field). Here is what I have so far.... Any suggestions
    or hints, would be greatly appreciated!

    Thanks!

    #!/usr/bin/perl -w

    use strict;


    my $datafile = "C:/brian/marion_sch.txt";
    open (IN, $datafile);
    while (<IN>) {
    chomp;

    my ($user_name, $full_name, $cust_name, $address, $cszc, $blank_line) =
    (split // );
    write;
    }

    format STDOUT =
    <<<<<<<<<<<<< >>>>>>>>>>>>>>>> <<<<<<<<<<<<<<<
    my $user_name, my $full_name, my $cust_name
    ..

    close IN;



    Brian Volk
    HP Products
    317.298.9950 x1245
    <mailto:com> com



    Brian Guest

  2. #2

    Default Re: re-format database file

    > I have a database file, some_sch.txt that is arranged like this: 



    You won't get what you expected, since the first read is 'username1', the
    second
    read is 'First Last', etc etc. So you are unable to split in this way.


    May be you can do in this way :

    use strict;
    open IN, "file.txt";
    my ( data, result );

    while (my $ln = <IN>)
    { chomp $ln;
    if (data == 4 ) # From 'username' to '1000 S Adams St'
    { push result, (join "\t", data);
    push result, $_ ; # This is '12345 U.S.A.'
    <IN> ; # read the blank line and let it be passed
    data = () # clear data for the next user
    }
    push data, $ln;
    }

    print "$_\n" for result;

    Note the code not tested, but guess the flow would be like this. Try modify
    it
    if you want it be in write-format ways.

    HTH,
    Bee


    Bee Guest

  3. #3

    Default Re: re-format database file

    Brian Volk wrote: 

    This, I think, is an easier approach:

    local $/ = ''; # enables "paragraph mode"
    while (<IN>) {
    my rows = split /\n/;
    print join("\t", rows), "\n";
    }

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Guest

Similar Threads

  1. format display from database field
    By JoyRose in forum Macromedia ColdFusion
    Replies: 4
    Last Post: June 7th, 09:06 PM
  2. File Viewer / Bloated file sizes / What is the best file format?
    By Martin_Folley@adobeforums.com in forum Adobe Illustrator Windows
    Replies: 1
    Last Post: June 27th, 05:24 PM
  3. How to export database date to Csv format
    By nic in forum ASP Database
    Replies: 1
    Last Post: January 8th, 04:13 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not 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