mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Dec 2002
Posts: 79
F
Fantas Offline OP
Babel fish
OP Offline
Babel fish
F
Joined: Dec 2002
Posts: 79
i'm looking for a way to count extensions in a text file like 1.jpg 2.gif etc
i know i would have to $read it and inc but i'm lost

Joined: Aug 2005
Posts: 525
S
Fjord artisan
Offline
Fjord artisan
S
Joined: Aug 2005
Posts: 525
It would help if you give the structure of the text file's contents.

Joined: Dec 2002
Posts: 79
F
Fantas Offline OP
Babel fish
OP Offline
Babel fish
F
Joined: Dec 2002
Posts: 79
ok
1.jpg
3.gif
1.png
2.mov

what i'm looking for is a way to set a variable then inc that variable

Joined: Sep 2005
Posts: 2,881
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
$lines(file.txt)

Joined: Dec 2002
Posts: 79
F
Fantas Offline OP
Babel fish
OP Offline
Babel fish
F
Joined: Dec 2002
Posts: 79
yeah but $lines returns all lines not just ones with the extensions

Joined: Sep 2005
Posts: 2,881
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
Code:
alias filterext {
  if ($isfile($2-)) {
    var %re = $+(/^\Q,$replacexcs($1,;,\E|\Q,\E,\E\\E\Q),\E$/)
    filter -ffg $qt($2-) nul %re
    echo -a * Found: $filtered
  }
}


/filterext <extensions> <file>

<extensions> should be a list of extensions you want to match like *.mp3;*.mpg;*.etc

Joined: Dec 2002
Posts: 3,547
S
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 3,547
Code:
alias ext {
  var %x = 1, %f = $$1
  while (%x <= $lines%f)) {
    if ($gettok($read(%f,%x),2,46)) inc $+(%,$v1)
    inc %x
  }
}


/ext <file>

Joined: Sep 2005
Posts: 2,881
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
There's a few problems with this script.

1) Setting %f to $$1 will not take into account files that have spaces in the name. Use $$1- instead.

2) $lines%f should be $lines(%f)

3) Calling $read without the 'n' flag will evaluate identifiers in the file.

Also, that loop could be greatly sped up by setting $lines(%f) to a variable instead of calling it in each iteration of the loop:

Code:
var %x = 1, %f = $$1-, %l = $lines(%f)
while (%x <= %l) {
...
}

Joined: Dec 2002
Posts: 3,547
S
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 3,547
Ahh good find, getting a little tired here. Cheers. smile

Joined: Dec 2002
Posts: 3,547
S
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 3,547
Code:
alias ext {
  var %n = ext
  .fopen %n $$1-
  if ($ferr) { .fclose %n | return }
  if ($fopen(%n)) {
    while (!$feof) {
      if ($gettok($fread(%n),2,46)) { inc $+(%,$v1) }
    }
    .fclose %n
  }
}


I think this might be a little faster than my $read post.

Joined: Dec 2002
Posts: 79
F
Fantas Offline OP
Babel fish
OP Offline
Babel fish
F
Joined: Dec 2002
Posts: 79
ok not quite sure i follow ya here %f is my text file?
alias ext {
var %x = 1, %f = $$1-, %l = $lines(%f)
while (%x <= %l) {
if ($gettok($read(%f,%x),2,46)) inc $+(%,$v1)
inc %x
}
}
so i type /ext .jpg pics.txt?

Joined: Sep 2005
Posts: 2,881
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
Mine is /filterext <extensions> <file>, Slade's is /ext <file>

Joined: Dec 2002
Posts: 3,547
S
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 3,547
Nope, type:

/ext <file>

Then it'd look like:

%png 1
%jpg 2
%bmp 30

Or something similar

Joined: Dec 2002
Posts: 79
F
Fantas Offline OP
Babel fish
OP Offline
Babel fish
F
Joined: Dec 2002
Posts: 79
Hixxys keeps returning 0 and /ext does nothing
i made a file called it pics.txt in the file i put
1.jpg
2.gif
1.png
3.mov
1.wmf

none worked

Joined: Sep 2005
Posts: 2,881
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
How did you call my alias?

Joined: Dec 2002
Posts: 79
F
Fantas Offline OP
Babel fish
OP Offline
Babel fish
F
Joined: Dec 2002
Posts: 79
/filterext .jpg pics.txt

Joined: Sep 2005
Posts: 2,881
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
Silly mistake.. just remove the "^" character from my alias:

Code:
alias filterext {
  if ($isfile($2-)) {
    var %re = $+(/\Q,$replacexcs($1,;,\E|\Q,\E,\E\\E\Q),\E$/)
    filter -ffg $qt($2-) nul %re
    echo -a * Found: $filtered
  }
}

Joined: Dec 2002
Posts: 79
F
Fantas Offline OP
Babel fish
OP Offline
Babel fish
F
Joined: Dec 2002
Posts: 79
that works Hixxy, thanks


Link Copied to Clipboard