473,655 Members | 3,072 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about LHS assignment with split()

8 New Member
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 1984
eWish
971 Recognized Expert Contributor
Unless I am misunderstandin g your intent I would use a hash to store the data in lieu of an array.
Nov 4 '07 #2
dtex23
8 New Member
Unless I am misunderstandin g 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 Recognized Expert Specialist
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 New Member
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 New Member
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 Recognized Expert Specialist
whats in $inv?
Nov 4 '07 #7
dtex23
8 New Member
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|qu antity
Nov 4 '07 #8
KevinADC
4,059 Recognized Expert Specialist
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 New Member
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
1559
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 with commas -Compare each word with words in the array -Open file-2 -if the word appears in the array write 'true' in file-2 -if not write 'false' -repeat this for every line in the file-1
4
12739
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 problem following the assignment of the selectednode in the treeview. Here's the relevant code: // separate the node names string split = mcurrentPath.Split(new Char {'\\'});
3
1830
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 function and saved it as 'fileloader.py' def fileload(fname): infile=open(fname) dates = times=
37
2765
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 in a nested scope, on variables from the outer scope: PythonWin 2.4.3 (#69, Mar 29 2006, 17:35:34) on win32. Portions Copyright 1994-2004 Mark Hammond (mhammond@skippinet.com.au) -
20
2124
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 processing can cause excessive tabification. For example, consider the following python snipet...
22
1526
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 does not change the loop
2
4878
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 is: i'm creating a dynamic list of checkboxes, basically to allow access to client information on a user by user basis. i use ajax to bring back a full list of clients, then loop through the list and create a checkbox for each client to...
14
991
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 an array of lenght 2 accepts index 3 which is larger than its lenght? Any thoughts?
0
8296
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8710
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8497
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8598
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7310
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2721
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.