Re: using split on every element in an array
by Fletch (Bishop) on Jan 17, 2003 at 15:15 UTC
|
You're calling split() in a void context, so whatever returned values it may be generating are just being discarded. You'd want to do something like push @split_data, split( /\t/ ) to save them off.
Or even better, look at Text::CSV_XS and see if that'll handle your data parsing needs.
| [reply] [d/l] [select] |
|
Is it possible to automatically convert every element of an array into a newly named scalar variable (each of which has a different name)??
| [reply] |
|
Every element of an array is a named scalar variable.
Eg. $array[3] = 'something'; $var = $array[4]; etc.
You can therefore use them directly with split.
Eg.
for my $element (@array) {
my @bits = split "\t", $element;
#do something with the bits.
}
The contents of @array will remain unmodified for further use later unless you assign something to $element.
It might be clearer to you written this way.
for my $index (0 .. $#array) { # $#array is the numer of the highest e
+lement.
my @bits = split "\t", $array[$index];
}
The first version is usually considered better though.
Examine what is said, not who speaks.
The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead. | [reply] [d/l] [select] |
|
It's possible, of course, but whenever you're thinking of creating a gazillion separate variables dynamically, you're usually better off using an array or hash. I'm not sure exactly what you want to do, but are you thinking of something like this?
foreach my $item (@data) {
push @data_split, [split /\t/, $item];
}
Then you will have an array of arrayrefs that contain each line's data split on tabs. And @data is kept intact for you.
-- Mike
--
just,my${.02} | [reply] [d/l] [select] |
|
I think you want something like the following inside your foreach loop.
my ($name, $first_name, $tel, @rest) = split /\t/, $item;
The array @rest is there as you are saying that you have a varying number of elements in each $item. Another alternative might be to just specify all possible elements and then check later if the variables are defined. Like this
my ($name, $first_name, $tel, $address, $email, $last_field) = split /
+\t/, $item;
if (defined $last_field) {
# do something
}
-- Hofmator | [reply] [d/l] [select] |
|
| [reply] |
|
| [reply] |
Re: using split on every element in an array
by VSarkiss (Monsignor) on Jan 17, 2003 at 15:32 UTC
|
Not sure if this is exactly what you're looking for, but you could try something like this:
my @new = map { split /\t/ } split /\n/, $data;
Another way to do it is to split on either character:
my @new = split /\n|\t/, $data;One of these will probably do what you want.
Note, I haven't tested either one.
| [reply] [d/l] [select] |
|
I'd think to preserve lines you'd want to use a variation, like:
my @new = map { [ split(/\t/) ] } split(/\n/, $data);
This way you end up with an array of arrays, one for each line in the file.
| [reply] [d/l] |
Re: using split on every element in an array
by thezip (Vicar) on Jan 17, 2003 at 16:02 UTC
|
If you're reading from a file, you can also read a single line at-a-time (instead of reading it _all_ and then splitting on the newlines), as in:
open(FH, $filename) or die qq(Could not open file "$filename" for read
+ing.\n);
while(<FH>) {
# my $line = $_;
# my @arr = split(\t/, $line);
# -or-
my @arr = split(/\t/); # split defaults to $_
# process @arr values for a single line
# ...
}
close FH;
Where do you want *them* to go today? | [reply] [d/l] |
Re: using split on every element in an array
by OM_Zen (Scribe) on Jan 17, 2003 at 19:58 UTC
|
while(<fname>){
chomp;
@data = split(/\\t/);
print "[$_]\n";
map{print "[$_]\n"}@data;
}
| [reply] [d/l] |