Jim, Thank you for your help!
My perl program contains the following code. I am getting errors when I run the program. Any insight is greatly appreciated. Thank you, Chris 1 #!/usr/bin/perl 2 3 use warnings; 4 use strict; 5 6 #Get data from EVDOPCMD.txt file and output to processed.txt file. 7 8 print "What file do you want to parse?"; 9 $filename = <STDIN>; 10 11 open( my $in, '<', $filename) or die("Can't open $filename for reading: $!"); 12 open( my $out, '>', $outputfile) or die("Can't create file $outputfile: $!); 13 14 #split by line 15 while( my $line = <$in> ) { 16 chomp($line); 17 my @fields = split(/;/,$line); 18 19 20 #Extrac the data you want using array slices: 21 22 my @data = @fields[0,5,44,31,32]; 23 24 #or into named variables: 25 26 #my( $version, $mtype, $rlptxat, $cell, $sector ) = @fields[0,5,44,31,32];@val = split (/;/, $line); 27 28 #Print the headers (one time): 29 30 print $out "Version;MType;RLPtxAT;Cell;Sector\n"; 31 32 #For each record, print the data to the output file: 33 34 print $out join(';',@data), "\n"; 35 } 36 37 close $out; ~ The errors I am getting are: bash-3.2$ ./smart_phone.pl Possible unintended interpolation of @fields in string at ./smart_phone.pl line 12. Possible unintended interpolation of @data in string at ./smart_phone.pl line 12. Possible unintended interpolation of @fields in string at ./smart_phone.pl line 12. Possible unintended interpolation of @fields in string at ./smart_phone.pl line 12. Possible unintended interpolation of @val in string at ./smart_phone.pl line 12. Bareword found where operator expected at ./smart_phone.pl line 30, near "print $out "Version" (Might be a runaway multi-line "" string starting on line 12) Global symbol "$filename" requires explicit package name at ./smart_phone.pl line 9. Global symbol "$filename" requires explicit package name at ./smart_phone.pl line 11. Global symbol "$filename" requires explicit package name at ./smart_phone.pl line 11. Global symbol "$outputfile" requires explicit package name at ./smart_phone.pl line 12. Global symbol "$outputfile" requires explicit package name at ./smart_phone.pl line 12. Global symbol "$line" requires explicit package name at ./smart_phone.pl line 12. Global symbol "$line" requires explicit package name at ./smart_phone.pl line 12. Global symbol "@fields" requires explicit package name at ./smart_phone.pl line 12. Global symbol "$line" requires explicit package name at ./smart_phone.pl line 12. Global symbol "@data" requires explicit package name at ./smart_phone.pl line 12. Global symbol "@fields" requires explicit package name at ./smart_phone.pl line 12. Global symbol "$version" requires explicit package name at ./smart_phone.pl line 12. Global symbol "$mtype" requires explicit package name at ./smart_phone.pl line 12. Global symbol "$rlptxat" requires explicit package name at ./smart_phone.pl line 12. Global symbol "$cell" requires explicit package name at ./smart_phone.pl line 12. Global symbol "$sector" requires explicit package name at ./smart_phone.pl line 12. Global symbol "@fields" requires explicit package name at ./smart_phone.pl line 12. Global symbol "@val" requires explicit package name at ./smart_phone.pl line 12. Global symbol "$line" requires explicit package name at ./smart_phone.pl line 12. Global symbol "$out" requires explicit package name at ./smart_phone.pl line 12. ./smart_phone.pl has too many errors. ________________________________________ From: Jim Gibson [jimsgib...@gmail.com] Sent: Thursday, December 23, 2010 3:50 PM To: Chris Stinemetz; beginners@perl.org Subject: Re: parsing data from txt file At 12:18 PM -0700 12/23/10, Chris Stinemetz wrote: >Hello, > >I hope someone can help me. I am trying to parse data from a txt >file and output the results to a new file with timestamp in the >name of the file. Look at the open function for reading existing files and creating new ones: perldoc -f open open( my $in, '<', $filename) or die("Can't open $filename for reading: $!"); open( my $out, '>', $outputfile) or die("Can't create file $outputfile: $!); (where $outputfile can contain some sort of timestamp). > >The format of the txt file is ";" delimited and is several thousand >records in length. Below is an example of the .txt format. If all fields are delimited with ';' characters, and no field contains a ';' character, you can use a simple split to extract the fields into an array (perldoc -f split): while( my $line = <$in> ) { chomp($line); my @fields = split(/;/,$line); > >PACE | EVDOPCMD | 33.0 | 101218 | 07 | >8;1023240136;1218;0;1;00a000001a2bcdc7;0310003147702376;ac016d4a;;;5.6.128.8;0;;;;;43234169;43234349;;;10000;1;1;;0;;19;5.6.128.22;172.30.151.5;304;3;304;3;;;;;15;175;15;175;15;175;1;1798;1251;0;0;2;19;20;;;;;1;1;1;0;128;5.6.128.8;;;;;;;301;5.6.128.8;;;8;304;3;;;;1;43244037;;;1;18;43234169;0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;1;;;;;;43234416;0;0;304;3;21;19;175;15;405;1;1;1;1;0;125;1;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;|;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;| >8;1023240137;1218;0;1;00a000001db74ace;;ac0174ca;43243423;1678442111;5.6.128.8;1;0;;43242544;43244207;43243423;43243647;;;1000;1;1;;0;;19;5.6.128.26;;372;2;372;2;;43243012;0;43243562;15;175;15;175;15;175;1;;;;;5;48;19;20;49;50;;0;1;2;0;68;5.6.128.8;;;;;;;301;5.6.128.8;;;8;372;2;;;;1;43244207;;;1;18;43243423;0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;1;;;;;;43242544;0;0;372;2;21;19;175;15;177;3;1;0;0;0;68;1;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;|43243753;0;0;372;2;21;19;175;15;177;3;1;1;1;0;71;1;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;| >8;1023240138;1218;0;1;00a000002017ccdb;0310003147833882;aca344d7;;;5.6.128.13;0;;;;;43234160;43234358;;;10000;1;1;;0;;19;5.6.128.31;172.30.151.5;320;2;320;2;;;;;15;75;15;75;15;75;1;2162;1317;0;0;2;19;20;;;;;1;1;1;0;104;5.6.128.13;;;;;;;306;5.6.128.13;;;8;320;2;;;;1;43244164;;;1;18;43234160;0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;1;;;;;;43234404;0;0;320;2;21;19;75;15;279;6;1;1;1;0;64;1;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;|;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;| >8;1023240139;1218;0;1;00a1000009237b21;0310003147774000;aca3141e;;;5.6.128.13;0;;;;;43235644;43235820;;;9000;1;1;;0;;19;5.6.128.19;172.30.151.5;502;1;502;1;;;;;15;175;15;175;15;175;1;48;79;0;0;2;19;20;;;;;1;1;1;0;124;5.6.128.13;;;;;;;306;5.6.128.13;;;8;502;1;;;;1;43244194;;;1;18;43235644;0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;1;;;;;;43235887;0;0;502;1;21;19;175;15;27;1;1;1;1;0;127;1;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;|;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;| > >I am very new to perl so any help is appreciated. > >Ideally I would like to extract the following columns 0, 5, 44, 31, >32, and give them the following headers indicated below. Extract the data you want using array slices: my @data = @fields[0,5,44,31,32]; or into named variables: my( $version, $mtype, $rlptxat, $cell, $sector ) = @fields[0,5,44,31,32]; > > >%fieldmap = ( > "Version" => 0, > "MType" => 5, > "RLPtxAT" => 44, > "Cell" => 31, > "Sector" => 32, >); Print the headers (one time): print $out "Version;MType;RLPtxAT;Cell;Sector\n"; For each record, print the data to the output file: print $out join(';',@data), "\n"; You have not explained where the headers should go, or why there are pipe characters in your input or what they mean, so I have ignored them. -- Jim Gibson j...@gibson.org -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/