#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2001
    Location
    Denver, CO
    Posts
    0
    Rep Power
    0

    Angry


    Guidance for data manipulating

    Now that I have it in one line I just have to ask what is the best way to make this data into a line containing 8 elements. My data looks like this.

    code:
    2,0|01-24-2001
    2,1|1300
    2,2|BB02SO
    2,3|1
    2,4|BLVBB
    2,5|BB01-BB06
    2,6|BLV
    2,7|N/A
    2,8|N/A
    3,0|01-24-2001
    3,1|1730
    3,2|ZK51024
    3,3|1
    3,4|DENZK
    3,5|ZK51
    3,6|DEN
    3,7|39528
    3,8|020101
    4,0|01-24-2001
    4,1|1730
    4,2|ZM53024
    4,3|1
    etc...

    [\code]

    I used the module that extracts data from an excel spreadsheet and writes it to a file. This is the format that it comes out in. I am trying to get the every nine lines of data into one line, or store every nine lines into a list of strings so that I can insert that data into a MySql database. I am really puzzled about how to get my loop to do this from the format that is shown above or I can also make a file that has one line seporated by a tab or any charater I choose.

    My loop looks like this:
    Code:
    while($line=<MYFILE>)                          
    {                                              
            @row_num=split(/\|/, $line);           
            $data1=$row_num[0];                    
            @col_num=split(/,/, $data1);           
            $data2=$col_num[1];                    
            $row_num[1]=~ s/^\s*(.*?)\s*$/$1/;     
                    if ($data2 == 0)               
                    {                              
                            $row_num[1]=$stardate; 
                    }                              
                    if ($data2] == 1)        
                    {                              
                            $row_num[1]=$startime; 
                    }                              
                    if ($data2 == 2)        
                    {                              
                            $row_num[1]=$sessname; 
                    }                              
                    if ($data2 == 3)        
                    {                              
                            $first1[1]=$irnum;
                    }
           ETC......
    
                    if ($data2 ==8)
                    {
                            $OK=1;
                            $row_num=$rundate;
                            if($OK ==1)
                            {
                                execute "insert into database";
                            }
                       $Cnt=0;
                     }
                $Cnt++;
        }
    will this work?

    Does Any one have a thought?

  2. #2
  3. Ole` Timer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2000
    Location
    N.W. Iowa
    Posts
    472
    Rep Power
    15
    If I'm reading your message correctly I think something like this would probably work to get the results your looking for. The $input file would contain the data you've shown in your example. The $output file would be the re-formatted data with the tab delimit, with 8 entries pre line.

    =================================
    my $input = "/path/to/input_file";
    my $output = "/path/to/output_file";

    open(FILE,"<$file_a") || die $!;
    @lines = <FILE>;
    close (FILE);
    my $i=0;
    open (NEWFILE,">$file_b") || die $!;
    foreach $line (@lines) {
    chomp $line;
    # set to 7 as this is the 8th field
    if ($i < 7) {
    # print data to output file
    print NEWFILE "$line\t";
    # increment counter by 1
    $i++;
    } else {
    print NEWFILE "$line\n";
    # Reset counter
    $i = 0;
    }

    }
    close (NEWFILE);
    =============================

    Hope this helps

    Mickalo

    [Edited by mickalo on 02-07-2001 at 08:32 PM]

    Thunder Rain Internet Publishing

    Custom Programming & Database development
    Providing Personal/Business
    Internet Solutions that work!

IMN logo majestic logo threadwatch logo seochat tools logo