Parse a word into three strings 
Author Message
 Parse a word into three strings

This seems like it should be a novice question.  In fact, it is.  I
need to take the first character of a string, read it into its own
string, and delete it from the original string.  Then I need to take
the next four characters from the original string minus the first
character and read them into another string, and delete them from the
original string.

Say the original string = "E1230UNIFORM"
What I want to end up with is:
$firstChar = "E"
$nextFourChars = "1230"
$theRestOfThe String = "UNIFORM"

I have been fiddling with regular expressions off and on all day and I
get nowhere.  Can anyone help?

Ray



Mon, 21 Mar 2005 20:54:52 GMT  
 Parse a word into three strings
Ray Frye announced:

Quote:
> This seems like it should be a novice question.  In fact, it is.  I
> need to take the first character of a string, read it into its own
> string, and delete it from the original string.  Then I need to take
> the next four characters from the original string minus the first
> character and read them into another string, and delete them from the
> original string.

> Say the original string = "E1230UNIFORM"
> What I want to end up with is:
> $firstChar = "E"
> $nextFourChars = "1230"
> $theRestOfThe String = "UNIFORM"

> I have been fiddling with regular expressions off and on all day and I
> get nowhere.  Can anyone help?

> Ray

why don't you use substr?

use strict;
my $string = "E1230UNIFORM";
my $firstChar=substr($string,0,1);
my $nextFourChars=substr($string,1,4);
my $theRestOfTheString=substr($string,5);
print join (',',$firstChar,$nextFourChars,$theRestOfTheString);

HTH,
Dominik



Mon, 21 Mar 2005 21:13:57 GMT  
 Parse a word into three strings
Also sprach Ray Frye:

Quote:
> This seems like it should be a novice question.  In fact, it is.  I
> need to take the first character of a string, read it into its own
> string, and delete it from the original string.  Then I need to take
> the next four characters from the original string minus the first
> character and read them into another string, and delete them from the
> original string.

> Say the original string = "E1230UNIFORM"
> What I want to end up with is:
> $firstChar = "E"
> $nextFourChars = "1230"
> $theRestOfThe String = "UNIFORM"

> I have been fiddling with regular expressions off and on all day and I
> get nowhere.  Can anyone help?

Either use substr() as Dominik suggested or something along this line:

    my ($first_char, $next_four, $rest) = $string =~ /(.)(....)(.*)/;

A regex in list context returns the matches captured by parens. In the
above, there are three of them that linearly get assigned to the three
variables on the left side.

Tassilo
--

pam{rekcahbus;})(rekcah{lrePbus;})(lreP{rehtonabus;})(rehtona{tsuJbus!;
$_=reverse;s/sub/(reverse"bus").chr(32)/xge;tr~\n~~d;eval;



Mon, 21 Mar 2005 21:20:39 GMT  
 Parse a word into three strings

Quote:

> I have been fiddling with regular expressions off and on all day and I
> get nowhere.  Can anyone help?

Assuming the original string is in the $_ variable:

my($firstChar, $nextFourChars, $theRestOfTheString) = m/(.)(....)(.*)/s;

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Mon, 21 Mar 2005 21:20:40 GMT  
 Parse a word into three strings
Also sprach Ray Frye:

Quote:
> This seems like it should be a novice question.  In fact, it is.  I
> need to take the first character of a string, read it into its own
> string, and delete it from the original string.  Then I need to take
> the next four characters from the original string minus the first
> character and read them into another string, and delete them from the
> original string.

> Say the original string = "E1230UNIFORM"
> What I want to end up with is:
> $firstChar = "E"
> $nextFourChars = "1230"
> $theRestOfThe String = "UNIFORM"

> I have been fiddling with regular expressions off and on all day and I
> get nowhere.  Can anyone help?

Since you have fixed-length records (except for the last) here's yet
another solution using unpack(). This is usually very fast:

    my ($first_char, $next_four, $rest) = unpack "a a4 a*", $string;

Tassilo
--

pam{rekcahbus;})(rekcah{lrePbus;})(lreP{rehtonabus;})(rehtona{tsuJbus!;
$_=reverse;s/sub/(reverse"bus").chr(32)/xge;tr~\n~~d;eval;



Mon, 21 Mar 2005 21:29:46 GMT  
 Parse a word into three strings


Quote:
> Ray Frye announced:
> > This seems like it should be a novice question.  In fact, it is.  I
> > need to take the first character of a string, read it into its own
> > string, and delete it from the original string.  Then I need to take
> > the next four characters from the original string minus the first
> > character and read them into another string, and delete them from the
> > original string.

> > Say the original string = "E1230UNIFORM"
> > What I want to end up with is:
> > $firstChar = "E"
> > $nextFourChars = "1230"
> > $theRestOfThe String = "UNIFORM"

> > I have been fiddling with regular expressions off and on all day and I
> > get nowhere.  Can anyone help?

> > Ray

> why don't you use substr?

Dominick:  I appreciate that suggestion.  It just now made me rethink some
code I'm writing.  I was splitting a string on nothing, then manipulating
the elements of the resulting array.  substr is so much easier.

my $keyword = '';  # trying to get a unique ID for a data record
my $rand = rand;




    versus

$keyword .= substr rand,2,1;

Jim Keenan



Tue, 22 Mar 2005 04:09:57 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. newbie trying to parse one file into three

2. words words words

3. Parsing Lines for Words?

4. Parsing Word to ASCII

5. parsing a template and replacing certain words (from a form)

6. Parsing Word Docs

7. Parsing line of text into words

8. parsing words

9. How to parse MS word documents?

10. ARGV processing of multi-worded strings?

11. Capitalize Words in a String

12. extract the first word in a string

 

 
Powered by phpBB® Forum Software