By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,795 Members | 1,216 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,795 IT Pros & Developers. It's quick & easy.

Question about LHS assignment with split()

P: 8
Good afternoon all,

Question about list assignment in perl...

Given a piece of code that looks kinda like this for parsing some '|' delimited input text lines :
Expand|Select|Wrap|Line Numbers
  1. ($var1, $var2, $var3) = split(/\|/, $listline);
Here's the hard part: There are differing programs that read from the same conf file that take data stream and split it, but at some point in the future I may need to expand the variables split to. I would like to basically put the LHS in the conf file and refer to it as need be.

Trying things like
Expand|Select|Wrap|Line Numbers
  1. @list = ($var1, $var2, $var3);
  2. @list = ('\$var1', '\$var2', '\$var3');
  3. @list = ("\$var1", "\$var2", "\$var3");
to assignments
Expand|Select|Wrap|Line Numbers
  1. (@list) = split(/\|/, $listline);
  2. $list = "@list";  ($list) = split(/\|/, $listline);
  3.  
  4. $list = [ '$var1', '$var2', '$var3');
  5. ($list) = split(/\|/, $listline);
doesn't work. Passing into a function wouldn't really work because I would like the variables returned in a dynamic, single-point list (this sounds really stupid as I type it, so I think I could be convinced otherwise.)

I would be grateful if someone could tell me what I am seeing wrong and/or point to something other than perlref 'cuz I just don't seem to get it.

Thanks!

DJ
email removed for users protection
Nov 3 '07 #1
Share this Question
Share on Google+
9 Replies


eWish
Expert 100+
P: 971
Unless I am misunderstanding your intent I would use a hash to store the data in lieu of an array.
Nov 4 '07 #2

P: 8
Unless I am misunderstanding your intent I would use a hash to store the data in lieu of an array.
It doesn't really matter what type is used. All I am looking for it the ability to have (the array of stuff) on the LHS be set from a file somewhere because the call is used in several diff. programs, and going like
($hash{'var1'}, $hash{'var2'}, etc.)
leaves me in the same boat, no?
Nov 4 '07 #3

KevinADC
Expert 2.5K+
P: 4,059
With a hash you can create the left hand side beforehand: the names of the keys.

# a list of variable names (these are just simple strings):
@LHS = qw(var1 var2 var3 var4 var5 ......... var100);

# convert it into a hash ready for accepting data:
%LHS = map {$_ => '' } @LHS;

now you scripts can use whichever of the hash keys it needs to use.
Nov 4 '07 #4

P: 8
With a hash you can create the left hand side beforehand: the names of the keys.

# a list of variable names (these are just simple strings):
@LHS = qw(var1 var2 var3 var4 var5 ......... var100);

# convert it into a hash ready for accepting data:
%LHS = map {$_ => '' } @LHS;

now you scripts can use whichever of the hash keys it needs to use.


OK, using code that looks alot like this:

Expand|Select|Wrap|Line Numbers
  1.   @LHS = qw/var1 var2 var3 varn/;
  2.   %lhs = map { $_ => ''; } @LHS;
  3.  
  4.   my @temp = split(/\|/, $inv);
  5.   my $i = 0;
  6.   foreach $lhs (@LHS) {
  7.     $split{$lhs} = $temp[$i];
  8.     $i++;
  9. }
  10.  
I got the program working.... but the solution with the temp array and iteration over it seems so inelegant. The program still works great (roughly the same 1.5 secs doing all it's parsing/transforming over a 15k text file). Did I miss something easier/better?
Nov 4 '07 #5

P: 8
Whoops, one too many lines!

delete this:
Expand|Select|Wrap|Line Numbers
  1. %lhs = map { $_ => ''; } @LHS;
  2. %lhs = map { $_ => split (/\|/, $line); } @LHS;
When using split(), it was the only way to avoid the map function returning something like
Expand|Select|Wrap|Line Numbers
  1. $hash{'var1'} = $var2
  2. $hash{'var3'} = $var4
From out on Google, I can see *how* this happens, but am not sure of the *why*.
Nov 4 '07 #6

KevinADC
Expert 2.5K+
P: 4,059
whats in $inv?
Nov 4 '07 #7

P: 8
whats in $inv?
Sorry... $inv is the actual line from the program (I didn't generic it to $line).

For what it's worth, the actual lines read something like
part number|location|description

What I have been worried about thru the whole affair is the ability to change it to something like
part number|location|description|quantity
Nov 4 '07 #8

KevinADC
Expert 2.5K+
P: 4,059
I don't see what the problem is really. Data often changes after a program has been written. When that happens you generally have to go back and update the program.
Nov 5 '07 #9

P: 8
I don't see what the problem is really. Data often changes after a program has been written. When that happens you generally have to go back and update the program.
No problem, just looking for ease of maintenance
Nov 6 '07 #10

Post your reply

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