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

Simple Split? - Special Character

Status
Not open for further replies.

Trancemission

Technical User
Oct 16, 2001
108
GB
Hi

I am attempting to form a split on lines of code with 2 sperators, one is not a problem it is the good old colon ( : )

The second is suqare brackets with a 2 inside ( [2] )

I am having trouble specifying both seperators at the same time:

#looping hrought filehandle LOGFILE......

($oid,$traptype,$trapdesc,$aux) = split(/[:,\[2\]]/);


All line follow the same format, a sample would be:

private.product.firewall:AtackAlarm[2]Trap information:Random info

Elements I want selected are in bold

Many Thanks




Trancemission
=============
If it's logical, it'll work!
 
Try escaping the colon.

"Age is nothing more than an inaccurate number bestowed upon each of us at birth as just another means for others to judge and classify us- sulfericacid
 
Getting there :eek:

For matching the [2] seperator do I need to enclose this inside anything? Only some of the OID's may contain [1] and they seem to be matching also

Cheers

Trancemission
=============
If it's logical, it'll work!
 
Not sure what you used the comma for, but I think your split should look like split /:|\[02\]/

"Age is nothing more than an inaccurate number bestowed upon each of us at birth as just another means for others to judge and classify us- sulfericacid
 
Not sure, re-discovering perl ;)

Many Thanks!

Trancemission
=============
If it's logical, it'll work!
 
split takes a pattern, not a list of types to split. Use something like this: /[:|\[2\]]/

----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Sorry icrf, but your solution is incorrect. You are splitting on each character in the character class, thus the last two fields would be blank.

sulfericacid's solution is the correct one as it doesn't bound the [2] in a character class, but as a string in its own right.

Barbie
Leader of Birmingham Perl Mongers
 
I'm sorry, I didn't really mean to put [] around the whole thing, I know better than that. I just didn't understand the 0 in sulferic's soln. Wouldn't that match [02] and not [2]? Probably just another typo.
Code:
my $str = 'private.product.firewall:AtackAlarm[2]Trap information:Random info';

my ($oid,$traptype,$trapdesc,$aux) = split /:|\[2\]/, $str;
print "$oid\n$traptype\n$trapdesc\n$aux\n";

----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top