Home > Archive > PERL Beginners > November 2007 > Split string into array
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Split string into array
|
|
| AndrewMcHorney 2007-11-17, 10:02 pm |
| Hello
I now got my directory listing into an array of strings. I would like
to now split up the string into a an array of strings from the
original string. For example, I might have a string of "a b c d"
and I now want a 4 element array containing "a", "b", "c", 'd".
I did the following but it did not work.
@new_dir = @string_array[$index];
The new_dir array is the string from the string array.
Where did I go wrong?
Andrew
| |
| Tom Phoenix 2007-11-17, 10:02 pm |
| On 11/16/07, AndrewMcHorney <andrewmchorney@cox.net> wrote:
> I now got my directory listing into an array of strings. I would like
> to now split up the string into a an array of strings from the
> original string. For example, I might have a string of "a b c d"
> and I now want a 4 element array containing "a", "b", "c", 'd".
>
> I did the following but it did not work.
>
> @new_dir = @string_array[$index];
What's wrong with it? Was that just some Perl-like code that you typed
at random, or was there some reason to think that it would do what you
want?
> The new_dir array is the string from the string array.
>
> Where did I go wrong?
Do you want split?
my @pieces = split / /, "a b c d";
I feel that I'm guessing (badly?) at what you want. Am I close? If
you're not trying to break up a string on space, what are you trying
to do?
Hope this helps!
--Tom Phoenix
Stonehenge Perl Training
| |
| AndrewMcHorney 2007-11-18, 4:01 am |
| Tom
I thought taking a string and assigning it to an array would do it
and I was wrong.
Here is a sample line of source;
"2004-08-03 23:57 1,712,128 GdiPlus.dll" and the
recommended split gives me 13 for the number of items. I would like
an array that has "2004-08-03", "23:57","1,712,128","GdiPlus.dll" as
the elements of the array.
Thanks,
Andrew
At 17:40 2007-11-17, Tom Phoenix wrote:
>On 11/16/07, AndrewMcHorney <andrewmchorney@cox.net> wrote:
>
>
>What's wrong with it? Was that just some Perl-like code that you typed
>at random, or was there some reason to think that it would do what you
>want?
>
>
>Do you want split?
>
> my @pieces = split / /, "a b c d";
>
>I feel that I'm guessing (badly?) at what you want. Am I close? If
>you're not trying to break up a string on space, what are you trying
>to do?
>
>Hope this helps!
>
>--Tom Phoenix
>Stonehenge Perl Training
>
>--
>To unsubscribe, e-mail: beginners-unsubscribe@perl.org
>For additional commands, e-mail: beginners-help@perl.org
>http://learn.perl.org/
>
>
>
>
>--
>No virus found in this incoming message.
>Checked by AVG Free Edition.
>Version: 7.5.503 / Virus Database: 269.16.0/1136 - Release Date:
>2007-11-17 14:55
| |
| Tom Phoenix 2007-11-18, 4:01 am |
| On 11/16/07, AndrewMcHorney <andrewmchorney@cox.net> wrote:
> Here is a sample line of source;
>
> "2004-08-03 23:57 1,712,128 GdiPlus.dll" and the
> recommended split gives me 13 for the number of items. I would like
> an array that has "2004-08-03", "23:57","1,712,128","GdiPlus.dll" as
> the elements of the array.
Well, that's not source. :-) But it sounds as if you want to use
/\s+/ as your split pattern. Or maybe /\x20+/, if you want to split
only on spaces. Does that help you past where you're stuck, writing
the pattern for split?
Cheers!
--Tom Phoenix
Stonehenge Perl Training
| |
| John W . Krahn 2007-11-18, 4:01 am |
| On Friday 16 November 2007 17:24, AndrewMcHorney wrote:
> Hello
Hello,
> I now got my directory listing into an array of strings. I would like
> to now split up the string into a an array of strings from the
> original string. For example, I might have a string of "a b c d"
> and I now want a 4 element array containing "a", "b", "c", 'd".
>
> I did the following but it did not work.
>
> @new_dir = @string_array[$index];
Found in /usr/lib/perl5/5.6.0/pod/perlfaq4.pod
What is the difference between $array[1] and @array[1]?
The former is a scalar value, the latter an array slice,
which makes it a list with one (scalar) value. You should
use $ when you want a scalar value (most of the time) and
@ when you want a list with one scalar value in it (very,
very rarely; nearly never, in fact).
Sometimes it doesn't make a difference, but sometimes it
does. For example, compare:
$good[0] = `some program that outputs several lines`;
with
@bad[0] = `same program that outputs several lines`;
The `use warnings' pragma and the -w flag will warn you
about these matters.
John
--
use Perl;
program
fulfillment
| |
| Oryann9 2007-11-18, 8:00 am |
|
Tom
I thought taking a string and assigning it to an array would do it
and I was wrong.
Here is a sample line of source;
"2004-08-03 23:57 1,712,128 GdiPlus.dll" and the
recommended split gives me 13 for the number of items. I would like
an array that has "2004-08-03", "23:57","1,712,128","GdiPlus.dll" as
the elements of the array.
Thanks,
Andrew
At 17:40 2007-11-17, Tom Phoenix wrote:
>On 11/16/07, AndrewMcHorney <andrewmchorney@cox.net> wrote:
>
like[color=darkred]
>
>What's wrong with it? Was that just some Perl-like code that you typed
>at random, or was there some reason to think that it would do what you
>want?
>
>
>Do you want split?
>
> my @pieces = split / /, "a b c d";
>
>I feel that I'm guessing (badly?) at what you want. Am I close? If
>you're not trying to break up a string on space, what are you trying
>to do?
>
>Hope this helps!
>
>--Tom Phoenix
>Stonehenge Perl Training
>
Tom is right as usual, so not sure what you are about.
/etc/skel$ perl -le 'my $string=q/2004-08-03 23:57 1,712,128 GdiPlus.dll/;print "\n$string\n";
> my @array=split /\s/, $string; print @array;'
2004-08-03 23:57 1,712,128 GdiPlus.dll
2004-08-0323:571,712,128GdiPlus.dll
/etc/skel$ perl -le 'my $string=q/2004-08-03 23:57 1,712,128 GdiPlus.dll/;print "\n$string\n";
my @array=split /\s/, $string; print $array[2];'
2004-08-03 23:57 1,712,128 GdiPlus.dll
1,712,128
#################
# The meaning of split #
#################
split / /, $foo;
and
split ' ', $foo;
are not the same thing.
split ' ', $foo is a special case that means to split on all sequences of whitespace.
It means the same thing as split /\s+/, $foo;
________________________________________
________________________________________
____
Never miss a thing. Make Yahoo your home page.
http://www.yahoo.com/r/hs
| |
| Dr.Ruud 2007-11-19, 10:02 pm |
| "Tom Phoenix" schreef:
> my @pieces = split / /, "a b c d";
split() has a great default mode, that you get when you supply a string
value, only containing a single space, as the first parameter.
my @pieces = split " ", " a b c d ";
--
Affijn, Ruud
"Gewoon is een tijger."
|
|
|
|
|