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

Substring till a specific character is found

Status
Not open for further replies.

jay25

MIS
Jun 17, 2002
16
US
Hello,

I'm trying to perform a substring on a string variable. I want to extract a part of the string till a specific character is found within the string.(the pipe character '|' in this case)

Currently, I'm using the index function to supply the Length argument:

my $cd = substr($_, 0, index($_,"|"));

Is there any other way to implement this?

Thanks!
 
my ($cd,$crap)=split /\|/, $_;
should do it

--Paul
 
What are you trying to do? What do you want with the character? Everything after the pipe, everything before?
 
Yeah, like Paul said
my ($beforePipe, $afterPipe) = split /\|/, $string;
 
Or using a regex:

my ($cd) = $_ =~ /^([^|]*)|/;
 
Whoops, there should be a backslash in front of that last
pipe --

my ($cd) = $_ =~ /^([^|]*)\|/;

Without the backslash it still stops at the pipe, but if
you have a string with no pipe in it, it captures the entire
string as opposed to nothing, and I don't think that's
what you're looking for.

If you want what comes after the first pipe, too --

my ($cd, $rest) = $_ =~ /^([^|]*)\|(.*)$/;



 
Regexes are powerful, but as with all things there's a time and a place for everything.

For all I know split could be implemented using a regex, but it is a function built specifically for Jay's purpose.

The regex engine needs to be loaded to perform a regex, and for something this simple, that would be a significant overhead (assuming split isn't built on it). There have been comparisons of unpack, vs regexes on this forum before, over a couple of million iterations, and it does make a significant difference. As does substr over regex

Time and a place
--Paul
 
Paul --

I didn't say that my method was superior, nor do I know
that it is, or isn't.

The original post simply asked for "other ways" to do what
the poster was trying to do. Since you and h011ywood did split, I did a regex match as yet "another way".

You're already using a regex in a less obvious way
when you say "split /\|/, $_", aren't you? What's that
between the //'s? Does the regex engine not need to be
loaded for that? Are re's within function calls somehow handled without this? (Not a rhetorical question. I don't know.) If you can enlighten me on this, please do.

Thanks.
 
Mike,

I'm not starting a pi**ing contest here. Honest I'm not.

Between the //'s is the character, or character string, on which to split - very reminiscent of the regex, or pattern matching syntax.

RE's as I've said are very powerful, and some people are very good at building (and testing) them. However, to some of these people it strikes (IMHO) me that they percieve RE's to be an epiphany, and that all code should contain RE's at all costs.

As you've said, you're pointing out other ways to to something which is totally valid. All I was doing was pointing out that there is a function built specifically for Jay's requirements v.v. split.

Best Regards
--Paul

BTW Me and RE's are not the best of friends, as it takes me some time to relearn the syntax everytime I come across one, probably because I came across them so late in life
 
Paul --

No offense taken.

No, I don't think re's are always the best solution, but I do like them. I came to Perl from Awk, and what attracted me to Awk was the re's and hashes ("associative arrays" in Awk), since they allowed me to solve some problems much more easily than I could in other languages that didn't have either. So to me re's are very central to Perl, and it seems strange to me that there are Perl programmers (not you) who are barely familiar with them. I'm not an re guru, just consider myself reasonably competent. (Of course, some real hotshots would demolish that modest claim in no time. Ever looked at Jeffrey Friedel's book Mastering Regular Expressions? Quite wonderful! The man's not an re guru, he's a f*****g re freak!)
 
I'll add that one to my Christmas list

Cheers
--Paul


It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top