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 : - ($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 - @list = ($var1, $var2, $var3);
-
@list = ('\$var1', '\$var2', '\$var3');
-
@list = ("\$var1", "\$var2", "\$var3");
to assignments - (@list) = split(/\|/, $listline);
-
$list = "@list"; ($list) = split(/\|/, $listline);
-
-
$list = [ '$var1', '$var2', '$var3');
-
($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 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.
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?
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.
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: -
@LHS = qw/var1 var2 var3 varn/;
-
%lhs = map { $_ => ''; } @LHS;
-
-
my @temp = split(/\|/, $inv);
-
my $i = 0;
-
foreach $lhs (@LHS) {
-
$split{$lhs} = $temp[$i];
-
$i++;
-
}
-
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?
Whoops, one too many lines!
delete this: - %lhs = map { $_ => ''; } @LHS;
-
%lhs = map { $_ => split (/\|/, $line); } @LHS;
When using split(), it was the only way to avoid the map function returning something like - $hash{'var1'} = $var2
-
$hash{'var3'} = $var4
From out on Google, I can see *how* this happens, but am not sure of the *why*.
KevinADC 4,059
Recognized Expert Specialist
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
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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 {'\\'});
|
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=
|
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) -
|
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...
| |
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
|
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...
|
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?
|
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,...
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
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
| |