Gossamer Forum
Home : General : Perl Programming :

splitting a $ variable into several variables

Quote Reply
splitting a $ variable into several variables
my subject may not be too clear, but i have a variable called $output =
20120608|Friday|08-Jun-2012|Open|zzzzz
20120609|Saturday|09-Jun-2012|Open|zzzzz
20120615|Friday|15-Jun-2012|Open|zzzzz
20120616|Saturday|16-Jun-2012|Open|zzzzz
20120622|Friday|22-Jun-2012|Open|zzzzz
20120623|Saturday|23-Jun-2012|Open|zzzzz
20120629|Friday|29-Jun-2012|Open|zzzzz
20120630|Saturday|30-Jun-2012|Open|zzzzz

each line in the variable ends with \n

i want to read each line individually and split at the | and access each field separately. if i were reading the lines from a file, i know how to do it. but i can't figure out how to split the lines of the variable. i tried

@lines = $output and @lines = split('\n',$output) and several other things. so far, nothing works. help please. thanks.
Quote Reply
Re: [delicia] splitting a $ variable into several variables In reply to
Code:
foreach (split /\n/, $data) {
chomp;
my @vals = split /|/, $_;

print "Field 1 value is: $vals[0], and field 2: $vals[1] etc \n"

}

Hope that helps.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates
Quote Reply
Re: [Andy] splitting a $ variable into several variables In reply to
i had tried something similar to that but it didn't work. i found this somewhere:

my @array = $data =~ /\G(?=.)([^\n]*)\n?/sg;

don't know why but it works. would like to understand if you can explain it to me! thanks
Quote Reply
Re: [delicia] splitting a $ variable into several variables In reply to
Hi,

From what it looks like, its extracting the values into the array using a loop. Personally, I would go with my code (as its more readable). Its untested, but I can't see any reason why it wouldn't work.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates
Quote Reply
Re: [Andy] splitting a $ variable into several variables In reply to
ok, i thought what i had was working but discovered it was not printing the last record.here's what i have now:

foreach (split /\n/, $records) {
chomp;
my @vals = split /|/, $_;

print "Field 1 value is: $vals[0], and field 2: $vals[1] etc \n"

}

this is what it printed:

Field 1 value is: 2, and field 2: 0 etc Field 1 value is: 2, and field 2: 0 etc

it should have printed Saturday 05-may-2012 Saturday 12-may-2012
Quote Reply
Re: [delicia] splitting a $ variable into several variables In reply to
ok, got yours working had to put \ before |

my @vals = split /\|/, $_;

thank you!!!