Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old August 7th, 2008, 02:31 PM
Newbie
 
Join Date: Dec 2006
Posts: 12
Default 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
Reply
  #2  
Old August 7th, 2008, 06:17 PM
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Posts: 3,662
Default

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.
Reply
  #3  
Old August 7th, 2008, 06:35 PM
Newbie
 
Join Date: Dec 2006
Posts: 12
Default

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




Quote:
Originally Posted by KevinADC
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.
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles