Similar Threads:
1.Trying to dynamically create arrays, getting can't use string as ARRAY ref
Greets.
I have a 'config file' that contains a group name (alphanumeric) and
machine name/numbers separated by whitespace. Each group is on it's
own line. The file looks like this:
prod01 456 345 234
prod02 789 517 325
...etc, etc, etc...
What I am attempting to do is:
Put the file contents into an array
Pull the first entry off the array
Add the name of the array to a 'master array' list of the machine
groups
Then create another array, which would be referenced by the group
name, and contains the machines associated with the group.
Here is the code I have now that is giving me problems:
# parse the config file
foreach (@filelist){
# for each line of the config file, place the entries
# into a temp array
my @temp=split(/ /);
# pull the first entry off the array
my $group=shift(@temp);
# add the name of the array to the main rg array list
push (@grouplist, $group);
# push the rest of the entries into an array that has
# the same name as the rg in question
foreach (@temp){
push (@$group, $_);
}
}
The idea of the script as a whole is to take the config file and split
it into multiple arrays. Once that is done, connect to each machine
and pull some files into another directory.
The script flow:
-Read the config file, put each line into an array - @temp = qw {prod1
456 345 234}
-Pull the first value from the temp array ($grp) and place it into the
master list - @master = qw {'prod1 prod2}
-Create a new array on the fly (it needs to be this way as new groups
may be created, or old ones removed) with the name of the pulled value
($grp) and place the machine names/numbers into that array - @prod1 =
qw {456 345 234} @prod02 = qw {789 517 325} etc...
-Loop through the master list, using each value as the pointer to the
individual array that contains the machines
-Connect to each machine in turn and pull the files
The script runs if I do not use strict (which I consider a Bad
Thing). But when I run it with strict, I receive the following error:
Can't use string ("prod01") as an ARRAY ref while "strict refs" in use
Am I missing something really basic that is covered in the Llama (3rd
edition), and can anyone give me any hints?
2.comparing 2 arrays strings when the number/name of arrays is dynamic
Hello,
I was wondering if anyone knows how to get around the problem of a perl
array intersection with more than 1 array where the number of arrays,
and there names, can be variable... I have noticed that the code below
and also List::Compare pmm do not allow one to dynamically at run time
build a string for the arrays that need intersecting... does anyone
know how to get around this ? I tried adding a scalar variable
($string) which I build dynamically - the code below seems to want you
to hard code in the names and number of arrays, but again, mine vary
since I am in a loop of sorts, and I need to be able to dynamically
pass a variable number of arrays to be intersected.. any thoughts would
be appreciated.. Thanks, Jack
@Array1 = qw (A B C D G I H);
@Array2 = qw (A C E G H K);
@Array3 = qw (A B C G H L);
@Array4 = qw (A C F G H M);
foreach $element (@Array1, @Array2, @Array3, @Array4)
{ $Hash{$element}++; }
foreach $element (keys %Hash)
{
if ($Hash{$element} == 4)
{ push (@Intersection, $element); }
}
print "@Intersection";
3.Two-dim array: Strange "can't use string as ARRAY ref"
I am using a two-dim hash with a string as first key, and an array as 2nd
key. But it doesn't work! Can anyone tell me what's wrong?
#!/usr/bin/perl -w
$hash{"test"}{["one", "two"]} = 42;
$hash{"test"}{["three", "four"]} = 23;
my $key = "test";
foreach(keys %{$hash{$key}}){
print "Value for $_ is $hash{$key}{$_}\n"; # ARRAY(0x804c958)
# print "@$_\n"; # Can't use string ("ARRAY(0x804c958)") as an ARRAY ref
}
The output is:
Value for ARRAY(0x804c958) is 23
Why does it print only *one* value? Why can't I dereference
the array ref $_?
Thanks!
4.Parse String
I have the following code in Perl
$buf = "<tr><td class=yfnc_tablehead1 width="48%">Change:</td><td
class=yfnc_tabledata1><b style=color:#008800;>2.04
(0.66%)</b></td></tr>"
I am using the following code to get the value 2.04.
if ($buf =~ m/$Change/)
{
($Part1, $Part2) = split (/$Change/, $buf);
# compare with the digit
if ($Part2 =~ m/(\+|-)?[0-9]+\.[0-9]*/)
{
I do not know what to write here to get 2.04
}
}
Can somebody help me out? I know this is the worst way to parse the
string. Is there some better way to solve it?
5.module for parsing a string into a formula
Sorry if I'm not saying this right...
I am coming from a C++ environment, that newsgroup suggested PERL as a
possible answer for my need. I have no experience with PERL at all, so will
have to do my homework if someone says there is a way.
I need a function ( module? ) that can take a string expression "(1 + 2) *
(3 + 4)" and return the answer of "21". It can be either a numerical value,
or a string value does not matter from this point.
Any pointers in the right direction would be welcome. A commercial software
package would be acceptable as well.
Thank you in advance
Michael
6. regex: parsing out varying length substr from varying string
7. Parsing line with similar strings
8. Parse a String that probably really simple to do