Examine your output to narrow down the line of code that adds the unwanted commas

Simplify/neaten your output so you can notice which comma is the unwanted one

ddumperBasic debugging checklist Basic debugging checklist item 4, and perltidyBasic debugging checklist Basic debugging checklist item 10

Your program with perltidy -olq -csc -csci=10 -cscl="sub : BEGIN END if " -otr -opr -ce -nibc -i=4 -pt=0 "-nsak=*"

#!/usr/bin/perl -- #~ .../buginmy.pl #~ 2013-10-16-23:50:42 #~ #~ ## perltidy -olq -csc -csci=10 -cscl="sub : BEGIN END if " -otr -opr +-ce -nibc -i=4 -pt=0 "-nsak=*" #!/usr/bin/perl -- use strict; use warnings; #charset below would be 1 and in future 1 for bookmark and 0 for hist +ory item 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.in', '.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 @charset9 = ( '":"' ); my @charset40 = ( '"' ); my @charset41 = ( ',' ); my @charset42 = ( '{"browserBookMarksData":[' ); my @charset43 = ( ']}' ); my @charset51 = ( '{' ); my $i = 0; my $prefix; my $suffix; print "Enter the limit : "; chomp( my $lim = <STDIN> ); { $prefix .= $charset42[ rand( @charset42 ) ]; } { $suffix .= $charset43[ rand( @charset43 ) ]; } #~ open( FILE, ">>bookmarkmirror.json" ); *FILE = *STDOUT; print FILE ( "$prefix" ); while( $lim-- ) { my $bookMark; my $created; my $date; my $title; my $url; my $www; my $com; my $visits; my $quotes; my $comma; my $start; my $end; my $curly; my $extra; { $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 ) ]; } #if ($EmailType == '0'){ #$m_emailLabel .="test";} #else {$m_emailLabel .="";} { $quotes .= $charset40[ rand( @charset40 ) ]; } { $comma .= $charset41[ rand( @charset41 ) ]; } { $curly .= $charset51[ rand( @charset51 ) ]; } { $www .= $charset7[ rand( @charset7 ) ]; } { $com .= $charset8[ rand( @charset8 ) ]; } { $extra .= $charset9[ rand( @charset9 ) ]; } print FILE ( "$curly" . "\"bookMark\"\:" . "$quotes" . "$bookMark" . "$quotes" . "$comma", "\"created\"\:" . "$quotes" . int( rand( 10000000000000 ) ) . "$quotes" . "$comma", "\"date\"\:" . "$quotes" . int( rand( 10000000000000 ) ) . "$quotes" . "$comma", "\"title" . "\"\:\"$title\"" . "$comma", "\"url" . "$extra" . "$www" . "$url" . "$com\"" . "$comma", "\"visits" . "\"\:\"$visits\"\}\ " . if( $lim != $i ) { "$comma"; } ## end if ); if( $lim == '1' ) { print FILE ( "," ); } $i++; } print FILE ( "$suffix" ); close( FILE );

While you're at it you might fix the syntax errors (if inside print)

And get rid of the extra curly braces ... and the seperation of declaration and initialization

my $bookMark = $charset[ rand( @charset ) ];

And the back slashing -- the battle of Hastings was very long ago, use alternate delimiters

print FILE ( qq{ { "bookMark": "$bookMark", "created": "$rand1", "date": "$rand2", "title": "$title", "url": "$www$url$com", "visits": "$visits" } } );

STDIN is a non-fun way to pass a single number, use @ARGV with a default

my $lim = int( shift || 3 ); warn "# The limit is $lim\n";

Don't make variables out of things that aren't variable, get rid of $comma/$prefix/$sufix/$curly

This ought to get you 99% there leaving only the comma logic which ought to be more apparent now

JSON parsers don't care if the output is tidy, humans however DO

jsontidy

#!/usr/bin/perl -- use Path::Tiny; use JSON; Main( @ARGV ); exit( 0 ); sub Main { my $in = shift or die "\nUsage: $0 foo.json > foo-tidy.json\n"; binmode STDOUT, ':encoding(UTF-8)'; print JSON->new->utf8(1)->pretty(1)->encode( JSON->new->decode( path( $in )->slurp ) ); }

In reply to Re: Bug in my perl script (JSON @ARGV jsontidy) by Anonymous Monk
in thread Perl Script to create jsons(Issue with comma) by rahul_lfo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.