Page Index Toggle Pages: [1] 2 3 
Topic Tools
Very Hot Topic (More than 25 Replies) Sorting a list by moving one item up or down one step (Read 8,342 times)
malmklang2
Jr. Developer
Language Team
***
Offline



Posts: 257
Location: Norway

None
Sorting a list by moving one item up or down one step
Oct 10th, 2012 at 9:27pm
Post Tools
First, I thought this was a section for non-YaBB related programming questions, yet all I see is YaBB related topics?

Anyways, I have this html list of let's say urls. I have ajax doing on-the-fly editing of each item (url). What I can't figure out any good solution for is moving items up or down one step. Moving an item all the way up or dow is easy with shift and pop, but just one step, that's a bit harder.

What I got so far is eh, basically nothing, other than that the entire list array comes from a mysql field (text) where each item is separated by some usual separator characters like ||.

So, the question is, I guess, how to pop/push or shift/unshift an item one step up or down this array, which might in fact turn out to be a hash in case I want both url and description of the url?
« Last Edit: Oct 10th, 2012 at 9:32pm by malmklang2 »  

if ($human) {die("Sorry, you lot have destroyed too much already")}
Back to top
IP Logged
 
Dandello
YaBB Administrator
YaBB Next Team
Operations Team
Beta Testers
Support Team
*****
Offline



Posts: 2,492
Location: Earth

YaBB 2.6.1
Re: Sorting a list by moving one item up or down one step
Reply #1 - Oct 10th, 2012 at 10:52pm
Post Tools
It was so much worse before things got moved to (more or less) more logical places.  Roll Eyes

Okay - what I think you're describing is turning a pipe delimited list into something else so it can be reordered easily?

Not sure it can be done easily automatically. But as a Perl monger one way to do it would be to turn your list into a newline delimited list, add a sortable key in front of each entry and then move things around with the sortable key.  Then turn it back to a pipe-delimited list.

Just an idea - no guarantees.
  

If you only have one solution to a problem you're not trying hard enough!
Back to top
WWW  
IP Logged
 
malmklang2
Jr. Developer
Language Team
***
Offline



Posts: 257
Location: Norway

None
Re: Sorting a list by moving one item up or down one step
Reply #2 - Oct 10th, 2012 at 11:47pm
Post Tools
Yah, might end up doing something like that. There's a problem with that approach as well, cause I'll have to number things while looping the first time, then I end up with two items with identical numbers, and then what? Tongue Cause I want say, number 8 on the list to be number seven after the "move up" link is clicked, or 8 to be 9 after the "move down" click.

And to add salt to the sore, I had planned this should be done through/with the help of ajax, which I've never used before. So, I'm in both over my head and knee-deep, caught between a rock and a hard place!!

Yeah, I know, I am a complete ass at explaining Wink

Too bad shift, unshift, pop and push doesn't take a position argument like push $item to 7, where 7 would be the number eight slot.....
« Last Edit: Oct 10th, 2012 at 11:57pm by malmklang2 »  

if ($human) {die("Sorry, you lot have destroyed too much already")}
Back to top
IP Logged
 
Dandello
YaBB Administrator
YaBB Next Team
Operations Team
Beta Testers
Support Team
*****
Offline



Posts: 2,492
Location: Earth

YaBB 2.6.1
Re: Sorting a list by moving one item up or down one step
Reply #3 - Oct 11th, 2012 at 1:53am
Post Tools
On the 'move up' 'move down', have it switch the key numbers?

I'm a complete n00b when it comes to Ajax - sorry.
  

If you only have one solution to a problem you're not trying hard enough!
Back to top
WWW  
IP Logged
 
malmklang2
Jr. Developer
Language Team
***
Offline



Posts: 257
Location: Norway

None
Re: Sorting a list by moving one item up or down one step
Reply #4 - Oct 11th, 2012 at 10:01pm
Post Tools
Yeyh!!! I found a way, a bit eh, awkward, but it does the job. I'll post some code when I have everything set up. Then you can pick it apart and improve it for me  Cool

What I can say now, is that it is part of a simplified pinterest, only with sort options and acceptance of only urls, instead of urls that must include pictures to be pinned.

I might drop the whole plan to have ajax do the updating of the db, cause, man, ajax and jquery is certainly not Perl, and as hard as I sometimes find Perl, those other languages or combined languages, they just get the best of me Tongue
« Last Edit: Oct 11th, 2012 at 10:02pm by malmklang2 »  

