[Perl] split on : but with extra challenge


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Perl] split on : but with extra challenge
# 1  
Old 03-15-2016
[Perl] split on : but with extra challenge

Hi,

I am looking for a little bit more advanced split.

Code:

Code:
#!/usr/bin/perl -w #-d
use strict;

my $Line = "value1:{value2a:value2b}:value3:{}:value5:{value6a:value6b}";

my @LineAttributes = split (/:/, $Line);

my $TotalLineAttributes = scalar @LineAttributes;

print "Line: $Line\n\n";
print "Total attributes: $TotalLineAttributes\n\n";

foreach my $Attribute (@LineAttributes) {
  print "Attribute: $Attribute\n";
}

Result:

Code:
Line: value1:{value2a:value2b}:value3:{}:value5:{value6a:value6b}

Total attributes: 8

Attribute: value1
Attribute: {value2a
Attribute: value2b}
Attribute: value3
Attribute: {}
Attribute: value5
Attribute: {value6a
Attribute: value6b}

I would like to see 6 as result.
The length of the line and the number of values my vary.

Any ideas?

Greetings,

Erik
# 2  
Old 03-15-2016
try:
Code:
#!/usr/bin/perl -w #-d
use strict;

my $Line = "value1:{value2a:value2b}:value3:{}:value5:{value6a:value6b}";
$Line=~s/({[^:}]+):([^}]+})/$1!=!$2/g;

my @LineAttributes = split (/:/, $Line);
$Line="";
for (@LineAttributes) {s/!=!/:/g; $Line=$Line . $_ . ":"};
$Line=~s/:$//;

my $TotalLineAttributes = scalar @LineAttributes;

print "Line: $Line\n\n";
print "Total attributes: $TotalLineAttributes\n\n";

foreach my $Attribute (@LineAttributes) {
  print "Attribute: $Attribute\n";
}

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 03-16-2016
Thanks a lot.
Works like a charm.
So the lesson is to substitute the colons that are in the way and later on to substitue them back again.

---------- Post updated at 10:30 AM ---------- Previous update was at 09:18 AM ----------

One additional question:

What if the number of values found within {} is variable ?

For example:

$Line = "value1:{value2a:value2b}:value3:{}:value5:{value6a:value6b}";

$Line = "value1:{value2a:value2b}:value3:{}:value5:{value6a:value6b:value6c}";

$Line = "value1:{value2a:value2b}:value3:{}:value5:{value6a:value6b:value6c:value6d}";

my $Line = "value1:{value2a:value2b}:value3:{}:value5:{value6a:value6b}";

For the moment I am only interested in the value of $TotalLineAttributes.
No need to retrieve the actual attributes.

---------- Post updated at 11:42 AM ---------- Previous update was at 10:30 AM ----------

I will use this until something better comes along:

$Line =~ s/{(.*?)}/XxXxXxXxXxXxXxXx/g;
# 4  
Old 03-16-2016
try:
Code:
#!/usr/bin/perl -w #-d
use strict;

my $Line = "value1:{value2a:value2b}:value3:{}:value5:{value6a:value6b:value6c:value6d}";

my @atts = ($Line =~ /{[^:}]*:[^}]*}/g);
for (@atts) {my $v=$_; $v=~s/:/!=!/g; $Line=~s/$_/$v/;};

my @LineAttributes = split (/:/, $Line);
$Line="";
for (@LineAttributes) {s/!=!/:/g; $Line=$Line . $_ . ":"};
$Line=~s/:$//;

my $TotalLineAttributes = scalar @LineAttributes;

print "Line: $Line\n\n";
print "Total attributes: $TotalLineAttributes\n\n";

foreach my $Attribute (@LineAttributes) {
  print "Attribute: $Attribute\n";
}

This User Gave Thanks to rdrtx1 For This Post:
# 5  
Old 03-16-2016
Thanks a lot for your time!
Works perfectly.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl split and array

Hello, I have the following code: while ($line = <fd_in>) { 126 $line = " " . $line ; 127 print "our_line:$line\n"; 128 @list = split (/\s+/, $line) ; 129 print "after_split:@list\n"; 130 print "$list\t$list\t$list\t$list\t$list\t$list$list\t\n"; 131 $len =... (2 Replies)
Discussion started by: Zam_1234
2 Replies

2. UNIX for Advanced & Expert Users

Interesting awk/Perl/sed parsing challenge

I have a log with entries like: out/target/product/imx53_smd/obj/STATIC_LIBRARIES/libwebcore_intermediates/Source/WebCore/bindings/V8HTMLVideoElement.cpp : target thumb C++: libwebcore <=... (8 Replies)
Discussion started by: glev2005
8 Replies

3. Shell Programming and Scripting

Split the string in perl

Hi All, How to split the string KAR_Celltick_Ban_GSMGW3 and want to pickup the third filed. Sometime the string may be "KAR_Celltick_Ban" like this Thanks in advance (1 Reply)
Discussion started by: sujit_kashyap
1 Replies

4. Shell Programming and Scripting

Regular Expression in Perl - very challenge

Hi expert, As we know in regular expression, means neither a nor b. Now the question is how to consider <ab> as a whole part or somehow a single char. Something like which is meaning neither ( nor a nor b nor ). Or do we have another way for this? The real requirement is as below: ... (2 Replies)
Discussion started by: summer_cherry
2 Replies

5. Shell Programming and Scripting

Perl split, but ignoring extra delimiters

Hi all, I'm going bonkers trying to figure something out that is probably simple for most of you. I have a choice between getting therapy for this or coming here to ask for help. I chose the latter. :D What I'm trying to do is perform a split on a line but only one split. For example, let's... (4 Replies)
Discussion started by: goober
4 Replies

6. Homework & Coursework Questions

PERL split function

Hi... I have a question regarding the split function in PERL. I have a very huge csv file (more than 80 million records). I need to extract a particular position(eg : 50th position) of each line from the csv file. I tried using split function. But I realized split takes a very long time. Also... (0 Replies)
Discussion started by: castle
0 Replies

7. Shell Programming and Scripting

Perl split question

hi, I have a seemingly really stupid question, but here goes! What do you enter into split delimiter to seperate something like this "December 12, 1995" and get December 12 1995 ? thanks (5 Replies)
Discussion started by: ade214
5 Replies

8. Shell Programming and Scripting

PERL cgi script... extra character driving me crazy

I'm using a PERL cgi script that uses rrdtool to make graphs. I can't get the syntax correct to use a degree sign (alt+0176 like this °) and also using a variable. If I use single quotes, I can't call the variable. If I use double quotes, there is an extra symbol (Â) before the ° which goes... (2 Replies)
Discussion started by: audiophile
2 Replies

9. Shell Programming and Scripting

Use split function in perl

Hello, if i have file like this: 010000890306932455804 05306977653873 0520080417010520ISMS SMT ZZZZZZZZZZZZZOC30693599000 30971360000 ZZZZZZZZZZZZZZZZZZZZ202011302942311 010000890306946317387 05306977313623 0520080417010520ISMS SMT ZZZZZZZZZZZZZOC306942190000 30971360000... (5 Replies)
Discussion started by: chriss_58
5 Replies

10. Shell Programming and Scripting

perl split function

$mystring = "name:blk:house::"; print "$mystring\n"; @s_format = split(/:/, $mystring); for ($i=0; $i <= $#s_format; $i++) { print "index is $i,field is $s_format"; print "\n"; } $size = $#s_format + 1; print "total size of array is $size\n"; i am expecting my size to be 5, why is it... (5 Replies)
Discussion started by: new2ss
5 Replies
Login or Register to Ask a Question