0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

paiza.ioでperl その7

Posted at

概要

paiza.ioでperlやってみた。
練習問題やってみた。

練習問題

qiita apiを叩いて、jsonをパースせよ。

サンプルコード

use Encode;
use JSON::PP ();
use LWP::UserAgent;
use URI;

$url = URI->new("https://qiita.com/api/v2/items");
$url->query_form("query", "neosvr");
my $ua = LWP::UserAgent->new;
$ua->agent("Dummy User Agent");
my $response = $ua->get($url);
if ($response->is_success) 
{
    my $items = JSON::PP::decode_json($response->content);
    for my $item (@$items) 
    {
       print("$item->{url} $item->{title}\n");
    }
} 
else 
{
    print 'failed';
}

#'title', 'id', 'created_at', 'updated_at','likes_count', 
#'comments_count', 'tags_str', 'user_id', 'user_permanent_id', 'url'


my $in_json = '[{"name":"Perlテキスト処理プログラミング入門","author_name":"木本裕紀","price":3190,"issued_date":"2021-01-03","id":1},{"id":2,"issued_date":"2021-01-03","name":"Web開発をやるぜPerl","author_name":"木本裕紀","price":2200},{"id":3,"issued_date":"2019-03-06","author_name":"田中太郎","name":"データベース入門","price":2090}]';
my $books = JSON::PP::decode_json($in_json);
for my $book (@$books) 
{
    my $id = $book->{id};
    my $name = $book->{name};
    my $price = $book->{price};
    my $issued_date = $book->{issued_date};
    my $price_zeikomi = $price * 1.1;
    $book->{price} = $price_zeikomi;
    print ("$id, $name, $price\n");
}


実行結果

https://qiita.com/ohisama@github/items/87ebaaff87383b611e67 neosでlogix その116
https://qiita.com/ohisama@github/items/c169ebfc77fe4557cad8 neosでlogix その115
https://qiita.com/ohisama@github/items/4466c74caf0e97421b3e neosでlogix その114
https://qiita.com/ohisama@github/items/53d3b0539cb37e828fe6 neosでlogix その113
https://qiita.com/ohisama@github/items/a6707afb09ae3fba7303 neosでlogix その112
https://qiita.com/ohisama@github/items/4d9057da8c197af6fce7 Keian wizbook KZG14X128BK 買ってみた。その2
https://qiita.com/ohisama@github/items/8bb4106b9f9ea1914c13 neosでlogix その111
https://qiita.com/ohisama@github/items/09c63827c9934944f56a neosでlogix その110
https://qiita.com/ohisama@github/items/bba84bc489a2753c9cc8 neosでlogix その108
https://qiita.com/ohisama@github/items/09bcd4296c00a9009815 neosでlogix その107
https://qiita.com/ohisama@github/items/8e7b92b1d22f2795bee4 neosでlogix その106
https://qiita.com/ohisama@github/items/2a89e3f37a48d5eb6f50 neosでlogix その105
https://qiita.com/ohisama@github/items/601df91891a390762951 neosでlogix その104
https://qiita.com/ohisama@github/items/00e91baad8d7e4dabff4 neosでlogix その103
https://qiita.com/ohisama@github/items/8686ae508c321fc05aa8 neosでlogix その102
https://qiita.com/ohisama@github/items/14fafeec2b5bc1251748 neosでlogix その101
https://qiita.com/ohisama@github/items/0ccfe30795664375c811 neosでlogix その100
https://qiita.com/ohisama@github/items/db9b63324eded0751f9b neosでlogix その99
https://qiita.com/ohisama@github/items/2a54291b387dbd7fff01 neosでlogix その98
https://qiita.com/ohisama@github/items/22c10e8cced8d6315371 neosでlogix その97
1, Perlテキスト処理プログラミング入門, 3190
2, Web開発をやるぜPerl, 2200
3, データベース入門, 2090

成果物

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?