if ($human) {die("Sorry, you lot have destroyed too much already")}
Back to top
IP Logged
 
malmklang2
Jr. Developer
Language Team
***
Offline



Posts: 257
Location: Norway

None
Re: Sorting a list by moving one item up or down one step
Reply #5 - Oct 11th, 2012 at 11:33pm
Post Tools
Well, I changed my mind. Might be wise to have you pick it apart before I continue, so that I don't replicate bad ways or awkward ways of programming, which I'm sure I am doing, by the way Tongue

http://malmklang.no/div/dynamic-list.txt
  

if ($human) {die("Sorry, you lot have destroyed too much already")}
Back to top
IP Logged
 
malmklang2
Jr. Developer
Language Team
***
Offline



Posts: 257
Location: Norway

None
Re: Sorting a list by moving one item up or down one step
Reply #6 - Oct 12th, 2012 at 6:19pm
Post Tools
So, my code is perfect? Darn, I ruuuuule  Grin

Naaah, I get it, we all have lives, no worries Smiley
  

if ($human) {die("Sorry, you lot have destroyed too much already")}
Back to top
IP Logged
 
Dandello
YaBB Administrator
YaBB Next Team
Operations Team
Beta Testers
Support Team
*****
Offline



Posts: 2,492
Location: Earth

