extract the first word in a string 
Author Message
 extract the first word in a string

I have many strings like
"Settled Date:04/30/02BostonSale"
"Settled Date:04/30/02New YorkSale"
"Settled Date:04/30/02San FranciscoSale"
Can someone help me figure out how I can get "Boston","New York","San
Francisco"
into a variable ?

I am chopping of "Settled Date" and "Sale" from the string using
substr() with index(), since these two strings are constant. My
problem is in extracting the words from what is left:
04/30/02Boston"
05/15/02New York"
02/17/03San Francisco"

Thanks in advance,

- RT



Mon, 26 Sep 2005 20:18:28 GMT  
 extract the first word in a string

Quote:

> I have many strings like
> "Settled Date:04/30/02BostonSale"
> "Settled Date:04/30/02New YorkSale"
> "Settled Date:04/30/02San FranciscoSale"
> Can someone help me figure out how I can get "Boston","New York","San
> Francisco"
> into a variable ?

Use a regular expression:

   while (<DATA>) {
       next unless /\d{2}([\w ]+)Sale$/;
       print "Matched [$1]\n";
   }

   __END__
   Settled Date:04/30/02BostonSale
   Settled Date:04/30/02New YorkSale
   Settled Date:04/30/02San FranciscoSale

There's a good introduction here:

   $ perldoc perlrequick

--
Steve



Mon, 26 Sep 2005 20:33:59 GMT  
 extract the first word in a string

Quote:

> Can someone help me figure out how I can get "Boston","New York","San
> Francisco"
> into a variable ?

> I am chopping of "Settled Date" and "Sale" from the string using
> substr() with index(), since these two strings are constant.

If you notice that the _length_ of the unwanted prefix and suffix
is constant, then you can use substr() to solve the problem.

----------------------------
#!/usr/bin/perl
use strict;
use warnings;

while ( <DATA> ) {
   chomp;
   substr($_, 0, 21) = '';
   substr($_, -4) = '';
   print "$_\n";

Quote:
}

__DATA__
Settled Date:04/30/02BostonSale
Settled Date:04/30/02New YorkSale
Settled Date:04/30/02San FranciscoSale
----------------------------

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Tue, 27 Sep 2005 13:43:56 GMT  
 extract the first word in a string

Quote:


>> Can someone help me figure out how I can get "Boston","New York","San
>> Francisco"
>> into a variable ?

>> I am chopping of "Settled Date" and "Sale" from the string using
>> substr() with index(), since these two strings are constant.

> If you notice that the _length_ of the unwanted prefix and suffix
> is constant, then you can use substr() to solve the problem.

> ----------------------------
> #!/usr/bin/perl
> use strict;
> use warnings;

> while ( <DATA> ) {
>    chomp;
>    substr($_, 0, 21) = '';
>    substr($_, -4) = '';
>    print "$_\n";
> }

> __DATA__
> Settled Date:04/30/02BostonSale
> Settled Date:04/30/02New YorkSale
> Settled Date:04/30/02San FranciscoSale
> ----------------------------

Or, probably better, just snatch the part you want with a single substr():

      my $city = substr($_, 21, -4);

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Tue, 27 Sep 2005 14:28:06 GMT  
 extract the first word in a string
Hi, I am posting an email conversation on my personal mailbox,
with a solution that worked for me!

Thanks to all for the replies.
- RT

----------------------------------------------------------------
Wow!!! Thank you so much, it works like a clock for both cases!!

- RT

Quote:
----- Original Message -----
From: "HELTERLINE,BRIAN (HP-Corvallis,ex1)"
To: "'Rex Lustrous'"
Sent: Thursday, April 10, 2003 5:49 PM
Subject: RE: extract the first word in a string

 No,

 my original RE would not work without a date.  you
 did not specify the date part was optional.
 If *only* the date is optional, then try this:

 my $string = 'Settled Date:04/30/02BostonSale';
 my ( $city ) = $string =~ /Settle Date:[\d/]*(.*)Sale/'

 This says to:
 Match the literal 'Settle Date:'
 Match the character class of digit or '/' zero or more times
 Match and capture the next character zero or more times
 Match the literal 'Sale'

 -brian

 -----Original Message-----
 From: Rex Lustrous
 Sent: Thursday, April 10, 2003 12:43 PM
 To: HELTERLINE,BRIAN (HP-Corvallis,ex1)
 Subject: Re: extract the first word in a string

 Would this work if the data has a few lines like this ?
 Thanks for the immediate response.

 But would it work if some of the lines don't have any date information  ?
 e.g.
 "Settled Date:BostonSale"
 "Settled Date:04/30/02New YorkSale"
 "Settled Date:San FranciscoSale"

 I don't really care for the date information. All I need is to be able to
 extract the City name.

 Thanks in advance,
 - RT

 ----- Original Message -----
 From: "HELTERLINE,BRIAN (HP-Corvallis,ex1)"
 To: Rex Lustrous
 Sent: Thursday, April 10, 2003 3:34 PM
 Subject: Re: extract the first word in a string

 If this is what is left, it looks like you want the 9th char to the end
of
 the string.

 my $input = '02/17/03San Francisco';
 my $city = substr $input, 8;

 or to to it all at once using a regular expression:
 my $string = 'Settled Date:04/30/02BostonSale';
 my ( $m $d, $y, $city ) = $string =~ /Settle
 Date:(\d\d)/(\d\d)/(\d\d)(.*)Sale/'

 If you don't want the settle date, you don't have have to capture it.

 - brian

