Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Stuck with manipulating an array

by BillKSmith (Monsignor)
on Aug 28, 2017 at 14:34 UTC ( [id://1198173]=note: print w/replies, xml ) Need Help??


in reply to Stuck with manipulating an array

You have not responded to Corion's comment about "good" binning. Is any valid solution "good enough"? Do you have additional criteria, but do not know how to specify them? Consider how you would want to divide the list of integers (0..1001). (Note that there are over 1000 possible solutions using two bins. Far more if more bins are allowed.)
Bill

Replies are listed 'Best First'.
Re^2: Stuck with manipulating an array
by Anonymous Monk on Aug 28, 2017 at 14:40 UTC
    I think the answer/snippet provided by tybalt89 was exactly what I was after...
      Ok, I am basically stuck here:
      #!/usr/bin/perl use Data::Dumper; while(<DATA>) { $all_numbers=$_; chomp $all_numbers; @vector=(); @vector = split(/\@/, $all_numbers); $start; @answer; for ( sort {$a <=> $b} @vector) { if( not defined $start or $_ > $start + 1000 ) { push @answer, [ $_ ]; $start = $_; } else { push @{ $answer[-1] }, $_; } } for $i ( 0 .. $#answer ) { print "$i\t [ @{$answer[$i]} ]\n"; } print "//\n"; } __DATA__ 141326478@103194415@50137382@86004442@86004438@86004434@85280835@17916 +134@85280834@86004437@85280832@53250112@50137387@50137380@29223108@25 +694155@86004431 6901075@6901079@34073753@88911904@34073751@91346449@34073757

      If I only have 1 line of data, it works perfectly, but If I have these 2, it creates this:
      0 [ 17916134 ] 1 [ 25694155 ] 2 [ 29223108 ] 3 [ 50137380 50137382 50137387 ] 4 [ 53250112 ] 5 [ 85280832 85280834 85280835 ] 6 [ 86004431 86004434 86004437 86004438 86004442 ] 7 [ 103194415 ] 8 [ 141326478 ] // 0 [ 17916134 ] 1 [ 25694155 ] 2 [ 29223108 ] 3 [ 50137380 50137382 50137387 ] 4 [ 53250112 ] 5 [ 85280832 85280834 85280835 ] 6 [ 86004431 86004434 86004437 86004438 86004442 ] 7 [ 103194415 ] 8 [ 141326478 6901075 6901079 34073751 34073753 34073757 88911904 +91346449 ] //

      What am I doing wrong?
        Ok, apparently I needed to put:
        undef $start; undef @answer;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1198173]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-19 12:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found