CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
chomp

 



itmajors
Novice

Jul 23, 2014, 11:12 AM

Post #1 of 8 (399 views)
chomp Can't Post

I'm new at perl and I need to update a flat file database pipe delimited, i'm using the following code it has 300 fields...

chomp $data;
($v000,$v001,$v002,$v003,$v004,$v005,$v006,$v007,$v008,$v009,$v010,$v011,$v012,$v013,$v014,$v015,$v016,$v017,$v018,$v019,$v020,$v021,$v022,$v023,$v024,$v025,$v026,$v027,$v028,$v029,$v030,$v031,$v032,$v033,$v034,$v035,$v036,$v037,$v038,$v039,$v040,$v041,$v042,$v043,$v044,$v045,$v046,$v047,$v048,$v049,$v050,$v051,$v052,$v053,$v054,$v055,$v056,$v057,$v058,$v059,$v060,$v061,$v062,$v063,$v064,$v065,$v066,$v067,$v068,$v069,$v070,$v071,$v072,$v073,$v074,$v075,$v076,$v077,$v078,$v079,$v080,$v081,$v082,$v083,$v084,$v085,$v086,$v087,$v088,$v089,$v090,$v091,$v092,$v093,$v094,$v095,$v096,$v097,$v098,$v099,$v100,$v101,$v102,$v103,$v104,$v105,$v106,$v107,$v108,$v109,$v110,$v111,$v112,$v113,$v114,$v115,$v116,$v117,$v118,$v119,$v120,$v121,$v122,$v123,$v124,$v125,$v126,$v127,$v128,$v129,$v130,$v131,$v132,$v133,$v134,$v135,$v136,$v137,$v138,$v139,$v140,$v141,$v142,$v143,$v144,$v145,$v146,$v147,$v148,$v149,$v150,$v151,$v152,$v153,$v154,$v155,$v156,$v157,$v158,$v159,$v160,$v161,$v162,$v163,$v164,$v165,$v166,$v167,$v168,$v169,$v170,$v171,$v172,$v173,$v174,$v175,$v176,$v177,$v178,$v179,$v180,$v181,$v182,$v183,$v184,$v185,$v186,$v187,$v188,$v189,$v190,$v191,$v192,$v193,$v194,$v195,$v196,$v197,$v198,$v199,$v200,$v201,$v202,$v203,$v204,$v205,$v206,$v207,$v208,$v209,$v210,$v211,$v212,$v213,$v214,$v215,$v216,$v217,$v218,$v219,$v220,$v221,$v222,$v223,$v224,$v225,$v226,$v227,$v228,$v229,$v230,$v231,$v232,$v233,$v234,$v235,$v236,$v237,$v238,$v239,$v240,$v241,$v242,$v243,$v244,$v245,$v246,$v247,$v248,$v249,$v250,$v251,$v252,$v253,$v254,$v255,$v256,$v257,$v258,$v259,$v260,$v261,$v262,$v263,$v264,$v265,$v266,$v267,$v268,$v269,$v270,$v271,$v272,$v273,$v274,$v275,$v276,$v277,$v278,$v279,$v280,$v281,$v282,$v283,$v284,$v285,$v286,$v287,$v288,$v289,$v290,$v291,$v292,$v293,$v294,$v295,$v296,$v297,$v298,$v299,$v300) = split(/\|/, $data);
$propertiesvar .= qq~$v000|$v001|$v002|$v003|$v004|$v005|$v006|$v007|$in{'p_address_2'}|$v009|$v010|$v011|$v012|$v013|$v014|$v015|$v016|$v017|$v018|$v019|$v020|$v021|$v022|$v023|$v024|$v025|$v026|$v027|$v028|$v029|$v030|$v031|$v032|$v033|$v034|$v035|$v036|$v037|$v038|$v039|$v040|$v041|$v042|$v043|$v044|$v045|$v046|$v047|$v048|$v049|$v050|$v051|$v052|$v053|$v054|$v055|$v056|$v057|$v058|$v059|$v060|$v061|$v062|$v063|$v064|$v065|$v066|$v067|$v068|$v069|$v070|$v071|$v072|$v073|$v074|$v075|$v076|$v077|$v078|$v079|$v080|$v081|$v082|$v083|$v084|$v085|$v086|$v087|$v088|$v089|$v090|$v091|$v092|$v093|$v094|$v095|$v096|$v097|$v098|$v099|$v100|$v101|$v102|$v103|$v104|$v105|$v106|$v107|$v108|$v109|$v110|$v111|$v112|$v113|$v114|$v115|$v116|$v117|$v118|$v119|$v120|$v121|$v122|$v123|$v124|$v125|$v126|$v127|$v128|$v129|$v130|$v131|$v132|$v133|$v134|$v135|$v136|$v137|$v138|$v139|$v140|$v141|$v142|$v143|$v144|$v145|$v146|$v147|$v148|$v149|$v150|$v151|$v152|$v153|$v154|$v155|$v156|$v157|$v158|$v159|$v160|$v161|$v162|$v163|$v164|$v165|$v166|$v167|$v168|$v169|$v170|$v171|$v172|$v173|$v174|$v175|$v176|$v177|$v178|$v179|$v180|$v181|$v182|$v183|$v184|$v185|$v186|$v187|$v188|$v189|$v190|$v191|$v192|$v193|$v194|$v195|$v196|$v197|$v198|$v199|$v200|$v201|$v202|$v203|$v204|$v205|$v206|$v207|$v208|$v209|$v210|$v211|$v212|$v213|$v214|$v215|$v216|$v217|$v218|$v219|$v220|$v221|$v222|$v223|$v224|$v225|$v226|$v227|$v228|$v229|$v230|$v231|$v232|$v233|$v234|$v235|$v236|$v237|$v238|$v239|$v240|$v241|$v242|$v243|$v244|$v245|$v246|$v247|$v248|$v249|$v250|$v251|$v252|$v253|$v254|$v255|$v256|$v257|$v258|$v259|$v260|$v261|$v262|$v263|$v264|$v265|$v266|$v267|$v268|$v269|$v270|$v271|$v272|$v273|$v274|$v275|$v276|$v277|$v278|$v279|$v280|$v281|$v282|$v283|$v284|$v285|$v286|$v287|$v288|$v289|$v290|$v291|$v292|$v293|$v294|$v295|$v296|$v297|$v298|$v299|$v300\n~;

If finds the record and updates everything blank the only field it writes is the field #8 ($in{'p_address_2'}) that i'm replacing on the database... all outher fields are blank...

Does chomp has a limitation on the number of fields it can handle or is there a better way to do the same process above?


FishMonger
Veteran / Moderator

Jul 23, 2014, 12:21 PM

Post #2 of 8 (389 views)
Re: [itmajors] chomp [In reply to] Can't Post

I don't mean this as an insult, but that is a very ugly way to deal with that data.

You should be using the Text::CVS_XS module.
http://search.cpan.org/~hmbrand/Text-CSV_XS-1.09/CSV_XS.pm

Here's a short example how to parse a csv (or pipe delimited) text file altering the 9th field (index #8) and then output that line.

$fh is the filehandle for the input data and $out is the filehandle for the output. This could easily be adjusted to assign the row to another var instead of printing it.

Code
my $csv = Text::CSV_XS->new({ binary => 1, sep_char => '|', eol => "\n"}); 

while (my $row = $csv->getline($fh)) {
$row->[8] = $in{'p_address_2'};
$csv->print($out, $row);
}



itmajors
Novice

Jul 23, 2014, 1:04 PM

Post #3 of 8 (383 views)
Re: [FishMonger] chomp [In reply to] Can't Post

FishMonger, Thank you for your quick reply...

How would I match the line I need to update? I have a record ID on field 0 SO i need to compare a ID to Field0 and them update field 9 for that line...

