Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I keep the end of this line? 1

Status
Not open for further replies.

kb0rhe

Technical User
Dec 25, 2006
16
US
This is what I have below.
BRG 100023 GASOHOL TEST KIT

This is what I want:
GASOHOL TEST KIT

I can extract the BRG and 100023 but I need the "Title" of the product now.

@array("BRG 100023 GASOHOL TEST KIT");
print $array[1] . " " . $array[2]. " ". $array[3];

That would only get me
BRG 100023 GASOHOL
 
Code:
@array = ("BRG 100023 GASOHOL TEST KIT");
print $array[1] . " " . $array[2]. " ". $array[3];

$array[0] should equal "BRG 100023 GASOHOL TEST KIT" since the array has one item in quotes. $array[1+] should be undef.

Code:
@array = qw(BRG 100023 GASOHOL TEST KIT);
print $array[1] . " " . $array[2]. " ". $array[3];

$array[1] = "100023"; $array[2] = "GASOHOL"; $array[3] = "TEST"

Arrays start at 0 and go from there, so that "BRG" is element 0, not element 1.

If you want to count backwards from the array, use negative numbers. $array[-1] is "KIT", $array[-2] is "TEST", and $array[-3] is "GASOHOL", and so-on.

-------------
Cuvou.com | The NEW Kirsle.net
 
Sorry I left out a bunch of code and information.
I actual split this line before I get to this point

Where $line = Z BRG 100023 GASOHOL TEST KIT
$line = $_; #Set value of $line
$line =~ s/\s+/ /g; #Remove extra spaces
@array1 = split(/ /, $line); $split line into segments.
$printthis1 = $array1[1]. " " . $array1[2]. " ". $array1[3];
print OUTFILE1 $printthis1 . "\n" ;

Therefore after all the above you would get exactly what I specified above.

I appreciate the quick response.
 
So...

Code:
print $array1[-3] . " " . $array1[-2] . " " . $array1[-1]; # "GASOHOL TEST KIT"

-------------
Cuvou.com | The NEW Kirsle.net
 
What we need to understand is that the "Title" can be any length. So printing the array in the last response wont work.

I need something that finds the lenght of the string and removes the leading portion of the text.

e.g.
"Z BRG 100023 GASOHOL TEST KIT".

I know that "Z BRG 100023" array elements will always be the same. However the string value will change as will the lenght so I need to extract Elements 0,1,2 from the above string.
 
You need to describe your situation better. I've read the thread twice and still do not understand what you need to do.

- Kevin, perl coder unexceptional!
 
maybe this is what you are trying to do?

Code:
$line = 'Z BRG 100023     GASOHOL TEST KIT';
$t = (split(/\s+/,$line,4))[3];
print $t;

- Kevin, perl coder unexceptional!
 
Kevin
Yes thanks works great. (I am just stupid in perl. No... wait... just started in perl.)

Can you explain line 2 to me?

I understand that you are setting $t = <Rightside of =>

I tried just doing:

$r = [3];
print $r ;

that prints out Array(0xXXXX)

What's happening on that line?


 
Code:
$t = (split(/\s+/,$line,4))[3];

the above is a shorter way of doing this:

Code:
@array = split(/\s+/,$line,4);
$t = $array[3];

first I used the limit option of the split function:

Code:
split(/pattern/, expression, [b]limit[/b])

to split the line into only 4 elements using space(s) as the pattern:

Code:
index#     value
------------------
  0         Z
  1         BRG
  2         100023
  3         GASOHOL TEST KIT

but instead of assigning all the return values of the split function to an array, I used an array slice to return only the value of interest, the forth value ([3]), to the scalar $t.

Same as this example:

Code:
@array = qw(foo blah bar blah baz blah);
($foo,$bar,$baz) = @array[0,2,4];

the main difference though is that in my code there is no named array so you have to use parenthesis around the right side to create an annonymous list and return only the values indicated in the square brackets at the end of the expression:

Code:
$t = (split(/\s+/,$line,4))[3];

the rest of the values are discarded. You can return as many values as you want, in just about any order:

Code:
($t,$r) = (split(/\s+/,$line,4))[3,0];

so $r would equal 'Z' and $t would eqaul 'GASOHOL TEST KIT' using your line of text.


- Kevin, perl coder unexceptional!
 
Wow.
Thank you very much for the excellent explaination!
Douglas
 
damn, made tired typing all that. Time for a drinky-winky [cheers]

- Kevin, perl coder unexceptional!
 
Hey Kevin, I just learned on how to lean my code a bit!
Thank you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top