LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-03-2004, 10:30 AM   #1
phlx
LQ Newbie
 
Registered: Nov 2003
Posts: 10

Rep: Reputation: 0
perl, help perl gurus!


Okay. I've Been tinkering with this code. My goal is to reverse the elements of an array, without using the reverse operator of course.

#!/usr/bin/perl

my @array = (1,2,3);


sub reverse_array {
my $arr_ref = shift;
my $rev_array = [];
foreach my $item (@$arr_ref) {
if (arr_ref($item) eq @array) {
$item = reverse_array($item);
}
unshift @$rev_array, $item;
}
return $rev_array;
}

reverse_array(@array);
print @array;


I am new to perl, so I pasted this together from various sources. A few things I dont understand if anyone could clarify, what does eq and arr_ref function as?
particularly, I don't understand this line of the code
if (arr_ref($item) eq @array)

Also, this code doesn't run successfully. Thanks in advance, Ryan.
 
Old 12-03-2004, 11:40 AM   #2
sigsegv
Senior Member
 
Registered: Nov 2004
Location: Third rock from the Sun
Distribution: NetBSD-2, FreeBSD-5.4, OpenBSD-3.[67], RHEL[34], OSX 10.4.1
Posts: 1,197

Rep: Reputation: 47
Courtesy of google and perlmonks:

Code:
#!/usr/local/bin/perl -w
                                                                                
$i   = 0;
@arr = (1,2,3);
$mid = $#arr/2;
                                                                                
while ($i <= $mid) {
        $tmp        = $arr[$i];
        $rear       = $#arr-$i;
        $arr[$i]    = $arr[$rear];
        $arr[$rear] = $tmp;
        ++$i;
}
                                                                                
print @arr;
To answer your questions about the code you posted -- eq is like == (sort of). arr_ref isn't a function, it's a mistyped array name and index (should be $arr_ref[$item]). The line if (arr_ref($item) eq @array) is checking to see if $arr_ref[$item] is an array, and if it is, recuse into reverse_array with it.
 
Old 12-03-2004, 12:39 PM   #3
phlx
LQ Newbie
 
Registered: Nov 2003
Posts: 10

Original Poster
Rep: Reputation: 0
sigsev, you are my hero

I have another perl question, but I don't want to flood the board with my perl cries so ill just pop it on here.

I'm trying to open a file with a standard format like
/x/y/z
then I want to read all the lines in the file and print an output of columns such that
X Y Z

So how could i scan a line and assign each category to an element in an array ?

Last edited by phlx; 12-03-2004 at 02:35 PM.
 
Old 12-03-2004, 03:28 PM   #4
sigsegv
Senior Member
 
Registered: Nov 2004
Location: Third rock from the Sun
Distribution: NetBSD-2, FreeBSD-5.4, OpenBSD-3.[67], RHEL[34], OSX 10.4.1
Posts: 1,197

Rep: Reputation: 47
Something like this?
Code:
while(<FILE>) {
        # Replace '/' with a tab
        s/\//\t/;
        # Print out the line
        print;
}
 
Old 12-03-2004, 11:06 PM   #5
hk_linux
Member
 
Registered: Nov 2004
Location: India
Distribution: RedHat, PCQLinux, Fedora
Posts: 95

Rep: Reputation: 15
How abt split. If "/" is the delimiter, u can use the following sniipet

$a="1/2/3";
@b = split("/", $a);

The array b, will contain the values 1, 2 and 3.

HTH.
 
Old 12-04-2004, 06:04 PM   #6
phlx
LQ Newbie
 
Registered: Nov 2003
Posts: 10

Original Poster
Rep: Reputation: 0
Well just in case you guys were wondering, here's what i ended up doing
the format of my file was
333-33-3333/First M. Last/2.5/Senior/2005

so i did this

#!/usr/local/bin/perl -w

use Tie::File;

tie @FILE, "Tie::File", 'file' or die $!;

unshift @FILE, "SSN\t\tFirst\tMiddle\tLast\tGPA\tStatus\tGrad. Yr.\n";

for(@FILE) {
s/ /\//g;
s/\//\t/g;

}
untie @FILE;
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with perl module for w3c validator to work on my local Apache+PHP+perl instal tbamt Linux - Software 0 12-16-2004 05:37 PM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
Can any PERL gurus give me a hand, please? WorldBuilder Programming 16 10-23-2003 05:08 PM
perl gurus plz help OutToSea Programming 6 06-24-2003 05:38 PM
chrooting apache v2 (php, ssl, perl support) ; perl configuration markus1982 Linux - Security 3 01-26-2003 06:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration