snehit.ar has asked for the wisdom of the Perl Monks concerning the following question:

It might be simple logic for someone ,but for me its not.So please bare with me.
Please consider array with 14 records .In FOR loop want to Add wait condition of (10sec), if my array count reach 10 push record(10)to print and then wait for 10sec and then push only remaining 4 records to print.Now i am just displaying all records in one shot.Below is my code with json

#!/usr/bin/env perl use warnings; use strict; use Data::Dumper; binmode STDOUT, ":utf8"; use utf8; use JSON; my @records; my $json; { local $/; open my $fh, "<", "reddataout.json"; $json = <$fh>; close $fh; } @records = @{decode_json($json)}; #sort the array @records and assign line number # @records = sort{ $b->{event_age} <=> $a->{event_age} } @records; for my $i (0 .. $#records) { $records[$i]->{line} = $i + 1; } #End# print Dumper \@records;

reddataout.json

[{"ticketnum":"123","event_age":1,"visible":"Yes"},{"ticketnum":"32"," +event_age":2,"visible":"Yes"},{"ticketnum":"98","event_age":3,"visibl +e":"Yes"},{"ticketnum":"45","event_age":4,"visible":"Yes"},{"ticketnu +m":"65","event_age":5,"visible":"Yes"},{"ticketnum":"67","event_age": +6,"visible":"Yes"},{"ticketnum":"56","event_age":7,"visible":"Yes"},{ +"ticketnum":"123","event_age":8,"visible":"Yes"},{"ticketnum":"123"," +event_age":9,"visible":"Yes"},{"ticketnum":"09","event_age":10,"visib +le":"Yes"},{"ticketnum":"16","event_age":11,"visible":"Yes"},{"ticket +num":"32","event_age":12,"visible":"Yes"},{"ticketnum":"123","event_a +ge":13,"visible":"Yes"},{"ticketnum":"123","event_age":14,"visible":" +Yes"}]

Thank you

Replies are listed 'Best First'.
Re: Break Array loop
by hippo (Archbishop) on Nov 02, 2017 at 12:12 UTC

    The number 10 appears 4 times in your description of the problem but nowhere in your code. Why is this?

    Why are you loading HTTP::Request, LWP::UserAgent, LWP::Simple, etc. when you never refer to them? See Short, Self-Contained, Correct Example - these should be absent, as should your file operations. As it stands your sample code is apparently entirely unrelated to your stated problem.

    Update: The OP has silently changed the parent post as opposed to How do I change/delete my post?

Re: Break Array loop
by Discipulus (Canon) on Nov 02, 2017 at 12:12 UTC
    Hello snehit.ar

    i dunno if I understood it but:

    for my $i (0 .. $#records) { # check here sleep (10) if $i == 11; $records[$i]->{line} = $i + 1; # print HERE not outside the loop }

    You basically check if you are processing the 11th element and sleep.

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Break Array loop
by 1nickt (Canon) on Nov 02, 2017 at 12:49 UTC

    Hi, not much mystery to sleep ...

    perl -Mstrict -wlE ' my @x = qw/ a b c d e f g h i j k l m n /; my $count = 0; do { sleep 10 if $count++ == 10; say "$_ " . time } for @x ' a 1509626827 b 1509626827 c 1509626827 d 1509626827 e 1509626827 f 1509626827 g 1509626827 h 1509626827 i 1509626827 j 1509626827 k 1509626837 l 1509626837 m 1509626837 n 1509626837


    The way forward always starts with a minimal test.