arrays
Steve Lane
sml at zfx.com
Sun Jan 27 13:52:49 CST 2002
R Dicaire wrote:
> Hi, I've been lurking on the list since last summer, and have a
> question.
> I'm trying to get the contents of 'uname -a' into an array.
> I have:
>
> #/usr/bin/perl
> @a=`uname -a`;
> print "$a[0] $a[1] $a[10]";
>
> But this isn't working as I thought it would, its not printing the
> selected
> array elements. Any ideas?
when you use backticks in list context, they
return a list of -lines- that the external
command printed. i think you are expecting
for @a to contain a list of -words- that
`uname -a` returns.
since `uname -a` prints only one line, that
entire line goes into $a[0], and so your print
statement prints the entire `uname -a` result.
to get words, you need to first grab the line
that `uname -a` prints, and then split it into
words. try this:
#/usr/bin/perl -w
use strict;
my $uname = `uname -a`;
chomp $uname;
my @words = split / /, $uname;
print "OS( $words[0] ) HOSTNAME( $words[1] ) ARCH( $words[10] )\n";
__END__
for more, see:
man perlop => qx/STRING/ or `STRING`
perldoc -f split
--
Steve Lane <sml at zfx.com>
http://knoxville.pm.org/
More information about the Knoxville-pm
mailing list