The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl Question serm Shell Programming and Scripting 2 09-28-2006 05:50 AM
perl question BG_JrAdmin Shell Programming and Scripting 1 09-09-2005 07:46 PM
Perl Question Gary Dunn Shell Programming and Scripting 2 12-03-2004 12:37 PM
PERL question frank Shell Programming and Scripting 1 06-18-2002 03:13 AM
Perl question Jubba Shell Programming and Scripting 1 04-01-2002 02:24 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-01-2007
hankooknara hankooknara is offline
Registered User
  
 

Join Date: Dec 2006
Posts: 80
Perl question regarding [ ]

Below program, I do not get why item I am looking for is [6], instead of [5].
When I do $#text, i get the right value for $value1, but when I do [5], i get somsething4, instead of
somsethingxxxxxxxxxxxxxxxxxxx(which is what I am looking for.

when I do [0], I get empty.. why? what did I do wrong? can you please help?

thanks.

#!/usr/bin/perl
my $value1;

while(<>)
{
my $line = $_;
(@text) = split(/\s+/,$line);
if($line =~ /Time/)
{
#$value1 = $text[6];
$value1 = $text[$#text];


Top of the file below
something somsething somsething something4 somethingxxxxxxxxxxxxx
  #2 (permalink)  
Old 07-01-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,416
If the line starts with spaces, the split command gives an empty value for the $text[0].

Code:
#!/usr/bin/perl

while(<>)
{
   my $line = $_;
   chop($line);
   (@text) = split(/\s+/,$line);
   print "Line='$line'\n";
   print "\$#text=$#text\n";
   print "\$text[\$#text]]='$text[$#text]'\n";
   foreach (0..$#text) { print "\$text[$_]='$text[$_]'\n"; }
}
Input file:
Code:
something somsething somsething something4 somethingxxxxxxxxxxxxx
          something somsething somsething something4 somethingxxxxxxxxxxxxx
Output:
Code:
Line='something somsething somsething something4 somethingxxxxxxxxxxxxx'
$#text=4
$text[$#text]]='somethingxxxxxxxxxxxxx'
$text[0]='something'
$text[1]='somsething'
$text[2]='somsething'
$text[3]='something4'
$text[4]='somethingxxxxxxxxxxxxx'
Line='          something somsething somsething something4 somethingxxxxxxxxxxxxx'
$#text=5
$text[$#text]]='somethingxxxxxxxxxxxxx'
$text[0]=''
$text[1]='something'
$text[2]='somsething'
$text[3]='somsething'
$text[4]='something4'
$text[5]='somethingxxxxxxxxxxxxx'
  #3 (permalink)  
Old 07-01-2007
hankooknara hankooknara is offline
Registered User
  
 

Join Date: Dec 2006
Posts: 80
thank you as always~~
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:13 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0