Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

perl comapring lines in an array

Original Message
Name: subodheee
Date: February 1, 2008 at 06:51:05 Pacific
Subject: perl comapring lines in an array
OS: win xp professional
CPU/Ram: intelx86
Model/Manufacturer: -
Comment:
i am new to perl, i just started programming in it ,so initially i have a problem like comparing lines within array like [see after question also ]
question :
op offset
ld2 bd8290
ld3 33c1f54
ld2 bd8290
st2 33c1f54
st2 33c1f54
st4 bd8290

i took op in one array,offset in other array. if same op,same offset is there than that line is equal else not.i stuck in comparing op,offset with other line . please u r help is apreciated

subodh .


Report Offensive Message For Removal


Response Number 1
Name: FishMonger
Date: February 1, 2008 at 09:43:26 Pacific
Subject: perl comapring lines in an array
Reply: (edit)
This sounds like a homework assignment, so I don't feel comfortable in simply providing you with the answer. However, I will assist you in fixing your script.

Please post your script and point out the portion that is giving you a problem.


Report Offensive Follow Up For Removal

Response Number 2
Name: FishMonger
Date: February 1, 2008 at 09:45:16 Pacific
Subject: perl comapring lines in an array
Reply: (edit)
As a hint, it would be easier and more efficient to use a hash instead of 2 arrays.

Report Offensive Follow Up For Removal

Response Number 3
Name: subodheee
Date: February 1, 2008 at 20:45:11 Pacific
Subject: perl comapring lines in an array
Reply: (edit)
#!usr/bin/perl
use strict;
#use warnings;
open FILE,"$ARGV[0]";
my @file_array = <FILE>;
close(FILE);
my $i=0;
my @array;
my @scull;


foreach(@file_array) {


if($file_array[$i] =~ /=== Memops on thread 0 ===/)
{
for(my $j=0;$j<128;$j++)
{
@array = split(" ", $file_array[$i+2+$j]);
@scull = $array[1]."\t".$array[4];
#print "\n",@scull;
program(@scull);

}

}

$i++;

}

after this i dont know how to compare 1line with all other lines ,i am new to hashes and perl .

subodh .


Report Offensive Follow Up For Removal

Response Number 4
Name: FishMonger
Date: February 2, 2008 at 13:34:14 Pacific
Subject: perl comapring lines in an array
Reply: (edit)

#!usr/bin/perl
use strict;

So far, so good

#use warnings;

Why did you comment that out?
The warnings pragma is used to help point out problems.

open FILE,"$ARGV[0]";

You should always check the return code of an open call and take proper action if it fails.
open FILE, $ARGV[0] or die "can't open $ARGV[0] $!";

or use the 3 arg form of open and a lexical var for the filehandle.
open my $FILE, '<', $ARGV[0] or die "can't open $ARGV[0] $!";

my @file_array = <FILE>;
close(FILE);

Slurrping the data into an array is very rarely the best/proper approach.

my $i=0;
my @array;
my @scull;


foreach(@file_array) {


if($file_array[$i] =~ /=== Memops on thread 0 ===/)
{
for(my $j=0;$j<128;$j++)
{
So, you're iterating 128 times for every line in the file!
That is the most inefficient approach to proccess the array.

@array = split(" ", $file_array[$i+2+$j]);
@scull = $array[1]."\t".$array[4];

@scull will have a single element, so either use a scalar or use push to add it to the array.
$array[4] is the fifth field, but in your sample data, you only have 2 fields.

What does your actual data look like and exactly what are you needing to compare?

#print "\n",@scull;
program(@scull);

program() is not a built-in Perl function, so did you create a subroutine, and if so, what does it do?

}

}

$i++;

}

As a short example, here's one way to find duplicate lines.

#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

my %hash;

while ( <DATA> ) {
chomp;
$hash{$_}++;
}
print Dumper \%hash;


__DATA__
ld2 bd8290
ld3 33c1f54
ld2 bd8290
st2 33c1f54
st2 33c1f54
st4 bd8290


Here's what it will output.
$VAR1 = {
'ld2 bd8290' => 2,
'ld3 33c1f54' => 1,
'st2 33c1f54' => 2,
'st4 bd8290' => 1
};


Report Offensive Follow Up For Removal

Response Number 5
Name: subodheee
Date: February 3, 2008 at 00:48:04 Pacific
Subject: perl comapring lines in an array
Reply: (edit)
hi fishmonger for the same problem i want to do in this way
num op offset
pcu_0 ld 24356566
pcu_1 ld 24356566
pcu_1 st 32323254
pcu_0 st 32323254 this is question my appraoch to this is
after splitting based on spaces
%hash =$array[1].$array[2];
my $var =$array[0];
foreach my $key (keys %hash) {
if ($var eq pcu-0 ) {
my $1 = substr($key,9);to get addreess
my $2= substr ($key,-10,4);
#to get ld ,or st;#this way its not working i had so many issues like this way
if ($1 eq ld) {
do some operation
}
if ($2 &$1 exist in pcu_0and pcu_1 ) {
than its shared line}
else
{ print some other operation
}
i want to compare op,offset with other line,may it is in pcu_0 or pcu_1 if itsin both its shared else its not
thanks

subodh .


Report Offensive Follow Up For Removal


Response Number 6
Name: FishMonger
Date: February 3, 2008 at 18:54:04 Pacific
Subject: perl comapring lines in an array
Reply: (edit)
The description of what you're tying to do is still unclear, and that might be due to English not being your primary language.

I haven't had enough free time to try and analyze what you what you've posted to be able figure out what you want, but the hash key should end up being the portion of the line that you want to compare, and the value could be either the hole line or an array of lines or a count of matching lines. When I have some free time, I'll see if I can work up something.


Report Offensive Follow Up For Removal



Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: perl comapring lines in an array

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




acer 312T BIOS problem

K7 Turbo possible max fsb?

Pc anywher problem

WinFLP & OE/Outlook2003

Computer resets after a few minutes


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC