#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


    I'm trying to get perl to extract information from a file and insert it into a database. The data that I am reading is something like this.

    2|01-24-2001
    2|1300
    2|BB02SO
    2|1
    2|BLVBB
    2|BB01-BB06
    2|BLV
    2|N/A
    2|N/A
    3|01-24-2001
    3|1730
    3|ZK51024
    3|1
    3|DENZK
    3|ZK51

    I am trying to take this data and assign one record as all the same first number. Like @record="01-24-2001","1300","bb02so"..... keyed off of the record number(first 1-2 charactors).

    When I am reading the lines from <STDIN> and splitting them on the "|" and print "$first[1]"; I am only getting one charactor. How can I get the whole value of the array to print?

    My Script looks something like this..

    $testfile="/mydir/tmp/file";
    #open (MYFILE, "$testfile");


    if (open(MYFILE, "$testfile"))
    {
    $line=<MYFILE>;
    while ($line ne "")
    {
    $data=$line;
    @first1=split(/|/, $data);
    print "$first1[1]\n";
    $line=<MYFILE>;
    }
    }

    can anyone help???

    Thank You
  2. #2
  3. No Profile Picture
    PerlGuy
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2001
    Posts
    720
    Rep Power
    42

    Thumbs up


    In the pattern in your 'split' function, you have to escape the pipe ('|') character. A '|' serves as an 'or' in pattern matching. So, what you are doing is saying;

    Split on the space between each character or the space between each character. Try this;
    Code:
    @first1 = split( /\|/, $data );
    Also, to get your code to look all pretty in your post, put it between code tags. Click on the vb Code link at the top of the thread to check out all the tags you can use in your posts.

    hope this helps!
    - dsb -
    Perl Guy
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2001
    Location
    Denver, CO
    Posts
    0
    Rep Power
    0
    Thanks it worked!!!

    Now any thoughts about joining 8 rows into 1.

    Would I use a Do or a While statement?



    [Edited by dark_jurmanji on 02-01-2001 at 03:54 PM]
  6. #4
  7. No Profile Picture
    PerlGuy
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2001
    Posts
    720
    Rep Power
    42

    Wink


    Look into the 'join' function. It is the opposite of a split. It would be something like:
    Code:
    @array = qw(one two three four);
    
    $var   = join( "|", @array );
    - dsb -
    Perl Guy
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

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

    Red face


    Ok now that I have split the data that I need, How would I do the loop to assign @array to collect the 9 lines into the @array. Then loop through it again until EOF?

    I am thinking that I can have a $Count go up to 9 the use a "Sub Insert" and insert the data into my table.
    Am I correct in thinking this?
  10. #6
  11. No Profile Picture
    PerlGuy
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2001
    Posts
    720
    Rep Power
    42
    Let me say first, that I'm sure you could find these answers on your own and you'd probably learn a lot more if you did so. I am glad to help, but I'd rather see you learn this stuff really well.

    That being said:
    Let me be sure what you are trying to do. Open a file, take it in one line at a time, trim off the pipe and everything before it, then create one long scalar with the information that remains. If that's true try this. Otherwise, be a little more specific about what you want to do. Maybe try posting what you want the output to look like and what the file looks like:

    Code:
    open( FH, "filename.txt" ) || die "no way man\n";
    
    while ( <FH> ) {
        chomp;
        @line   = split( /\|/, $_ );
        if ( $record eq "" ) {
            $record = $line[1];
        } else {
            $record .= " " . $line[1];
        }
    }
    
    print $record, "\n";
    The output will be one line of space delimited fields holding all the data from your file.

    You'll have to replace some of this with the information that's appropriate to your files. For example, plug in the name of your file where i have 'filename.txt'.

    Hope this helps
    - dsb -
    Perl Guy
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

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

    Unhappy 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 reall ypuzzled 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.
    Does Any one have a thought?

IMN logo majestic logo threadwatch logo seochat tools logo