----------------------------------------------------------------



Tue, 27 Sep 2005 16:57:36 GMT  
 extract the first word in a string
Hi, I am posting an email conversation on my personal mailbox,
with a solution that worked for me!

Thanks to all for the replies.
- RT

----------------------------------------------------------------

Quote:
 ----- Original Message -----
 From: "HELTERLINE,BRIAN (HP-Corvallis,ex1)"
 To: Rex Lustrous
 Sent: Thursday, April 10, 2003 3:34 PM
 Subject: Re: extract the first word in a string

 If this is what is left, it looks like you want the 9th char to the end
of
 the string.

 my $input = '02/17/03San Francisco';
 my $city = substr $input, 8;

 or to to it all at once using a regular expression:
 my $string = 'Settled Date:04/30/02BostonSale';
 my ( $m $d, $y, $city ) = $string =~ /Settle
 Date:(\d\d)/(\d\d)/(\d\d)(.*)Sale/'

 If you don't want the settle date, you don't have have to capture it.

 - brian

 -----Original Message-----
 From: Rex Lustrous
 Sent: Thursday, April 10, 2003 12:43 PM
 To: HELTERLINE,BRIAN (HP-Corvallis,ex1)
 Subject: Re: extract the first word in a string

 Would this work if the data has a few lines like this ?
 Thanks for the immediate response.

 But would it work if some of the lines don't have any date information  ?
 e.g.
 "Settled Date:BostonSale"
 "Settled Date:04/30/02New YorkSale"
 "Settled Date:San FranciscoSale"

 I don't really care for the date information. All I need is to be able to
 extract the City name.

 Thanks in advance,
 - RT

----- Original Message -----
From: "HELTERLINE,BRIAN (HP-Corvallis,ex1)"
To: "'Rex Lustrous'"
Sent: Thursday, April 10, 2003 5:49 PM
Subject: RE: extract the first word in a string

 No,

 my original RE would not work without a date.  you
 did not specify the date part was optional.
 If *only* the date is optional, then try this:

 my $string = 'Settled Date:04/30/02BostonSale';
 my ( $city ) = $string =~ /Settle Date:[\d/]*(.*)Sale/'

 This says to:
 Match the literal 'Settle Date:'
 Match the character class of digit or '/' zero or more times
 Match and capture the next character zero or more times
 Match the literal 'Sale'

 -brian

 -----Original Message-----
 From: Rex Lustrous
 Sent: Thursday, April 10, 2003 12:43 PM
 To: HELTERLINE,BRIAN (HP-Corvallis,ex1)
 Subject: Re: extract the first word in a string

Wow!!! Thank you so much, it works like a clock for both cases!!

- RT

----------------------------------------------------------------



Tue, 27 Sep 2005 16:59:46 GMT  
 extract the first word in a string

Quote:

> I have many strings like
> "Settled Date:04/30/02BostonSale"
> "Settled Date:04/30/02New YorkSale"
> "Settled Date:04/30/02San FranciscoSale"
> Can someone help me figure out how I can get "Boston","New York","San
> Francisco"
> into a variable ?

> I am chopping of "Settled Date" and "Sale" from the string using
> substr() with index(), since these two strings are constant. My
> problem is in extracting the words from what is left:
> 04/30/02Boston"
> 05/15/02New York"
> 02/17/03San Francisco"

> Thanks in advance,

> - RT

Hi RT:

A few thoughts you might find useful:

(1)
You don't say how these items are stored- in an array perhaps? If in a
file I recommend you slurp them up into an array, assuming the size of
the file isn't prohibitive. Use something like:

  die "huh? cant open it??' unless open N, 'myNamefile.txt'

  close N;

(2)
Ok now you have your file in an array which is much nicer to work with
than data through a file handle. A simple operation will get you the
data you see; basically you might want to apply something like this:

   s/.+\d(.+)Sale/$1/;

Assuming that (a) the data doesn't have lines NOT in this format, and
(b) there is only one such string per element.

(3)
now, how to apply this to the array? map comes to mind:


 so the whole program:

    #!/usr/bin/perl -wd

    die "huh? cant open it??\n\n" unless open N, 'myNamefile.txt';

    close N;




Notice- no explicit loops except prints, and no *ick!!* substr().
substr() is what people who don't know regexes use since it "looks
like c". Avoid it. It has very limited use in Perl and its unfortunate
it was even included. (I'm sort of negative on substr, can you tell?)

(4)
FINALLY, say you have duplicate cities in the file but you want an
array of *unique* cities from the original? Sound like a possibility?
We can use a hash to help us- substitute the map and print lines with
(we throw away the values):


   print "$_\n" for keys %names;

Now you get a list of UNIQUE names. How much easier can this get!?

Hope some of this was helpful..

Cheers, have a nice weekend...
-Gx



Tue, 27 Sep 2005 19:58:26 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. How to extract first letter in string?

2. how to get the first word of a string

3. How to: Create Regex which extracts N number of words before target word

4. How to Capitalize the first letter of a word an or whole word

5. extract string from another string

6. regular expression to extract a string between 2 other strings

7. Extract string from a string

8. Extract last word on line

9. Extracting a range of words!

10. extracting word from line

11. Extracting Word from Variable

12. Help please extracting data from a word document

 

 
Powered by phpBB® Forum Software