473,385 Members | 1,748 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,385 software developers and data experts.

Question about LHS assignment with split()

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
9 1963
eWish
971 Expert 512MB
Unless I am misunderstanding your intent I would use a hash to store the data in lieu of an array.
Nov 4 '07 #2
dtex23
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
4,059 Expert 2GB
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
dtex23
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
dtex23
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
4,059 Expert 2GB
whats in $inv?
Nov 4 '07 #7
dtex23
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
4,059 Expert 2GB
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
dtex23
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

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

Similar topics

13
by: Hadi | last post by:
Hi, I have two files and the array that contains 20 words in it. I'm trying to write python script that suppose achieve the following: -Open file-1, read the line that contains 20 words separated...
4
by: David Elliott | last post by:
Hi, I have a Windows Explorer-like treeview that displays directories. After a cut/paste operation, I want the treeview to display the folder where the paste happened. But, I'm running into a...
3
by: Luke | last post by:
I'm pretty stuck at the moment and wondering if anyone can spot the problem. Trying to create a function that will read a text file into a list and return that list. I wrote the following...
37
by: Tim N. van der Leeuw | last post by:
Hi, The following might be documented somewhere, but it hit me unexpectedly and I couldn't exactly find this in the manual either. Problem is, that I cannot use augmented assignment operators...
20
by: TimeHorse | last post by:
I would like to gauge interest in the following proposal: Problem: Assignment statements cannot be used as expressions. Performing a list of mutually exclusive checks that require data...
22
by: Cesar G. Miguel | last post by:
I've been studying python for 2 weeks now and got stucked in the following problem: for j in range(10): print j if(True): j=j+2 print 'interno',j What happens is that "j=j+2" inside IF...
2
by: scottSD | last post by:
Hi everyone. this is my first post here, but i've found quite a bit of great information from reading the forums in the past. i'm not sure if what i'm trying to do is possible or not, but here it...
14
by: =?Utf-8?B?RWR3YXJk?= | last post by:
Hi everybody, To me the following code shouldn't work but it does ! Imports system.String Dim x As String="This,is,a,test" Dim y(1) As String y=x.Split(",") TextBox1.text=y(3) why "Y" which is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?

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.