473,408 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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
2 2140
KevinADC
4,059 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

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

Similar topics

4
by: JeffM | last post by:
Quick C# question: I have comma delimited values in a string array that I want to pass to seperate variables. Any tips on splitting the array? Thanks in advance! JM
20
by: Opettaja | last post by:
I am new to c# and I am currently trying to make a program to retrieve Battlefield 2 game stats from the gamespy servers. I have got it so I can retrieve the data but I do not know how to cut up...
15
by: Daren | last post by:
Hi, I need to be able to split large string variables into an array of lines, each line can be no longer than 70 chars. The string variables are text, so I would additionally like the lines...
4
by: reshmidoudou | last post by:
a string contains an equation and I have to break the string into integers and other variables : example of the string is "2 + 3 = 5" I have tried to use substr but it does not work as I can...
2
by: CharChabil | last post by:
Using Vb.net 2005, I want to read each part in this string in an array (splitting the string) ----------- A1/EXT "BK82 LB73 21233" 105 061018 1804 ----------- That Code that i used is as follow:...
2
by: shadow_ | last post by:
Hi i m new at C and trying to write a parser and a string class. Basicly program will read data from file and splits it into lines then lines to words. i used strtok function for splitting data to...
37
by: xyz | last post by:
I have a string 16:23:18.659343 131.188.37.230.22 131.188.37.59.1398 tcp 168 for example lets say for the above string 16:23:18.659343 -- time 131.188.37.230 -- srcaddress 22 ...
12
by: kevineller794 | last post by:
I want to make a split string function, but it's getting complicated. What I want to do is make a function with a String, BeginStr and an EndStr variable, and I want it to return it in a char...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.