in reply to Perl Script to create jsons(Issue with comma)

Why don't you use JSON to create your output. Instead of struggling with complicated strings, you would only have to create some hashes and let the module do the work for you. For example:

use strict; use warnings; use JSON; my %bookmarks; for (1..3) { push @{$bookmarks{browserBookMarksData}}, { bookmark => "0", c +reated => "01/12/2012" }; } print to_json \%bookmarks;

would create

{"browserBookMarksData":[{"created":"01/12/2012","bookmark":"0"},{"cre +ated":"01/12/2012","bookmark":"0"},{"created":"01/12/2012","bookmark" +:"0"}]}

No need to worry about commata...

Update: The last part of your script could look similar to this:

print "Enter the limit : "; chomp(my $lim = <STDIN>); my %bookmarks; while ( $lim-- ) { push @{$bookmarks{browserBookMarksData}}, { bookMark => $charset[ rand(@charset) ], created => $charset2[ rand(@charset2) ], date => $charset3[ rand(@charset3) ], title => join '', @charset4[map {int rand @charset4} (1..8) +], url => join '', @charset5[map {int rand @charset5} (1..8) +], visits => $charset6[rand(@charset6) ], }; } print to_json \%bookmarks;

Replies are listed 'Best First'.
Re^2: Bug in my perl script
by rahul_lfo (Initiate) on Oct 17, 2013 at 09:30 UTC
    Hi , Thanks for the replies. I updated my json which is as follows--
    use strict; use warnings; use JSON; open(FILE, ">>bookmarkmirror.json"); my %bookmarks; my @charset = ('1','0'); my @charset2 =(''); my @charset3 =(''); my @charset4 = (('A'..'Z'), ('a'..'z')); my @charset5 = (('A'..'Z'), ('a'..'z')); my @charset6 = ('1' .. '1000'); my @charset7 =('http://www.','https://www.'); my @charset8=('.com','.co','.in','.info','.org','.net','.biz','.us','. +me','.mobi','.co.in','.firm.in','.gen.in','.ind.in','.net.in','.org.i +n','.tv','.ag','.am','.asia','.at','.be','.bz','.ca','.cc','.co.nz',' +.co.uk','.com.ag','.com.au','.com.br','.com.bz','.com.co','.com.es',' +.com.mx','.com.pe','.com.so','.com.tw','.cz','.de','.es','.eu','.fm', +'.fr','.gs','.idv.tw','.it','.jobs','.jp','.la','.me.uk','.ms','.mx', +'.net.ag','.net.au','.net.br','.net.bz','.net.co','.net.nz','.net.pe' +,'.net.so','.nl','.nom.co','.nom.es','.nom.pe','.org.ag','.org.au','. +org.es','.org.nz','.org.pe','.org.so','.org.tw','.org.uk','.pe','.se' +,'.so','.tk','.tw','.ws','.xxx'); my $i=0; print "Enter the limit : "; chomp(my $lim = <STDIN>); while ( $lim-- ) { push @{$bookmarks{browserBookMarksData}}, { bookMark => $charset[ rand(@charset) ], created => $charset2[ rand(@charset2) ], date => $charset3[ rand(@charset3) ], title => join '', @charset4[map {int rand @charset4} (1..8) +], url => join '', @charset5[map {int rand @charset5} (1..8) +], visits => $charset6[rand(@charset6) ], }; } print FILE to_json \%bookmarks; close(FILE);
    The ouput is pasted below-

    {"browserBookMarksData":{"created":"","bookMark":"1","date":"","title":"YvJzwzBRurlOgWzGkJEvisits843"},{"created":"","bookMark":"1","date":"","title":"COubQkTRurlfVbzdDGUvisits393"}}

    Now the problem is that the url and visit fields get merged .There is no comma seperation between there the url and the visits fields and also how to join the three character set ie. charset4 ,charset7 and charset 8 to make the url field.

      In order to avoid join to consume the rest of the list, try

      title => join( '', @charset4[map {int rand @charset4} (1..8 +)]), url => join( '', @charset5[map {int rand @charset5} (1..8 +)]),
        Thanks Perl GOD ..You are awesome...
        Hi Monks, Just another query that can we use push for creating multiple beans. So that the output can be as given below-
        {"contactsData":[{"m_UID":"963","m_display_name":"Lynne","m_emailList" +:[{"m_emailID":"jhs@email.com","m_emailLabel":"","m_emailType":"2"}], +"m_eventList":[{"m_startDate":"1991-10-25","m_eventLabel":"","m_etype +":"1"}],"m_firstName":"Lynne","m_imList":[{"m_CustomProtocol":"test", +"m_CustomType":"","m_imAddress":"Lynne","m_imProtocol":"-1","m_imType +":"3"}],"m_isStarred":"1","m_lastName":"ROWLAND","m_middleName":"Lynn +e","m_namePrefix":"","m_nameSuffix":"","m_nickname":"Lynne","m_note": +"","m_organizationList":[{"m_JobDepartment":"","m_JobDescription":"", +"m_OfficeLocation":"Ruzhou","m_OfficePhoneticName":"","m_OfficeSymbol +":"","m_TypeLabel":"test","m_organizationName":"Rio Tinto Group","m_t +itle":"","m_type":"0"}],"m_phoneNumberList":[{"m_label":"","m_number" +:"9841003417","m_type":"21"}],"m_phonetic_fname":"Lynne","m_phonetic_ +lname":"Lynne","m_phonetic_mname":"Lynne","m_photoBitmap":"","m_posta +lAddresses":[{"m_address":"787-4199 Orci Avenue","m_label":"","m_regi +on":"West Africa","m_city":"Ruzhou","m_country":"Guinea-bissau","m_ne +ighbourhood":"","m_PoBox":"251861","m_street":"","m_zipcode":"444610" +,"m_type":"1"}],"m_source":"Google","m_vPredefinedTags":"1","websiteL +ist":[{"m_website":"","m_websiteLabel":"","m_websiteType":"3"}],"rela +tionship":[{"m_relationLabel":"","m_relationName":"ROWLAND","m_relati +onType":"6"}],"socialList":[{"m_Type":"0","m_ProfileName":"ROWLAND"," +m_CustomProfileName":"test"}],"contactMatching":"true"} ]}
        Thanks it Worked.