SunQuest
           Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old February 1st, 2001, 03:36 PM
dark_jurmanji dark_jurmanji is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2001
Location: Denver, CO
Posts: 0 dark_jurmanji User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 sec
Reputation Power: 0
Send a message via ICQ to dark_jurmanji
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

Reply With Quote
  #2  
Old February 1st, 2001, 04:07 PM
dsb dsb is offline
PerlGuy
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2001
Posts: 714 dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 15 h 44 m 20 sec
Reputation Power: 36
Send a message via AIM to dsb
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

Reply With Quote
  #3  
Old February 1st, 2001, 04:25 PM
dark_jurmanji dark_jurmanji is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2001
Location: Denver, CO
Posts: 0 dark_jurmanji User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 sec
Reputation Power: 0
Send a message via ICQ to dark_jurmanji
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]

Reply With Quote
  #4  
Old February 2nd, 2001, 08:56 AM
dsb dsb is offline
PerlGuy
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2001
Posts: 714 dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 15 h 44 m 20 sec
Reputation Power: 36
Send a message via AIM to dsb
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 );

Reply With Quote
  #5  
Old February 2nd, 2001, 11:26 AM
dark_jurmanji dark_jurmanji is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2001
Location: Denver, CO
Posts: 0 dark_jurmanji User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 sec
Reputation Power: 0
Send a message via ICQ to dark_jurmanji
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?

Reply With Quote
  #6  
Old February 2nd, 2001, 11:55 AM
dsb dsb is offline
PerlGuy
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2001
Posts: 714 dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 15 h 44 m 20 sec
Reputation Power: 36
Send a message via AIM to dsb
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

Reply With Quote
  #7  
Old February 5th, 2001, 11:36 PM
dark_jurmanji dark_jurmanji is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2001
Location: Denver, CO
Posts: 0 dark_jurmanji User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 sec
Reputation Power: 0
Send a message via ICQ to dark_jurmanji
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?

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Simple Array Question


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway