more useful options PerlMonks

### Re: Str extract the word between the first and the 2nd '|' character

 on Apr 11, 2008 at 16:41 UTC ( #679755=note: print w/replies, xml ) Need Help??

Well, the regex and split will certainly work. I'd like to provide two more options for you.

The first uses index and substr. This has the advantage of being very fast (often faster than regex -- regexes can do more, though). I don't know if it's faster than split (probably not), but there may be reasons for you to use it.

my $string = '(<some word> | No Qualifier | <some word> | <some word>' +; my$start = index( $string, '|' ); # find first pipe position my$end = index( $string, '|',$start+1 );  # find next pipe position

my $qualifier = substr($string, $start,$end-$start+1 ); # extract [download] The second suggestion I have is a bit more robust, and leverages the module Text::CSV_XS. You'd use this approach if you have, e.g. a file of lines that look like your example -- and especially if you're interested in more than one field in that line, and/or if your data might have escaped pipes in it. use IO::File; use Text::CSV_XS; my$csv = Text::CSV_XS->new({ sep_char=>'|' });
my $io = IO::File->new('myfile.piped.txt', '<') or die "Can't read fi +le:$!\n";

until ($io->eof) { my$row = $csv->getline($io);  # read and parse line into an ARRAY
+ref
print "Second field says: ", \$row->[1], "\n";
}
[download]

Even if your data isn't in a file, Text::CSV_XS can parse it for you; it's worth looking into.

Ramblings and references
The Code that can be seen is not the true Code
I haven't found a problem yet that can't be solved by a well-placed trebuchet

Create A New User
Node Status?
node history
Node Type: note [id://679755]
help
Chatterbox?
 [thezip]: I currently don't have access to CYGWIN, else I'd just do a tail -f on the logfile. [Corion]: thezip: If you want to open vim and can live with opening a second console window, use start "The results" vim.exe c:\path\to\logfile .log [thezip]: Ooops... I lied. I guess Cygwin is back. I'll just do a tail -f instead. Better. Sorry for the noise.

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (12)
As of 2017-03-27 18:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should Pluto Get Its Planethood Back?

Results (321 votes). Check out past polls.