Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Open Source > Perl
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 6th, 2008, 08:47 PM
Friend of Wrox
 
Join Date: Aug 2004
Location: Orange County, CA, USA.
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.



Reply With Quote
  #2 (permalink)  
Old March 7th, 2008, 11:56 AM
Friend of Wrox
Points: 1,515, Level: 15
Points: 1,515, Level: 15 Points: 1,515, Level: 15 Points: 1,515, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2003
Location: Oxford, , United Kingdom.
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

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:

Code:
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:

Code:
>abc123   <
>   345 def<


--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk
Reply With Quote
  #3 (permalink)  
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!!!

#!/usr/bin/perl
use warnings;

my $inVar = " abc123 | 345 def ";

@arr=split(/\|/,$inVar);

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

}

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




Use code above to get rid of your problems
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 04:46 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.