Thank for your help..


FishMonger
Veteran / Moderator

Jul 23, 2014, 1:22 PM

Post #4 of 8 (376 views)
Re: [itmajors] chomp [In reply to] Can't Post


Code
while (my $row = $csv->getline($fh)) { 
if ($row->[0] eq $id) {
$row->[8] = $in{'p_address_2'};
}
$csv->print($out, $row);
}



itmajors
Novice

Jul 23, 2014, 1:24 PM

Post #5 of 8 (374 views)
Re: [FishMonger] chomp [In reply to] Can't Post

THANK YOU!!! FishMonger

That's much easier than what I had... I'm installing the perl module now...


FishMonger
Veteran / Moderator

Jul 23, 2014, 1:25 PM

Post #6 of 8 (373 views)
Re: [itmajors] chomp [In reply to] Can't Post

Another option would be to use the DBD::CSV module and use sql statements to make the changes.

http://search.cpan.org/~hmbrand/DBD-CSV-0.41/lib/DBD/CSV.pm


itmajors
Novice

Jul 23, 2014, 1:54 PM

Post #7 of 8 (369 views)
Re: [FishMonger] chomp [In reply to] Can't Post

I'm having trouble here is a simple script that I put together with your code but it's not running it gives me an error
Can't locate object method "getline" via package "IO::Handle" at update.cgi line 10

What I'm doing wrong here...

#!/usr/bin/perl
# --------------------------------------------
print "Content-type: text/html\n" ;
print "Pragma: no-cache\n\n";
use Text::CSV_XS;
$id = "832333";
$in{'p_address_2'} = "Test 123";
my $fh = Text::CSV_XS->new({ binary => 1, sep_char => '|', eol => "\n"});
open my $csv, "<", "./Admin/data/bims-properties.db" or die "./Admin/data/bims-properties.db: $!";
while (my $row = $csv->getline($fh)) {
if ($row->[0] eq $id) {
$row->[8] = $in{'p_address_2'};
print qq~ $row->[8]~;
}
$csv->print($out, $row);
}
close $fh or die "./Admin/data/bims-properties.db: $!";
print qq~ end~;

exit;


FishMonger
Veteran / Moderator

Jul 23, 2014, 4:32 PM

Post #8 of 8 (360 views)
Re: [itmajors] chomp [In reply to] Can't Post

Please use the code tags when posting your code.

First, you're missing 2 pragmas (strict and warnings) which should be in every script. Your script should begin like this:

Code
#!/usr/bin/perl 

use strict;
use warnings;
use Text::CSV_XS;


The strict pragma will require you to declare your vars, which is done with the 'my' keyword.

The main reason your script failed was because you flipped the vars in the $fh and $csv assignments and you forgot to open the $out filehandle.

Here's an adjusted version, which still could use additional improvements.

Code
#!/usr/bin/perl 

use strict;
use warnings;
use Text::CSV_XS;

# --------------------------------------------
print "Content-type: text/html\n" ;
print "Pragma: no-cache\n\n";

my $id = 832333;
my %in = ('p_address_2' => "Test 123");
my $csv = Text::CSV_XS->new({ binary => 1, sep_char => '|', eol => "\n"});

open my $fh, "<", "./Admin/data/bims-properties.db"
or die "./Admin/data/bims-properties.db: $!";

open my $out, ">", "./Admin/data/new_bims-properties.db"
or die "./Admin/data/bims-properties.db: $!";

while (my $row = $csv->getline($fh)) {
if ($row->[0] == $id) {
$row->[8] = $in{'p_address_2'};
print qq~ $row->[8]~;
}
$csv->print($out, $row);
}

close $fh or die "./Admin/data/bims-properties.db: $!";
close $out or die "./Admin/data/new_bims-properties.db: $!";

rename('./Admin/data/new_bims-properties.db',
'./Admin/data/bims-properties.db');

print qq~ end~;

exit;


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives