Solved

perl extract "subfields" from text string

Posted on 2013-11-05
3
300 Views
Last Modified: 2013-11-05
Hi,

I have a file with contents as follows:

000000001 ZZZ  $$aTextinA$$bTextinB$$cTextinC
     
There will be several of these, but have just included one for clarity.

For each line, I'm wanting to split the data into variables/fields using a perl script.  Each field is delimited by "$$".  So, for example, from the above I'm wanting to end up with:

$subfield_a = "TextinA"
$subfield_b = "TextinB"
$subfield_c = "TextinC"
           

Equally, if, say, the line was as follows:

000000001 ZZZ  $$aTextinA$$cTextinC
           
I would want to end up with:

$subfield_a = "TextinA"
$subfield_b = ""
$subfield_c = "TextinC"
           
i.e. if a field is not present (in this case $$b)
            
I'm currently doing that as follows (while looping round the file)

@fields = split ('\$\$',$line);
        
	@suba = grep {/^a/} @fields;
        if ( $suba[0] =~ /^a(.*)/ ) {
          $subfield_a = $1;
        } else {
          $subfield_a = "";
        }
        @subb = grep {/^b/} @fields;
        if ( $subb[0] =~ /^b(.*)/ ) {
          $subfield_b = $1;
        } else {
          $subfield_b = "";
        }
	@subc = grep {/^c/} @fields;
        if ( $subc[0] =~ /^c(.*)/ ) {
          $subfield_c = $1;
        } else {
          $subfield_c = "";
        }

Open in new window

           
But I'm guessing there is a far more efficient way to be achievening this?
0
Comment
Question by:yelbow
3 Comments
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 39625525
Try this...
my @tfields = split '\$\$', $line;
shift @tfields; # get rid of "header"
my %fields = (map { m{(\w)(.*)}; $1 => $2 } @tfields);

Open in new window

The %fields hash now looks like this for your two examples:
a => TextInA
b => TextInB
c => TextInC

and
a => TextInA
c => TextInC

Let me know if you have any questions...
0
 

Author Closing Comment

by:yelbow
ID: 39625731
Perfect, an awful lot cleaner - thanks so much
0
 
LVL 84

Expert Comment

by:ozo
ID: 39626319
my %fields = $line=~/\$\$(\w)([^\$\n]*)/g;

Open in new window


Or, if you really want the variables in $subfield_a,  $subfield_b, $subfield_c:
  ${"subfield_$1"}=$2 while $line=~/\$\$(.)([^\$\n]*)/g;
but I would not recommend that method.
better might be
 ${${{a=>\$subfield_a,b=>\$subfield_b,c=>\$subfield_c}}{$1}}=$2 while $line=~/\$\$(.)([^\$\n]*)/g;
better still would be to use %fields instead of $subfield_a,  $subfield_b, $subfield_c
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now