CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Storing a variable amount of text?

 



ofmike
New User

Dec 9, 2011, 8:25 AM

Post #1 of 2 (528 views)
Storing a variable amount of text? Can't Post

Hello all,

I'm trying to store some text into a variable...
The text is in between double quotes but the length is variable. Also I need to ignore a line if it contains a certain value in those double quotes, such as "CODE". The remaining line's text needs to be stored in a different variable.

so here would be the example file:
"CODE"
"abc","other fields"
"abcdef","other fields"
"abcdefghij","other fields"

Previously the fields were fixed 16 characters so I did this:
my $1stfield = substr($line,0,16);
my $remaining = substr($line,16);

Could someone please help me solve this? Many thanks for your time.


BillKSmith
Veteran

Dec 9, 2011, 12:32 PM

Post #2 of 2 (494 views)
Re: [ofmike] Storing a variable amount of text? [In reply to] Can't Post


Code
use strict; 
use warnings;
while (<DATA>) {
next if /^"CODE"/;
(my $firstfield, my $remaining) = /^"([^"]*)"(.*)$/;
print $firstfield, "<-->", $remaining, "\n";
}
__DATA__
"CODE"
"abc","other fields"
"abcdef","other fields"
"abcdefghij","other fields"

Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives