MarkMc Posted July 5, 2018 Share Posted July 5, 2018 Short version is- I'm looking for a way to trim the beginning and the end or a string with Ruby. Part to trim at the beginning is ">qty>" where qty may be one or two integers-so I need to trim between 3 and 4 characters depending. Part to trim at the back is ">price>" where price may be one, two or three integers. (need to trim between 3 and 5 characters) Is there a simple way to do this in Ruby? Long version- I was working on stuff for my upcoming web meeting on cabinet pricing. I decided to try something that might make it simpler for others to use and idea occrred to me that would make it much easier for others to use (I've been doing it so long I know where the rocks are). After more fiddling than I expected it is more user friendly. But I've run into a glitch while updating some actual cabinets I had saved. I've got an OIP field (supplier) formatted to allow the schedule to be copied and the pasted into a spreadsheet with item number, qty, code, price to each end up in the correct column Format in the field is currently is (>qty>mfg_code>price;space). > is being used as a text delimitier when pasting. As noted earlier qty is typicall one or two integers; price is typical one, two or three integers. I use a spreadhseet with a concatenate formula in it to alter existing manufacturer codes to suit so that part is really easy once set up. The deliminiter MUST be a special character. It can't be something that upsets Ruby, the spreadhseet, match things that Chief places automatically, or be something needed for a manufacturer code (have run into every possible problem there already). It can't be-tab, space, return, comma, semi-colon, dash, or backslash. Typical supplier entry looks like: >2>FLSHENDB LR>65; >1>NSPO - set 17-1/2" from floor>219; >4> 4 REV-A-SHELF 597-12-CR on floor>40; >1>FDS set 13-1/2" above floor>22; (I can generate these easily enough in a spreadhseet for regular use, and it pastes as needed) Schedule looks like The _Order field is another custom OIP field that puts the label and the supplier fields together Pasted into sheet Then I have a macro in a custom OIP field to strip aways the deliminiters, quantity, price to use in a schedule that goes on the printed layout. the macro I have in there is mods=owner.supplier mods.delete!("\r\n,0-9,>") The output reads: FLSHENDB LR; NSPO - set -/" from floor; REV-A-SHELF --CR on floor; FDS set -/" above floor; Instead of FLSHENDB LR; NSPO - set 17-1/2" from floor; 4 REV-A-SHELF 597-12-CR on floor; FDS set 13-1/2" above floor; What I have deletes numbers I need from the mfg_code (which I missed while doing all the testing to get this to paste into columns correctly, only discovered as I was adjusting some saved cabinets I had and checking things) I haven't been able to figure it out (not even close). I'm basically throwing darts with methods and pretty poor at syntax. It would also be nice to trim off the very last semi colon but that is minor (heck I might even figure that one out). Link to comment Share on other sites More sharing options...
Joe_Carrick Posted July 5, 2018 Share Posted July 5, 2018 Yes, there is a way to do this. For the beginning of the string it requires that you get the integer value of the string. You can then convert that to a string value (str) and use string.sub(str,"") For the end of the string it's a little more complicated in that you need to determine which how many characters are numerical and just shorten the string. 1 Link to comment Share on other sites More sharing options...
MarkMc Posted July 5, 2018 Author Share Posted July 5, 2018 4 minutes ago, Joe_Carrick said: For the beginning of the string it requires that you get the integer value of the string. You can then convert that to a string value (str) and use string.sub(str,"") It varies for each string-1, 2, 24, 56...doesn't that present a problem. 6 minutes ago, Joe_Carrick said: For the end of the string it's a little more complicated in that you need to determine which how many characters are numerical and just shorten the string. I checked and pasting works if I add "0" 's so that there are the same number of integers. But the integers need to be followed by a semi-colon. Does that still have possibilities? I'm assuming that I can always add in to remove the > after ? or at the same time? Link to comment Share on other sites More sharing options...
MarkMc Posted July 5, 2018 Author Share Posted July 5, 2018 5 minutes ago, solver said: Quick test using gsub and a regular expression (pattern to match). Thanks, heading back down the rabbit hole to have at that, let you know how I do. Nothing in life has ever made me feel as stupid as Ruby and it's been a while since I did much with it so whatever I had is all lost Link to comment Share on other sites More sharing options...
MarkMc Posted July 5, 2018 Author Share Posted July 5, 2018 56 minutes ago, solver said: Forgot to add, you can also modify the string in place (with gsub!) like this x='abc>123>xyz>456789>' x.gsub!(/>[0-9]*>$/,'') That got me there. The commenting helped immensely. When I included $ it returned blank. Tried a few variations ended up with this mods=owner.supplier x=mods.delete!("\r\n") y=x.gsub!(/>[0-9]*>/,'') y.gsub!(/>[0-9]*/,'') Perhaps not the most elegant solution but it worked (I'm already further in on this whole deal than I expected.) Image with both columns side by side-will be used separately. BIg thanks Eric Link to comment Share on other sites More sharing options...
Joe_Carrick Posted July 6, 2018 Share Posted July 6, 2018 Knowing there might be other numerical characters in the original string I took the approach of specifically eliminating values at the beginning and end. Link to comment Share on other sites More sharing options...
MarkMc Posted July 6, 2018 Author Share Posted July 6, 2018 9 hours ago, Joe_Carrick said: Knowing there might be other numerical characters in the original string I took the approach of specifically eliminating values at the beginning and end. Joe, I just couldn't figure out how to make it work, just not that good. I'd already tried gsub (among other things) without success and got that going first. Link to comment Share on other sites More sharing options...
Alaskan_Son Posted July 8, 2018 Share Posted July 8, 2018 In my opinion, you’re going about this in a much more complicated and problematic way than you really need to. I’m not gonna write the code for free but consider this: Be be more straightforward and specific about what you want from Ruby, and make data entry more intuitive. Do you really want Ruby to remove numbers from the beginning and end of the string, or do you actually want to remove quantities and prices? And what if you or someone else decides they want to tweak things a bit or use a different naming/numbering convention? my suggestion would be this: 1. Clearly define what is what both in your macro and at your data entry point... part number = 1235 color = yellow and black size = large name = bumble bee suit price = 300 2. Break that down and reconfigure using additional code to get your schedule formatting... name>part number>size>color>price 3. Reconfigure separately to get your desired label... Large Yellow and Black Bumble Bee Suit $300.00 SKU# 1235 Link to comment Share on other sites More sharing options...
MarkMc Posted July 8, 2018 Author Share Posted July 8, 2018 10 hours ago, Alaskan_Son said: In my opinion, you’re going about this in a much more complicated and problematic way than you really need to. Thanks for concern Michael. I'm happy with what I've got going now thanks to Eric and Gerry. I have a cabinet label macro from some a year and a half ago that got worked out with help from you, Joe, & Gerry. It's a lot like what you suggest. 11 hours ago, Alaskan_Son said: I’m not gonna write the code for free but consider Several times I thought of contacting (and paying) one of you Ruby folks to do this and another (even though I'm giving it away). In the end I decided that might not be as efficient as beating my head against the wall. (which provided a nice learning opportunity I will be showing the cabinet label macro at the webinar, even though it's not really a part of the system I'm using now. It works for one line of cabinets from one brand with their online ordering system. If anyone wants that adapted to a different brand or enhanced I'll be referring them to you or Joe or whoever else is offering Ruby services. 10 hours ago, Alaskan_Son said: And what if you or someone else decides they want to tweak things a bit or use a different naming/numbering convention? That's addressed. I think my system is easy to: learn, adapt per brand, and use. It may turn out that folks throw their hands up, "crazy, too weird, going back to 2020". I hope not but that's ok. I've been getting data out of Chief to use ordering manufactured cabinets for 5 yrs, in 6 brands, with varied order requirements & methods. This is the best I can do. At first it was pretty limited, at times complicated. Working on this presentation has improved it enormously just for my use, though that's less important nowadays. I'm really happy with it now though being newly revised their may be bugs. ;-> Link to comment Share on other sites More sharing options...
Alaskan_Son Posted July 9, 2018 Share Posted July 9, 2018 Sorry for the interruption. I’ll try not to let it happen again. Carry on. Link to comment Share on other sites More sharing options...
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now