473,320 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Help with splitting up a input file.

17
I have a text file with a list of servers and then a description of the server along with the applications that may be hosted on them. Here is a example

Expand|Select|Wrap|Line Numbers
  1. SERVER01
  2. SERVER01 Windows Server 2003 RC2. SQL server. Runs blah blah..
  3. blah. and so forth.
  4.  
  5. SERVER02
  6. SERVER02 REDHAT Linux. MAIL etc etc etc
I am trying to open up the file and parse it into an array that will only list the SERVER Names and then a hash that will have the SERVER name as the key and the description as the hash value.

I have a start of this and am trying to use a regex expression to at least find and add the servers to the array but this simply input the entire contents of the text file into the array.

Expand|Select|Wrap|Line Numbers
  1. sub ParseInputFile(){
  2. open(SERVERS, "servers.txt") || die "$!";
  3. @contents=<SERVERS>;
  4. close(SERVERS);
  5.  
  6. foreach $server (@contents){
  7.     if($server =~ /\w/){
  8.         my @server_array = (@server_array, $server);
  9.         print "Its added\n";
  10.     }
  11.  
  12. }
I figure If I get this done first i can work at then getting it into a hash or maybe even in the same step I can also split it into the hash value as well. But is there any wildcards to use this for PERL? The \w operator matches any word but how do I split my input into words then put the word into a array and then put it into a hash?

Thanks
Feb 19 '08 #1
9 1603
nithinpes
410 Expert 256MB
I am modifying your foreach loop to accomplish both tasks.

Expand|Select|Wrap|Line Numbers
  1. foreach $server (@contents){
  2.       chomp $server;
  3.       if($server =~ /^(\w+)\s+(.*)$/) {  # grouping first word&remaining words of line
  4.           $sername = $1;
  5.           $desc = $2;
  6.           push @server_array, $sername;  # array of server names
  7.           $server_hash{$sername} = $desc;   #hash of server name=>description
  8.          }    
  9.     }
  10.  
To include accidentally introduced space at the begining, you can modify your regex to:
Expand|Select|Wrap|Line Numbers
  1. if($server =~ /^\s*(\w+)\s+(.*)$/)
  2.  
I am assuming description is not spread over multiple lines.
Feb 19 '08 #2
blazted
17
Thanks for the input. Unfortunately the description can be over multiple lines so that is where I am having my main problems. PERL splits each line of the input file into the array which is what is causing me the most problems. There is a blank line between each description.

I tried your suggestion but it put every word into the array. When I tried removing the chomp operator so I could use whitespace as my end point but this worked better slightly better. But since the whitespace is one or more it thinks that any space in the separation in words is my delimiter character. Is there a way to make the expression 2 or more whitespace?
Feb 19 '08 #3
eWish
971 Expert 512MB
As a side note, one thing that will make your script faster is if you read the file line by line rather than slurping the entire file into memory. If you want to slurp the file I would suggest that you use File::Slurp.

--Kevin
Feb 19 '08 #4
KevinADC
4,059 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3. use Carp;
  4. my %hash = ();
  5. my $flag = 0;
  6. my $desc;
  7. my $key;
  8. open(SERVERS, "servers.txt") or die "$!";
  9. while(<SERVERS>){
  10.    chomp;
  11.    if(/^SERVER\d+$/) {# beginning of hash record
  12.       $key = $_;      
  13.       $hash{$key} = '';
  14.       $flag = 1;
  15.       next;
  16.    }
  17.    if(/^\s*$/) { # end of hash record
  18.       $hash{$key} = $desc;
  19.       $key = ''; 
  20.       $flag = 0;
  21.       $desc = '';
  22.    }
  23.    elsif ($flag) {# value of hash record
  24.       $desc .= "$_ ";
  25.    }
  26.    else {# possible unknown condition
  27.       warn "A line I don't know what to do with: $_";
  28.    }
  29. }
  30. print "$_ $hash{$_}\n" for sort keys %hash;;
  31.  
Feb 19 '08 #5
blazted
17
I got it to work sort of but could not get it to work using a FILEHANDLE. To get my regex correct I simply opened it after assigning a variable to the name of the text file then reading it in. This reads the server name into the hash key and the description as the hash value.

Expand|Select|Wrap|Line Numbers
  1. my %hash = ();
  2. my @array;
  3. my $file = "servers.txt";
  4.  
  5. open($file, "<$file");
  6.  
  7. while (<$file>) {
  8.    last if /^$/;
  9.    my $val = $_;
  10.    $hash{$val} = "";
  11.    push(@array, $_);
  12.  
  13.    while (<$file>) {
  14.      last if /^$/;
  15.      $hash{$val} .= $_;
  16.    }
  17. }
  18.  
  19. foreach (keys %hash) {
  20.    print "$_$hash{$_}\n";
  21.  
  22. }
But I cannot get this to work by using it as a filehandle. I believe this is because of the way it read in with the filehandle.

Expand|Select|Wrap|Line Numbers
  1.  open(SERVERS, "servers.txt") || die "$!";
  2.     my $contents = "<servers.txt";
  3.     close(SERVERS);
  4.  
  5.     my %hash = ();
  6.     my @array;
  7.  
  8.     while (<$contents>) {
  9.         last if /^$/;
  10.         my $desc = $_;
  11.         $hash{$desc} = "";
  12.         push(@array, $_);
  13.  
  14.         while (<$contents>) {
  15.             last if /^$/;
  16.             $hash{$desc} .= $_;
  17.     }
  18.  
  19.     foreach (keys %hash) {
  20.         print "$_$hash{$_}\n";
  21.     }
Feb 20 '08 #6
KevinADC
4,059 Expert 2GB
Comments removed: I posted in the wrong thread, sorry
Feb 20 '08 #7
blazted
17
Never mind I got it. Took me a bit but the problem I was having was the way I was reading the file. I stopped reading it into a array and then searching and simply split it up as it was being read.
Feb 21 '08 #8
eWish
971 Expert 512MB
The servers.txt is just a file, not a filehandle. The $contents variable is not a filehandle either.

Expand|Select|Wrap|Line Numbers
  1. open(my $FILEHANDLE, '<', $text_file) || die "Can't open file $!\n";
  2.      while (<$FILEHANDLE>) {
  3.      ...do somthing here......
  4.      }
  5. close($FILEHANDLE);
perldoc open

--Kevin
Feb 21 '08 #9
KevinADC
4,059 Expert 2GB
Never mind I got it. Took me a bit but the problem I was having was the way I was reading the file. I stopped reading it into a array and then searching and simply split it up as it was being read.

Did you not like the code I posted earlier in the thread or did it not work? You seemed to have ignored it.
Feb 21 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Brice Vissi?re | last post by:
Hello, I would like to handle an XML file structured as following <ROOT> <STEP> .... </STEP> <STEP> .... </STEP>
4
by: Tad Johnson | last post by:
Hi all, I would not normally post about this issue but after a few hours of struggling maybe it's time for some help. I am a pascal programmer moving to C++. I am learning from a couple of...
11
by: MM | last post by:
Hi I have never written any C programs before, but it seems that I need to do so now. Hope some of you out there can spend a few minutes and help me by writing a simple example of something...
2
by: Trint Smith | last post by:
Ok, My program has been formating .txt files for input into sql server and ran into a problem...the .txt is an export from an accounting package and is only supposed to contain comas (,) between...
9
by: Tristán White | last post by:
Hi I am very new to PHP - actually, this is my second day at it, as I've only recently started a new job last week. We're a charity. I have a "No input file selected" problem. A Google search...
1
by: wishbone34 | last post by:
Below is a postfix eval. program i have made, it works fine, however my professor wants me to make a header file with the class, and I tried this class StackType { public: void...
0
by: shrik | last post by:
I have following error : Total giant files in replay configuration file are : File name : /new_file/prob1.rec Given file /new_file/prob1.rec is successfully verified. Splitting for giant file...
6
by: portCo | last post by:
Hello there, I am creating a vb application which is some like like a questionare. Application read a text file which contains many questions and display one question and the input is needed...
3
by: Eric_Dexter | last post by:
I am trying to take some data in file that looks like this command colnum_1 columnum_2 and look for the command and then cange the value in the collum(word) number indicated. I am under...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.