Wednesday, 20 July 2016

PERL MERGE JSON

Merging multiple JSON files into one using JSON::XS IN PERL


$json1 =

[
   {
      "name" : "bob",
      "title" : "janitor",
      "email" : "",
      "iq" : "180",
      "favorite_food" : "wagyu steak"
   },
   {
      "name" : "joe",
      "title" : "software engineer",
      "email" : "",
      "iq" : "80",
      "favorite_food" : "raw hamburger"
   }
]
 
$json2 =  
{
   "People" : [
      {
         "name" : "bob",
         "title" : "janitor",
         "email" : "",
         "iq" : "180",
         "favorite_food" : "wagyu steak"
      },
      {
         "name" : "joe",
         "title" : "software engineer",
         "email" : "",
         "iq" : "80",
         "favorite_food" : "raw hamburger"
      },
      {
         "name" : "sandy",
         "title" : "dishwasher",
         "email" : "",
         "iq" : "240",
         "favorite_food" : "filet mignon"
      },
      {
         "name" : "george",
         "title" : "software engineer",
         "email" : "",
         "iq" : "14",
         "favorite_food" : "tacos"
      }
   ]   
}

CODE IS,
 
my @fields;
my $uuts = {};
while(<$fh>) {
    chomp;
    next if !-e "/tmp/files/$_.json";
    my $decoded = decode_json( read_file("/tmp/files/$_.json") );
    push @fields, $decoded;
}
$uuts->{People} = [ @fields ];
[download]

to just this:

my $uuts;
while (<$fh>) {
    chomp;
    next if ! -e "/tmp/files/$_.json";
    push @{$uuts->{People}}, @{decode_json(read_file("/tmp/files/$_.js
+on"))};
}  
 
I'm not really in a position to test that; however, here's a small test that shows the technique involved:

$ perl -Mstrict -Mwarnings -le '
    use Data::Dumper;
    my @jsons = ([{a=>1}, {b=>2}], [{c=>3}, {d=>4}]);
    my $combo;
    for (@jsons) {
        print Dumper $_;
        push @{$combo->{People}} => @$_;
    }
    print Dumper $combo;
'
$VAR1 = [
          {
            'a' => 1
          },
          {
            'b' => 2
          }
        ];

$VAR1 = [
          {
            'c' => 3
          },
          {
            'd' => 4
          }
        ];

$VAR1 = {
          'People' => [
                        {
                          'a' => 1
                        },
                        {
                          'b' => 2
                        },
                        {
                          'c' => 3
                        },
                        {
                          'd' => 4
                        }
                      ]
        };

Wednesday, 27 April 2016

Perl Basics

Perl Basics has two main goals. First, it provides a succinct summary of major Perl elements. Second, it provides perspective and relates features to one another. Thus, you may think of it as an extended and structured checklist, with commentary.
The discussion is oriented toward answering two questions:
  • What are the things Perl provides you to work with?
  • What can you do to those things?
The discussion includes six major sections:
  1. Variables and their Related Operators
  2. Control structures
  3. Functions
  4. Regular Expressions
  5. Input/Output
  6. System Operators

1. Variables and their Related Operators

Perl provides three kinds of variables: scalars, arrays, and associative arrays. The discussion includes the designation of these three types and the basic operators provided by Perl for their manipulation.

2. Control Structures

Perl is an iterative language in which control flows from the first statement in the program to the last statement unless something interrupts. Some of the things that can interrupt this linear flow are conditional branches and loop structures. Perl offers approximately a dozen such constructs. Each of these basic constructs are described along with examples illustrating their use.

3. Functions

Functions are a fundamental part of most programming languages. They often behave like an operator, producing a change in the value of some variable or returning a value that can be assigned to a variable. They also control the flow of execution, transferring control from the point of invocation to the function definition block and back. Thus, they combine properties of the two preceding discussions. The discussion will cover both the designation of functions and their invocation and use.

4. Regular Expressions and Related Operators

Regular expressions are strings that can be recognized by a regular grammar, a restricted type of context-free grammar. Basically, they are strings that can be parsed left to right, without backtracking, and requiring only exact symbol matching, matching of a symbol by a category of symbols, or matching of a symbol by a specified number of sequential occurrences of a symbol or category. Perl provides a general mechanism for specifying regular expressions. It also provides several operators that manipulate strings based upon the evaluation of a regular expression.
The discussion will begin by describing the various mechanism for specifying patterns and then discuss expression-based operators.

5. Input/Output

Perl provides basic I/O for both the standard input (keyboard) and output (display) devices and for files in the UNIX file system. More sophisticated I/O is provided through the UNIX DBM library. These various I/O capabilities are discussed.

6. System Operators

Perl offers a number of operators that mimic or call UNIX system operators or analogous operators for other operating systems. The discussion here will be cast in the context of UNIX and will assume familiarity with basic UNIX facilities. Perl system operators can be roughly divided into two large categories: file/directory operators and process operators. Both types are discussed.

Wednesday, 30 March 2016

What is Memcached?

Free & open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.
Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.
Memcached is simple yet powerful. Its simple design promotes quick deployment, ease of development, and solves many problems facing large data caches. Its API is available for most popular languages.


Quick Example

Cache Results

function get_foo(foo_id)
    foo = memcached_get("foo:" . foo_id)
    return foo if defined foo

    foo = fetch_foo_from_database(foo_id)
    memcached_set("foo:" . foo_id, foo)
    return foo
    end

Play with telnet

$ telnet localhost 11211
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
get foo
VALUE foo 0 2
hi
END
stats
STAT pid 8861
(etc)

Need more information? Check out --> https://memcached.org/about

Wednesday, 10 February 2016

Perl Programming.. How to Use Regular Expressions in your code by prem 10-2-2016.


Qus : Here given one date 2016-02-10. How to convert  date-month-year format via perl programming?
(for example 2016-02-10 to 10-02-2016).


$date = "2012-10-02";

if($date =~ m/(\d{4})\-(\d{2})\-(\d{2})/))
{
    $year = $1;
    $month =$2;
    $date = $3;
    $converted_date = $date . "-" . $month . "-" . $year;
    print "$converted_date\n";
  }

----------------------------------program completed-------------------------------------------


Here $1,$2 and $3 is special varibles which contains group values from regular expressions. \d is refers to digits and \d{4} is refers to digits should be contain with 4 digit.