467,146 Members | 1,047 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,146 developers. It's quick & easy.

Splitting string into variables

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
  • viewed: 1892
Share:
2 Replies
KevinADC
Expert 2GB
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
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.

Similar topics

20 posts views Thread by Opettaja | last post: by
2 posts views Thread by CharChabil | last post: by
2 posts views Thread by shadow_ | last post: by
37 posts views Thread by xyz | last post: by
12 posts views Thread by kevineller794@gmail.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.