Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Open Source > Perl
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Perl section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old February 6th, 2008, 08:47 PM
Friend of Wrox
Join Date: Aug 2004
Posts: 385
Thanks: 0
Thanked 0 Times in 0 Posts
Default Parsing question:

I am an admitted perl newbie. So please be gentile I bruise easily. I have a very simple question for someone who actually knows perl as I stumble through it. I have a variable and I want to split/parse/seperate it into two variables. One taking all characters before the pipe "|" and the other taking all characters after the "|". For example here is what I have:

$inVar = " abc123 | 345 def ";

what I want to parse it out into is:

$outSplitVar1 = "abc123";
$outSplitVar2 = "345 def";

I want to trim off all leading and trailing spaces but retain any spaces in the middle. Any suggestions other than loop through character by character. I can do that but seek something simpler.

A humbly thank you in advance for any Perl Gods who are willing to waste their time to answer my sniveling unimportant trivial question.

Old March 7th, 2008, 11:56 AM
Friend of Wrox
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts

Hey robprell

Sounds like you need to use regular expressions, list context and split. You can do a google for each of these things. Once you know them, you can do:

my $in = "  abc123   |   345 def   "; # initial thing to split
$in=~s/^\s+//; # strip leading spaces
$in=~s/\s+$//; # strip trailing spaces
my ($out1,$out2) = split /\|/,$in; # split string (nb \| is different to |)
print ">" . $out1 ."<\n>" . $out2 . "<\n" # print the results
Gives us:

>abc123   <
>   345 def<

Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk
Old May 28th, 2010, 09:51 AM
Registered User
Join Date: May 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Solution!!!

use warnings;

my $inVar = " abc123 | 345 def ";


foreach (@arr)
$_ =~ s/^[\s]* (.+\S) [\s]*$/$1/g;


foreach (@arr)
print "$_#######\n";

Use code above to get rid of your problems

Similar Threads
Thread Thread Starter Forum Replies Last Post
Parsing the xml which is having vikkiefd XML 14 July 28th, 2008 05:21 AM
Parsing a String? MBowen SQL Server 2000 9 December 1st, 2006 01:17 PM
Parsing mwviola98 SQL Language 4 July 21st, 2005 10:35 AM
XML Parsing tgopal Javascript 2 July 27th, 2004 08:54 AM
Parsing in PHP Walter G Pro PHP 2 August 14th, 2003 01:54 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.