YaBB 2.6.1
Re: Sorting a list by moving one item up or down one step
Reply #7 - Oct 12th, 2012 at 11:42pm
Post Tools
Just a couple things - ideally, all variables should be localized by 'my' (but sometimes it has to be 'our'), all subroutines really ought to end with a 'return;' (That specifies exactly what's supposed to be coming out of the subroutine AKA OO black box.)

3 part C-style loops are considered passe in some circles and $b and $a are reserved for sorts. (The idiomatic Perl version of one of the loops would be
Code
Select All
for my $bb ( 0 ..  $realstep) {
		$newUrlList .= qq~$listUrls[$bb]||~;
} 



And Damien Conway (Perl Best Practices) is adamant about only using curly brackets or '/' as regex delimiters (but the tilde works just fine anyway, especially since YaBB uses so many curly brackets for other things.)

Translation, it should work, but get used to putting 'my' in front of your variables.  Wink

« Last Edit: Oct 12th, 2012 at 11:43pm by Dandello »  

If you only have one solution to a problem you're not trying hard enough!
Back to top
WWW  
IP Logged
 
malmklang2
Jr. Developer
Language Team
***
Offline



Posts: 257
Location: Norway

None
Re: Sorting a list by moving one item up or down one step
Reply #8 - Oct 13th, 2012 at 12:07am
Post Tools
much valued feedback and I'm sure you had to restrain yourself from well, shaking your head is probably unavoidable, but, from yelling at me in public Smiley

I kind of was aware of a and b being reserved, just didn't think too much of it, since it has always worked the way I used it in that code sample Smiley

About the my prefix... I had a very bad experience with strict and my once.... I spent hours trying to figure out why a my variable wouldn't work till I figured out it was cause it was inside a sub, or something.... since then, I've stayed clear of both strict and my, but my is usable without strict, so... yeah..
  

if ($human) {die("Sorry, you lot have destroyed too much already")}
Back to top
IP Logged
 
Dandello
YaBB Administrator
YaBB Next Team
Operations Team
Beta Testers
Support Team
*****
Offline



Posts: 2,492
Location: Earth

YaBB 2.6.1
Re: Sorting a list by moving one item up or down one step
Reply #9 - Oct 13th, 2012 at 2:19am
Post Tools
Strict is good to aim for. (But I admit I was a late comer to that one too.)  Scripts will work without it, but crossing your 't's and dotting your 'i's is good for making sure things stay working the way you think they should.

One good tool really is PerlCritic. And you can take baby steps with it.  Some things that it flags are head scratchers, but mostly it's good advice.
  

If you only have one solution to a problem you're not trying hard enough!
Back to top
WWW  
IP Logged
 
malmklang2
Jr. Developer
Language Team
***
Offline



Posts: 257
Location: Norway

None
Re: Sorting a list by moving one item up or down one step
Reply #10 - Oct 13th, 2012 at 9:58am
Post Tools
Ok, I'll start using both strict and my again, but darnit, I'm coming for you the next time I spend hours figuring out why a my from a sub won't work outside of the sub!!!! Tongue
Might be cause I don't use the return fnction you mentioned? Ah, I don't know..... there's so much I don't know...

And it is probably advicable to start using a real editor aimed at Perl in particular as well, huh? It's just that ever since I started with HTML and CSS i 1998, then Perl in 1999-2000, I've done things the hard way, with nothing but a glorified notepad replacement, notetab, from Fokes. I kind of like to torment myself Wink
Old habits die what now? Tongue

That's also why I'm stuck in cave age ways of coding, I guess... oh well, I think this is about to change soon. For the first time in my coding life, I'm sort of ready to get serious with Perl, not just mess about for own usage, having code that works, but might be both awkward and cpu consuming Tongue

Darn, why haven't you told me about PerlCritic before???? We've "known" eachother for what, 10 days now, and NOW you tell me!!!!  Cool
« Last Edit: Oct 13th, 2012 at 9:59am by malmklang2 »  

if ($human) {die("Sorry, you lot have destroyed too much already")}
Back to top
IP Logged
 
Dandello
YaBB Administrator
YaBB Next Team
Operations Team
Beta Testers
Support Team
*****
Offline



Posts: 2,492
Location: Earth

YaBB 2.6.1
Re: Sorting a list by moving one item up or down one step
Reply #11 - Oct 13th, 2012 at 8:37pm
Post Tools
I cheerfully blame the people writing the Perl books who tell you (in fine print and only once somewhere in the introduction) "all our examples are missing 'my' and 'strict' and other things to make them clearer." WTF? So all the examples in the book are actually only half there to save on ink?

And the real trick with subs is to know exactly what supposed to be going into the sub and what's supposed to be coming out. (Which is why Conway recommends calling subs with  'mysub();' instead of '&mysub'. They're both correct but the first one reminds you that something is probably supposed to be going in for the sub to work on.

And wait till you start rolling your own modules - I still sometimes scratch my head on those. (The fracking thing worked as a sub in the script but doesn't work when called as a pm?  Angry )
« Last Edit: Oct 13th, 2012 at 8:40pm by Dandello »  

If you only have one solution to a problem you're not trying hard enough!
Back to top
WWW  
IP Logged
 
xnoddyx
Support Team
Documentation Team
YaBB Moderators
YaBB Next Team
Beta Testers
****
Offline



Posts: 1,593
Location: UK:Scotland/livingston

None
Re: Sorting a list by moving one item up or down one step
Reply #12 - Oct 14th, 2012 at 2:19pm
Post Tools
Dandello wrote on Oct 13th, 2012 at 8:37pm:
And wait till you start rolling your own modules - I still sometimes scratch my head on those. (The fracking thing worked as a sub in the script but doesn't work when called as a pm?   )

you may want to see this  Wink http://www.drdobbs.com/web-development/building-your-own-perl-modules/184415975
  

YaBB install help video
1. what yabb forum are you running and the url
2. describe in as much detail as you can what happens and also post screenshots if you can
3. please be patient we live in different time zones and have other commitments but we will help you
as bill and ted say (Be excellent to each other)
Back to top
IP Logged
 
Dandello
YaBB Administrator
YaBB Next Team
Operations Team
Beta Testers
Support Team
*****
Offline



Posts: 2,492
Location: Earth

YaBB 2.6.1
Re: Sorting a list by moving one item up or down one step
Reply #13 - Oct 14th, 2012 at 3:42pm
Post Tools
He's in my bookmarks  Wink

An another tidbit I'll toss out there - in YaBB, if it's a .pl and doesn't have a shebang, it's actually a .pm using an old-fashioned naming structure.  Roll Eyes (Remember that YaBB is 12+ years old.)

So - the actual data is in /Boards, /Members, /Messages, and /Variables. (Help is an odd duck - the way it's put together and controlled is different than everything else.)

Everything else is in a folder that ideally should be designated as a module library folder.  Roll Eyes

  

If you only have one solution to a problem you're not trying hard enough!
Back to top
WWW  
IP Logged
 
malmklang2
Jr. Developer
Language Team
***
Offline



Posts: 257
Location: Norway

None
Re: Sorting a list by moving one item up or down one step
Reply #14 - Oct 14th, 2012 at 4:58pm
Post Tools
Well, Perl Best Practices is now supposedly on its way from the states to me, so, beware, after Christmas, I'll be back to teach you things you never knew Perl could do!!! Wink
  

if ($human) {die("Sorry, you lot have destroyed too much already")}
Back to top
IP Logged
 
Page Index Toggle Pages: [1] 2 3 
Topic Tools