rahul_lfo has asked for the wisdom of the Perl Monks concerning the following question:

#!/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.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 @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"); 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"."$quo +tes"."$comma","\"created\"\:"."$quotes".int(rand(10000000000000))."$q +uotes"."$comma","\"date\"\:"."$quotes".int(rand(10000000000000))."$qu +otes"."$comma","\"title"."\"\:\"$title\""."$comma","\"url"."$extra"." +$www"."$url"."$com\""."$comma","\"visits"."\"\:\"$visits\"\}\ ". if ( +$lim != $i){"$comma"}); if($lim == '1') { print FILE (","); } $i++; } print FILE ("$suffix"); close(FILE);
Output of the File----
{"browserBookMarksData":[{"bookMark":"0","created":"5663146972656","da +te":"6965942382812","title":"QQjVYxgf","url":"https://www.ySLjEGzo.or +g","visits":"783"} ,,{"bookMark":"1","created":"3386840820312","date" +:"4520874023437","title":"GTHCiPLs","url":"http://www.cmBoOQmy.pe","v +isits":"946"} ,]}

The problem is that I am getting two unwanted comma's,which is at the end of the json created and the last bean. Please help how to eliminate these unwanted comma's.

Replies are listed 'Best First'.
Re: Bug in my perl script
by hdb (Monsignor) on Oct 17, 2013 at 07:40 UTC

    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;
      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 +)]),
Re: Bug in my perl script (JSON @ARGV jsontidy)
by Anonymous Monk on Oct 17, 2013 at 07:34 UTC

    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=*"

    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 ) ); }
Re: Perl Script to create jsons(Issue with comma)
by davido (Cardinal) on Oct 17, 2013 at 14:41 UTC

    Is this JSON producer a learning exercise, or is it to create a JSON encoder that does something the others don't? Is the issue that you're not allowed to install 3rd party modules? I'm just curious. There may be a more robust solution that consumes less work than the path you're currently on. Even if your specification is to not use any modules from CPAN, modules such as JSON::Tiny are set up to allow copy/paste embedding if your situation requires it.


    Dave