(Solved) How to trim varied lengths from the beginning and end of strings with Ruby


MarkMc
 Share

Recommended Posts

 

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 5b3e89c91f18d_Readytocopyandpaste.thumb.png.41daa77e79d2e0b1a094bfcdb6540115.png
The _Order field is another custom OIP field that puts the label and the supplier fields together

Pasted into sheet

5b3e89ce5aff4_Pastedtosheet.thumb.png.5128abd227288944e7a1906e3e15700e.png

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;

5b3e89cbebced_Forlayout.thumb.png.0ff32ce37ca9a83cd71edf0deb29145e.png
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

Yes, there is a way to do this. 

  1. 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,"")
  2. 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.
  • Upvote 1
Link to comment
Share on other sites

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

On 7/5/2018 at 2:14 PM, MarkMc said:

 

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)

 

 

Quick test using gsub and a regular expression (pattern to match).

 

# define a test string

x='>abc>123>xyz>456789>'

 

# pattern is delimited by /

# search for a > followed by any number of digits followed by > and the end of string (the $ sign is end of string)

# find the matching pattern and replace it with nothing (2 single quotes)

# create a new string y
y=x.gsub(/>[0-9]*>$/,'')

 

# return the original string and the modified one
x+"\n"+y

 

# ^ is the start of string, so /^>[0-9]*>/ would match the same string at the beginning instead of the end.

 

  • Upvote 1
Link to comment
Share on other sites

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

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

Shows both options.png

Link to comment
Share on other sites

19 minutes ago, MarkMc said:

When I included $ it returned blank.

 

I noticed this happened too, but not every time. I thought I misstyped something. Leaving it off is OK as long as the pattern only matches once. 

 

With a string like >123>abc>123>

 

You will get abc without the $.

Link to comment
Share on other sites

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

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

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

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share