By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,691 Members | 1,997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,691 IT Pros & Developers. It's quick & easy.

Splitting string into variables

P: 12
I have a text file that I am reading in with perl line by line and breaking into variables for processing. The lines are being broken on white spaces using split.

Here are a couple of lines from the text file:

bl-render -render /renders/HOE/HD_RAW/REEL_5/reel_5.%.6F.dpx -profile HD_bake -format "Super 16mm" -mask "Full" -range 0-30408 max:hoe:reel_5
bl-render -render /renders/HOE/HD_RAW/REEL_6/reel_6.%.6F.dpx -profile HD_bake -format "Super 16mm" -mask "Full" -range 0-13472 max:hoe:reel_6

Question: is there a way to break each line into variables and keep the " " strings as 1 variable? Simply breaking on white spaces results in "Super 16mm" being broken into 2 variables "Super and 16mm" while I would like it to be one variable "Super 16mm".

Any ideas?


Thanks
Aug 7 '08 #1
Share this Question
Share on Google+
2 Replies


KevinADC
Expert 2.5K+
P: 4,059
What you have is the same as a comma delimited file but with spaces instead of commas. So we can use the code from the Perl Cookbook, parsing comma-seperated data, and substitute a space where the commas would be. (Not well tested):

Expand|Select|Wrap|Line Numbers
  1. my @data;
  2. while (<DATA>) {
  3.    push @data,parse_data($_);
  4. }
  5. print "$_\n" for @data;
  6.  
  7. sub parse_data {
  8.     my $text = shift;
  9.     my @new  = ();
  10.     push(@new, $+) while $text =~ m{
  11.         "([^\"\\]*(?:\\.[^\"\\]*)*)"\s?
  12.            |  ([^ ]+)\s?
  13.            | \s
  14.        }gx;
  15.        push(@new, undef) if substr($text, -1,1) eq ' ';
  16.        return @new;
  17. }  
  18. __DATA__
  19. bl-render -render /renders/HOE/HD_RAW/REEL_5/reel_5.%.6F.dpx -profile HD_bake -format "Super 16mm" -mask "Full" -range 0-30408 max:hoe:reel_5
  20. bl-render -render /renders/HOE/HD_RAW/REEL_6/reel_6.%.6F.dpx -profile HD_bake -format "Super 16mm" -mask "Full" -range 0-13472 max:hoe:reel_6

It does assume there is a single space delimiting the data fields, if there can be more than one space the code would need to be changed a little bit. I have no idea how it will work if a field is blank.

Apply the parse_data() subroutine however is appropriate for your purposes.
Aug 7 '08 #2

P: 12
Thanks Kevin!

I will give this a try. I have access to the Perl Cookbook so I will look it up as well.

Cheers

Alan




What you have is the same as a comma delimited file but with spaces instead of commas. So we can use the code from the Perl Cookbook, parsing comma-seperated data, and substitute a space where the commas would be. (Not well tested):

Expand|Select|Wrap|Line Numbers
  1. my @data;
  2. while (<DATA>) {
  3.    push @data,parse_data($_);
  4. }
  5. print "$_\n" for @data;
  6.  
  7. sub parse_data {
  8.     my $text = shift;
  9.     my @new  = ();
  10.     push(@new, $+) while $text =~ m{
  11.         "([^\"\\]*(?:\\.[^\"\\]*)*)"\s?
  12.            |  ([^ ]+)\s?
  13.            | \s
  14.        }gx;
  15.        push(@new, undef) if substr($text, -1,1) eq ' ';
  16.        return @new;
  17. }  
  18. __DATA__
  19. bl-render -render /renders/HOE/HD_RAW/REEL_5/reel_5.%.6F.dpx -profile HD_bake -format "Super 16mm" -mask "Full" -range 0-30408 max:hoe:reel_5
  20. bl-render -render /renders/HOE/HD_RAW/REEL_6/reel_6.%.6F.dpx -profile HD_bake -format "Super 16mm" -mask "Full" -range 0-13472 max:hoe:reel_6

It does assume there is a single space delimiting the data fields, if there can be more than one space the code would need to be changed a little bit. I have no idea how it will work if a field is blank.

Apply the parse_data() subroutine however is appropriate for your purposes.
Aug 7 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.