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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Clipboard Windows 1

Status
Not open for further replies.

folkda

Technical User
Sep 3, 2010
3
US
I want to parse model numbers based on clipboard copy. Text looks like this.

20 3 - - GG225FZTQ00 22 CU FF CHEST SPHINKTER released 1.000000 M model I 01-Jan-1900
20 3 - - GG225FZTQ11 22 CU FF CHEST SPHINKTER released 1.000000 M model I 01-Jan-1900

I want to have just the model number e.g. GG225F2TQ00.

I have a perl program that reads data from a file and it parses out great. However I am having problems reading the data from the clipboard and parsing.


 
my $CLIP= Win32::Clipboard();
my @text = $CLIP->GetText();
print @text;
print "Done\n";

while(my $line=$CLIP->GetText()){
my $stringLng = length($line); #Get string Length
my $area = index($line, "released"); # GET POSITION OF WORD RELEASED
my $f_string = substr($line,0, $area); #Remove balance of Line including the word RELEASED, OBSOLETE etc,
my @array = split(/ /, $f_string); # Split each line into an Array.
my $array = @array; # Assign Array to Scaler?
my $h_value1 = length($array[0]); #Get the length of Array item 0.
my $h_value2 = $array[5]+5; #Value is to calculate where the part number array starts.
my $h_value3 = $array[4]+4; #Value is needed to see Where the part number array starts if a plant code exists.
my $text_value = length($array[2]);
my $g_pn_lng = length($h_value2); #Part Number length w/o Plant Code.
my $g_pn_lng01 = length($h_value3); #Part Number length WITH Plant Code.
my $pos_short = index($f_string, $array[$h_value3]); #Plant Code existing
my $pos_long = index($f_string, $array[$h_value2]); #NO Plant Code.
my $aasize = @array; #Returns the size of the Array Number of elements..
my $array_element = length($array[$aasize-1]); #finds the length of the last element (-1 is necessary to see the last physical element)

if (length($array[2]) > 1)
{
#my $f_print01 = substr($f_string, $pos_short, length($f_string));
print "$array[$h_value3] "; #Get Part Number - With Plant code in place.
#print OFILE "$f_print01\n";
}
else
{
#my $f_print02 = substr($f_string, $pos_long, length($f_string));
print "$array[$h_value2]\n";
#print OFILE "$f_print02\n";
}
#print "$array[$getval01]\n";
} # END WHILE

*** GO easy on me - this works when I use a file for the input.
 
How about something like this?
Code:
use warnings;
use strict;
use Win32::Clipboard;

my $clip =  Win32::Clipboard->new();
if ($clip->IsText()) {
	my @lines = split /[\x0a\x0d]+/, $clip->GetText();
	for (my $i = 0; $i <= $#lines; $i++) {
		if ($lines[$i] =~ m/^\s*\d+\s+\d+ - - ([^ ]+)/) {
			$lines[$i] = $1;
		} else {
			$lines[$i] .= ':no match';
			warn "$lines[$i]\n";
		}
	}
	$clip->Set(join("\r\n", @lines));
} else {
	die "No text in clipboard\n";
}
 
Thank you. I wish I understood the reg exp. better. Looks like you did in 4/5 lines what I attempted in 20.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top