<< 1 2 3 4 ... 15 16 17 ... 47 48 49 50 >>
<< 1 2 3 4 5 6 7 ... 47 48 49 50 >>
<< 1 2 3 4 ... 44 45 46 47 48 49 50 >>
(the bold is the selected page)
Is there any cleaver logic out there that creates scaling pagination like this? I have created one of these before but it ended up as a mess of logic statements.
The language i am doing this in now is PHP but if you have examples or tips for any language, it would be appreciated.
By scaling i mean when there are only a few pages. The pagination displays this.
<< 1 2 3 4 5 6 7 >>
As the number of pages grow to a certain point, the pagination stops showing all numbers and starts splitting them up.
<< 1 2 3 4 ... 47 48 49 50 >>
<< 1 2 3 4 5 6 ... 47 48 49 50 >>
<< 1 2 3 4 5 6 7 8 ... 47 48 49 50 >>
<< 1 2 3 4 .. 7 8 9 ... 47 48 49 50 >>
<< 1 2 3 4 .. 15 16 17 ... 47 48 49 50 >>
<< 1 2 3 4 ... 44 45 46 47 48 49 50 >>
<< 1 2 3 4 ... 47 48 49 50 >>
(note, the actual numbers and how many it shows before and after is not relevant)
Sorry for the blob of code but here goes. Hopefully the comments are enough to tell you how it works - if leave a comment and I might add some more.
/**
* Get a spread of pages, for when there are too many to list in a single <select>
* Adapted from phpMyAdmin common.lib.php PMA_pageselector function
*
* #param integer total number of items
* #param integer the current page
* #param integer the total number of pages
* #param integer the number of pages below which all pages should be listed
* #param integer the number of pages to show at the start
* #param integer the number of pages to show at the end
* #param integer how often to show pages, as a percentage
* #param integer the number to show around the current page
*/
protected function pages($rows, $pageNow = 1, $nbTotalPage = 1, $showAll = 200, $sliceStart = 5, $sliceEnd = 5, $percent = 20, $range = 10)
{
if ($nbTotalPage < $showAll)
return range(1, $nbTotalPage);
// Always show the first $sliceStart pages
$pages = range(1, $sliceStart);
// Always show last $sliceStart pages
for ($i = $nbTotalPage - $sliceEnd; $i <= $nbTotalPage; $i++)
$pages[] = $i;
$i = $sliceStart;
$x = $nbTotalPage - $sliceEnd;
$met_boundary = false;
while ($i <= $x)
{
if ($i >= ($pageNow - $range) && $i <= ($pageNow + $range))
{
// If our pageselector comes near the current page, we use 1
// counter increments
$i++;
$met_boundary = true;
}
else
{
// We add the percentate increment to our current page to
// hop to the next one in range
$i = $i + floor($nbTotalPage / $percent);
// Make sure that we do not cross our boundaries.
if ($i > ($pageNow - $range) && !$met_boundary)
$i = $pageNow - $range;
}
if ($i > 0 && $i <= $x)
$pages[] = $i;
}
// Since because of ellipsing of the current page some numbers may be double,
// we unify our array:
sort($pages);
return array_unique($pages);
}
Related
Say I have a file which contains:
22 30 31 3a 31 32 3a 32 " 0 9 : 1 2 : 2
30 32 30 20 32 32 3a 31 1 2 7 2 2 : 1
And, I want to print only the column fields that have string composed of 1 character. I want the output to be like this:
" 0 9 : 1 2 : 2
1 2 7 2 2 : 1
Then, I want to print only those strings that are composed of two characters, the output should be:
22 30 31 3a 31 32 3a 32
30 32 30 20 32 32 3a 31
I am a beginner and I really don't know how to do this. Thanks for your help!
Could you please try following, I am trying it in a different way for provided samples. Written and tested with provided samples only.
For getting values before BULK SPACE try:
awk '
{
line=$0
while(match($0,/[[:space:]]+/)){
arr=arr>RLENGTH?arr:RLENGTH
start[arr]+=RSTART+prev_start
prev_start=RSTART
$0=substr($0,RSTART+RLENGTH)
}
var=substr(line,1,start[arr]-1)
sub(/ +$/,"",var)
print var
delete start
var=arr=""
}
' Input_file
Output will be as follows.
22 30 31 3a 31 32 3a 32
30 32 30 20 32 32 3a 31
For getting values after BULK SPACE try:
awk '
{
line=$0
while(match($0,/[[:space:]]+/)){
arr=arr>RLENGTH?arr:RLENGTH
start[arr]+=RSTART+prev_start
prev_start=RSTART
$0=substr($0,RSTART+RLENGTH)
}
var=substr(line,start[arr])
sub(/^ +/,"",var)
print var
delete start
var=arr=""
}
' Input_file
Output will be as follows:
" 0 9 : 1 2 : 2
1 2 7 2 2 : 1
You can try
awk '{for(i=1;i<=NF;++i)if(length($i)==1)printf("%s ", $i);print("")}'
For each field, check the length and print it if it's desired. You may pass the -F option to awk if it's not separated by blanks.
The awk script is expanded as:
for( i = 1; i <= NF; ++i )
if( length( $i ) == 1 )
printf( "%s ", $i );
print( "" );
The print outside loop is to print a newline after each input line.
Assuming all the columns are tab-separated (So you can have a space as a column value like the second line of your sample), easy to do with a perl one-liner:
$ perl -F"\t" -lane 'BEGIN { $, = "\t" } print grep { /^.$/ } #F' foo.txt
" 0 9 : 1 2 : 2
1 2 7 2 2 : 1
$ perl -F"\t" -lane 'BEGIN { $, = "\t" } print grep { /^..$/ } #F' foo.txt
22 30 31 3a 31 32 3a 32
30 32 30 20 32 32 3a 31
The below code gives me 4 variables saving 4 different rows with 10 colums, whereas I need to save like 4 columns in 10 rows. I'm using hexdump syntax to extract from the file
program main
use mpi
integer :: wsize,wrank,ierr,i,fh,offset
integer , parameter :: count = 10
integer :: buf1(count),buf2(count),buf3(count),buf4(count)
integer , dimension(10,2) :: buf
call MPI_Init(ierr)
call MPI_Comm_rank(MPI_COMM_WORLD,wrank,ierr)
call MPI_Comm_size(MPI_COMM_WORLD,wsize,ierr)
offset = 0;
call MPI_File_open(MPI_COMM_WORLD, "test1.dat", MPI_MODE_RDWR + MPI_MODE_CREATE, MPI_INFO_NULL, fh, ierr)
do i = 1,count
buf1(i) = 1*i
buf2(i) = 2*i
buf3(i) = 3*i
buf4(i) = 4*i
end do
call MPI_FILE_WRITE_AT(fh, offset, /buf1,buf2,buf3,buf4/), 4*count, MPI_INTEGER, mpi_status_ignore, ierr)
call MPI_File_close(fh,ierr)
call MPI_FINALIZE(ierr)
end program main
using Hexdump command:
hexdump -v -e ' "%10d" ' -e ' "\n"' test1.dat > hextest1.dat`
If you try to open hextest1.dat after conversions it seems to be like what I posted.
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
I am beginner for Linux. I have the following flat file test.txt
Iteration 1
Telephony
Pass/Fail
5.1.1.1 voiceCallPhoneBook 50 45
5.1.1.4 voiceCallPhoneHistory 50 49
5.1.1.7 receiveCall 100 100
5.1.1.8 deleteContacts 20 19
5.1.1.9 addContacts 20 20
Telephony 16:47:42
Messaging
Pass/Fail
5.1.2.3 openSMS 50 49
5.1.2.1 smsManuallyEntryOption 50 50
5.1.2.2 smsSelectContactsOption 50 50
Messaging 03:26:31
Email
Pass/Fail
Email 00:00:48
Email
Pass/Fail
Email 00:00:40
PIM
Pass/Fail
5.1.6.1 addAppointment 5 0
5.1.6.2 setAlarm 1 0
5.1.6.3 deleteAppointment 5 0
5.1.6.4 deleteAlarm 1 0
5.1.6.5 addTask 1 0
5.1.6.6 openTask 1 0
5.1.6.7 deleteTask 1 0
PIM 00:03:06
Multi-Media
teration 2
Telephony
Pass/Fail
5.1.1.1 voiceCallPhoneBook 50 47
5.1.1.4 voiceCallPhoneHistory 50 50
5.1.1.7 receiveCall 100 100
5.1.1.8 deleteContacts 20 20
5.1.1.9 addContacts 20 20
Telephony 04:02:05
Messaging
Pass/Fail
5.1.2.3 openSMS 50 50
5.1.2.1 smsManuallyEntryOption 50 50
5.1.2.2 smsSelectContactsOption 50 50
Messaging 03:20:01
Email
Pass/Fail
Email 00:00:47
Email
Pass/Fail
Email 00:00:40
PIM
Pass/Fail
5.1.6.1 addAppointment 5 5
5.1.6.2 setAlarm 1 1
5.1.6.3 deleteAppointment 5 5
5.1.6.4 deleteAlarm 1 1
5.1.6.5 addTask 1 1
5.1.6.6 openTask 1 1
5.1.6.7 deleteTask 1 1
PIM 00:09:20
Multi-Media
I want to count the number of occurrences for specific word in the file Eg: if i search with "voiceCallPhoneBook" it's display as 2 times.
i can use
cat reports.txt | grep "5.1.1.4" | cut -d' ' -f1,4,7,10 |
after running this script i got output like below
5.1.1.4 voiceCallPhoneBook 50 45
5.1.1.4 voiceCallPhoneBook 50 47
It is very large file and i want to make use of loops with bash/awk scripts and also find the average of SUM of 3rd and 4th column value. i am struggling to write in bash scripts. It would be appreciated someone can give the solution for it.
Thanks
#!/usr/bin/awk -f
BEGIN{
c3 = 0
c4 = 0
count = 0
}
/voiceCallPhoneBook/{
c3 = c3 + $3;
c4 = c4 + $4;
count++;
}
END{
print "column 3 avg: " c3/count
print "column 4 avg: " c4/count
}
1) save it in a file for example countVoiceCall.awk
2) awk -f countVoiceCall.awk sample.txt
output:
column 3 avg: 50
column 4 avg: 46
Briefly explain:
a. BEGIN{...} block uses for variables initialization
b. /PATTERN/{...} blocks uses to search your keyword, for example "voiceCallPhoneBook"
c. END{...} block uses for print the results
This will search for lines containing 5.1.1.4
Make a tally of the 3rd and 4th columns
Then print them all out
awk '/^5\.?\.?\.?/ {a[$1" " $2] +=$3 ; b[$1" " $2] +=$4 }
END{ for (k in a){
printf("%-50s%-10i%-10i\n",k,a[k],b[k])}
}' $1
Duplicate from earlier today is here Parse the large test files using awk
With headers avg and Occurence count and formatted a bit neater for easier reading :)
awk 'BEGIN{
printf("%-50s%-10s%-10s%-10s\n","Name","Col3 Tot","Col4 Tot","Ocurr")
}
/^5\.?\.?\.?/ {
count++
c3 = c3 + $3
c4 = c4 + $4
a[$1" " $2] +=$3
b[$1" " $2] +=$4
c[$1" " $2]++
}
END{
for (k in a)
{printf("%-50s%-10i%-10i%-10i\n",k,a[k],b[k],c[k])}
print "col3 avg: " c3/count "\ncol4 avg: " c4/count
}' $1
static void main(args){
System.in.withReader {
def input = it.readLine()
for(def i = 0; i < input; i++){
println i
}
}
}
The source code..simple one I guess but dont know why it is printing till 48..here is the output if the argument supplied is 1.
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
what could be the problem?
Tartar is right, the solution is to change
def input = it.readLine()
To
def input = Integer.parseInt( it.readLine() )
Or (more Groovy)
def input = it.readLine().toInteger()
(the reason it is using the ASCII value of 1 is that groovy will convert single char strings to their ASCII value if you try to coerce them into an int... It has been argued that this is confusing, and it may change in future versions of groovy, but for now it remains for backward compatibility reasons)
ascii value for character 1 is 49. so convert input to integer maybe?
I have an interesting problem that involves taking the last day from a matrix and finding its last month day. Eg, if the date today is Oct-10-2011, you try to search for Sep-10-2011 or the first day < Sep-10-2011 in the matrix.
Matrix has multiple IDs and last trading dates may not be the same.
Vectorized solution is desired. Thanks!
mat = [
1000 734507 11 ; 1000 734508 12 ; 1000 734509 13 ;
2001 734507 21 ; 2001 734508 22 ; 2001 734513 23 ; 2001 734516 25 ;
1000 734536 14 ; 1000 734537 15 ; 1000 734538 16 ;
2001 734536 26 ; 2001 734537 27 ; 2001 734544 28 ; 2001 734545 29;2001 734546 30
];
% datestr(mat(:,2))
[~,m,~] = unique(mat(:,1), 'rows', 'last') ;
lastDay = mat(m,;) ;
Tried using addtodate to get last-month-date here but it fails (more than 1 row)
Once I get the last-dates for each ID, I need to get the exact_day_lastmonth. After this, I need to get data on this day OR the day nearest to it (should be < exact_day_lastmonth).
Answer:
current_lastdays = [1000 734538 16 ; 2001 734546 30] ; % 4-Feb-2011, 12-Feb-2011
matching_lastmon = [1000 734507 11 ; 2001 734513 23] ; % 4-Jan-2011, 10-Jan-2011
Unless you want to risk rather large arrays with complicated indexing, I think a loop is the way to go.
mat = [ 1000 734507 11 ; 1000 734508 12 ; 1000 734509 13 ;
2001 734507 21 ; 2001 734508 22 ; 2001 734513 23 ; 2001 734516 25 ;
1000 734536 14 ; 1000 734537 15 ; 1000 734538 16 ;
2001 734536 26 ; 2001 734537 27 ; 2001 734544 28 ;2001 734545 29;2001 734546 30];
%# datestr(mat(:,2))
[~,m,~] = unique(mat(:,1), 'rows', 'last') ;
lastDay = mat(m,;) ;
matching_lastmon = lastDay; %# initialize matching_lastmon
oneMonthBefore = datenum(bsxfun(#minus,datevec(lastDay(:,2)),[0,1,0,0,0,0]));
for iDay = 1:size(lastDay,1)
%# the following assumes that the array `mat` is sorted within each ID (or globally sorted by date)
idx = find(mat(:,1)==lastDay(iDay,1) & mat(:,2) <= oneMothBefore(iDay),1,'last')
if isempty(idx)
matching_lastmon(iDay,2:3) = NaN;
else
matching_lastmon(iDay,:) = mat(idx,:);
end
end